[SOLVED] Rule not triggering when set up as a rules file but works in paper UI

Hey Guys,

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 :slight_smile:

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.
https://www.w3schools.com/js/js_if_else.asp

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

If you’re editing rules in two ways, bear in mind that every rule name must be unique.

Thanks for all your tips guys.
Yeah I have read about sleep not being a good idea, but this is the only rule running when noone is home.

I figured it out late last night. The motion Item was set as a switch with the type “motion”. When I set it to switch, it started working.

Is not that weird?

It showed up in the console exactly the same, but must make some difference…

This actually looks alot cleaner. I will give this a go. I am still wrapping my head around the syntax of rules and get a lost easily :blush: