I have combed over this rule a lot to see where my error is but I am pulling my hair out!
I set it up in paper UI but with one “then” statement and it works great. My rules file loads up fine in the console logs no errors, but nothing happens…
Is there something I am missing?
Thanks for taking a look
rule "motion alarm"
when
Item HuePresenceSensor_Motion received update ON
then
if (Alarm_Sound_Switch.state == ON) {
KitchenEcho_Speak.sendCommand('Please enter your password to disable the alarm')
Thread::sleep(2000)
playSound("countdown.mp3")
Thread::sleep(8000)
if (DummyAlarmOnSwitch_Switch.state == OFF) {
// do nothing
} else {
KitchenEcho_Speak.sendCommand('Unauthorized Presence Detected. Alerting the home owner and routing call to emergency services.')
sendHttpPostRequest("<request a phone all from ifft>")
playSound("sonos:zoneplayer:RINCON_XXXXXXXXXXXXXX","alarmtriggered.wav")
}
}
end
Take a look at this topic about why thread sleep is a bad idea.
Fo the rule try changing the if and else parts.
Example:
when
Item HuePresenceSensor_Motion received update ON
then
if (Alarm_Sound_Switch.state == ON) {
KitchenEcho_Speak.sendCommand('Please enter your password to disable the alarm')
//Thread::sleep(2000)
createTimer(now.plusSeconds(2)) [|
playSound("countdown.mp3")
]
createTimer(now.plusSeconds(8)) [|
Thread::sleep(8000)
]
}
else (DummyAlarmOnSwitch_Switch.state == OFF) return;
if else {
KitchenEcho_Speak.sendCommand('Unauthorized Presence Detected. Alerting the home owner and routing call to emergency services.')
sendHttpPostRequest("<request a phone all from ifft>")
playSound("sonos:zoneplayer:RINCON_XXXXXXXXXXXXXX","alarmtriggered.wav")
}
}
end
Here’s a good link to describe how to use if, else, and if else.
I have no idea why your rule doesn’t get triggered at all.
Are you sure the rule file gets loaded without errors?
Thread::sleep() is indeed a bad idea (when it comes to values above 500 Milliseconds).
This version is a little state machine:
// global var and val are defined at top of rules file
var Timer tAlarm = null // pointer to timer
var Number nAlarm = 0 // step index
rule "motion alarm"
when
Item HuePresenceSensor_Motion changed to ON // so it was off before...
then
if (Alarm_Sound_Switch.state == ON && DummyAlarmOnSwitch_Switch.state != OFF) {
tAlarm?.cancel
nAlarm = 0
tAlarm = createTimer(now, [|
nAlarm += 1
switch (nAlarm) {
case 1 : {
KitchenEcho_Speak.sendCommand('Please enter your password to disable the alarm')
tAlarm.reschedule(now.plusSeconds(2))
}
case 2 : {
playSound("countdown.mp3")
tAlarm.reschedule(now.plusSeconds(8))
}
case 3 : {
KitchenEcho_Speak.sendCommand('Unauthorized Presence Detected. Alerting the home owner and routing call to emergency services.')
sendHttpPostRequest("<request a phone all from ifft>")
playSound("sonos:zoneplayer:RINCON_XXXXXXXXXXXXXX","alarmtriggered.wav")
}
}
])
}
end
rule "stop Alarm"
when
Item DummyAlarmOnSwitch_Switch received command OFF
then
tAlarm?.cancel
end