Skip to content

Instantly share code, notes, and snippets.

@ketra

ketra/Motion.js Secret

Last active Sep 3, 2018
Embed
What would you like to do?
Motion Script Homey
// Motion.
try {
var d = new Date().getHours();
let devices = await Homey.devices.getDevices()
console.log(args[0])
if (args[0].substring(0, 3) != 'PIR' && args[0].substring(0, 3) != 'SEC') return;
if (args[0].substring(0, 3) == 'PIR') {
var SearchString = args[0].substring(6)
if (args[0].substring(3, 4) == 'D') var dimmer = true;
if (args[0].substring(4, 5) == 'N') var night = true;
var device = _.find(devices, function (o) { return o.name == SearchString; });
var Trigger = _.find(devices, function (o) { return o.name == args[0]; });
var security = false
}
else if (args[0].substring(0, 3) == 'SEC') {
console.log('Sec Sensor nothing needed')
var Trigger = _.find(devices, function (o) { return o.name == args[0]; });
var security = true
}
console.log(Trigger.name)
}
catch (err) {
console.error(err)
}
try {
if (Trigger.state.alarm_motion) {
let alarmstate = await GetAlarmState()
if (!security) {
if (!alarmstate) {
if (dimmer) {
if (night && (d < 5 || d > 23)) {
device.setCapabilityValue('dim', 0.35);
console.log('Swithed ' + device.name + ' To 35 percent')
}
else {
device.setCapabilityValue('dim', 0.75);
console.log('Swithed ' + device.name + ' To 75 percent')
}
}
else {
device.setCapabilityValue('onoff', device.state.onoff);
console.log('Swithed ' + device.name)
}
}
else {
resutl = await ExecuteAlarmFlow('Trigger')
console.log(resutl)
}
}
else {
if (!alarmstate) { return }
resutl = await ExecuteAlarmFlow('Trigger')
console.log(resutl)
}
}
return device.name;
}
catch (err) {
console.log(err)
}
async function GetAlarmState() {
let alarm = false
let State = await Homey.apps.getApp({ id: 'nl.regoor.statecontrol' })
let statecontrol = await State.apiGet('get-settings')
let settings = statecontrol.settings
let sections = settings.sections
let states = settings.states
let groups = settings.groups
var current = await State.apiGet('section-states')
var section = _.find(sections, function (o) { return o.description == 'Alarm' });
var state = _.find(states, function (o) { return o.description == 'ALARM' });
Object.values(current.sectionStates).forEach(status => {
if (status.sectionId == section.id && status.stateId == state.id)
alarm = true
})
return alarm
}
async function ExecuteAlarmFlow(SetState) {
let State = await Homey.apps.getApp({ id: 'nl.regoor.statecontrol' })
let statecontrol = await State.apiGet('get-settings')
let actions = statecontrol.settings.flowActions
let settings = statecontrol.settings
let sections = settings.sections
let states = settings.states
let groups = settings.groups
var section = _.find(sections, function (o) { return o.description == 'Alarm' });
if (SetState == 'Alarm')
var action = _.find(actions, function (o) { return o.description == 'Alarm Enabled' });
else if (SetState == 'Normal')
var action = _.find(actions, function (o) { return o.description == 'Alarm Disabled' });
else if (SetState == 'Trigger')
var action = _.find(actions, function (o) { return o.description == 'Alarm Triggered' });
console.log(action)
let result = await State.apiGet('/execute-flow-action/?sectionid=' + section.id + '&actionid=' + action.id)
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.