Rules triggered at startup

Whenever I restart openHAB, my rule that responds to changes in the DSC alarm fires as though it has received an update from the panel. It hasn’t, it’s just getting updated as the persistence restores the existing value.

Is there any wa of preventing this from happening. Apart from not persisting that item?

Thanks

Continuing the discussion from Can't quite get started with rules:

Hello @tinkerfailure,

When the DSC Alarm binding initially connects to the alarm system it sends a Status Report command (001) to get status information on things like partitions and zones. This is probably what your rule is responding to when you restart openHAB. I’m not sure how you can avoid that in the rule.

@tinkerfailure, have you tried fully specifying your item change transition? For example…

Item <item> changed from OFF to ON

This will not fire if the item state is undefined (at startup) and then set to ON by the persistence code.

1 Like

Following on what @steve1 is saying here are the rules I use to send me emails when the partition arms and disarms:

rule "Partition1Armed"

when
    Item PARTITION1_ARMED changed from OFF to ON
then
    emailMessage = "DSC Alarm Stystem Armed\n\nPartition 1 has been armed."
    sendMail(emailTo,emailSubject,emailMessage)
end

rule "Partition1Disarmed"

when
    Item PARTITION1_ARMED changed from ON to OFF
then
    emailMessage = "DSC Alarm Stystem Disarmed\n\nPartition 1 has been disarmed."
    sendMail(emailTo,emailSubject,emailMessage)
end

I had the same problem…

Solved it with;
folder:persistence=15,persist
folder:rules=30,rules

Seems this loads the persistance 15 sec before rules (they are also only refreshed every 30 sec but thats okay)…

Yeah thanks @rsstephens and @steve1 , it looks like I need to change the trigger for this. I’ll try using PARTITION1_ARMED as the trigger, and then perform the if checks on PARTITION1_ARM_MODE afterwards.

Which will mean splitting it into two rules instead of one. One for ‘arming’ and one for ‘disarming’.

At the moment I’m just checking for a change of PARTITION1_ARM_MODE and then matching the different arm/disarm states to do different things.

rule "Alarm State Change Lighting Actions"
when    
        Item PARTITION1_ARM_MODE changed
then
        if(PARTITION1_ARM_MODE.state == 0) {
...
            if(PARTITION1_ARM_MODE.state == 1) {
    ...
........

I tried changing the settings in openhab.cfg as @unixhaj suggested, but it still behaves the same.

EDIT:
I’d like to perform different actions on the event of a ‘disarm’ happening. If it’s from an ‘away’ arming, and it’s not daylight, I’d like to switch the lights on. If it’s from a ‘stay’ arming, it might be because my child has woken in the night and we’ve come downstairs to the kitchen for some medicine, so I don’t really want all the the light to go on throughout the house… waking everyone else up! Can I access the previousState for a different item than the one I’m using? i.e could I reference the previousState of PARTITION1_ARM_MODE when triggered by PARTITION1_ARMED?
Or maybe I could capture the ‘arm mode’ of the alarm in a variable before it gets changed by the disarming?

Looks like I’ve sorted it. Thanks @rsstephens and @steve1.

I’ve changed to using Item PARTITION1_ARMED instead, and it seems to prevent the rule being triggered on startup. I had to re-jig things a little bit to split them into two rules, but that has other advantages for other things, so that’s a plus too.

Now I’ve got:

java
    rule "Alarm Disarm Lighting Actions"
    when     Item PARTITION1_ARMED changed from ON to OFF
    then
            if(PARTITION1_ARM_MODE.state == 0) {
            
                logInfo("Alarm","Partition Disarmed") 
                    val message = "The alarm is now\n\nDisarmed and is ready\n\nRegards,\n\nopenHab"
                    sendMail(mailTo, "Alarm Disarmed", message)
                    
                    if(daylight.state == OFF)    {
                        logInfo("Alarm","It's dark outside so lights to normal")
                            normal.members.forEach[light |  // for each light
                            light.sendCommand(ON)   
                        logInfo("Alarm","Lights to Normal Commands Sent")]
                    }  
                    else {
                         logInfo("Alarm","It's daylight. So we do nothing.") }
            }    
    end

    rule "Alarm Arm Lighting Actions"
    when     Item PARTITION1_ARMED changed from OFF to ON or
            Item Light_Test changed
    then    
        
                if(PARTITION1_ARM_MODE.state == 1) {
            
                logInfo("Alarm","Partition Armed AWAY")         
                    val message = "The alarm is now\n\nArmed Away\n\nRegards,\n\nopenHab"
                    sendMail(mailTo, "Alarm ARMED Away", message)
                
                logInfo("Alarm","The house is empty, all lights off in 10 seconds...")
                        waitTimer3 = createTimer(now.plusSeconds(10), [|
                    allLights.members.forEach[light |  // for each light
                    light.sendCommand(OFF)   ]]
                
                    logInfo("Alarm","ALL Lights OFF Commands Sent") 
                
            }

    ... and so on... 

So again, thanks.

Just for the record… I’ve had to introduce a ‘wait’ after the "Alarm Arm Lighting Actions" rule gets triggered as it seemed openHAB wasn’t getting the update to the PARTITION1_ARM_MODE immediately and so would not do anything 50% of the time. And I removed the if(PARTITION1_ARM_MODE.state == 0) as it was redundant.

So it’s now all working very smoothly and as expected!

java
    rule "Alarm Disarm Lighting Actions"
    when     Item PARTITION1_ARMED changed from ON to OFF
    then
            logInfo("Alarm","Partition Disarmed") 
                    val message = "The alarm is now\n\nDisarmed and is ready\n\nRegards,\n\nopenHab"
                    sendMail(mailTo, "Alarm Disarmed", message)
                    
                    if(daylight.state == OFF)    {
                        if(morning.state == OFF)    {
                        logInfo("Alarm","It's dark outside so lights to normal")
                            normal.members.forEach[light |  // for each light
                            light.sendCommand(ON)
                            logInfo("Alarm","Lights to Normal Command Sent")]
                        }
                        else    {
                            logInfo("Alarm","It's morning. So we do nothing.")    
                        }
                    }
                
    end

    rule "Alarm Arm Lighting Actions"
    when     Item PARTITION1_ARMED changed from OFF to ON
    then    
            logInfo("Alarm","Partition Armed, waiting for state to update...")
            armTimer = createTimer(now.plusSeconds(2), [|
                   if(PARTITION1_ARM_MODE.state == 1) {
                     logInfo("Alarm","Partition Armed AWAY")         
                            val message = "The alarm is now\n\nArmed Away\n\nRegards,\n\nopenHab"
                            sendMail(mailTo, "Alarm ARMED Away", message)
                    logInfo("Alarm","The house is empty, all lights off in 2 seconds...")
                                waitTimer3 = createTimer(now.plusSeconds(2), [|
                            allLights.members.forEach[light |  // for each light
                            light.sendCommand(OFF)   ]]
                    logInfo("Alarm","ALL Lights OFF Commands Sent") 
                }
                if(PARTITION1_ARM_MODE.state == 2) {
                    logInfo("Alarm","Partition Armed STAY") 
                            val message = "The alarm is now\n\nArmed Stay\n\nRegards,\n\nopenHab"
                            sendMail(mailTo, "Alarm ARMED Stay", message)
                      logInfo("Alarm","Everyone's in bed, lights out in 2 seconds...")
                            waitTimer4 = createTimer(now.plusSeconds(2), [|
                            night.members.forEach[light |  // for each light
                            light.sendCommand(OFF)    ]]
                      logInfo("Alarm","Nightime lighting Commands Sent")
                }
                if(PARTITION1_ARM_MODE.state == 3) {
                    logInfo("Alarm","Partition Armed AWAY no delay. Not sure what this is.") 
                        val message = "The alarm is now\n\nArmed Stay\n\nRegards,\n\nopenHab"
                        sendMail(mailTo, "Alarm ARMED Away no delay", message)       
                }
                if(PARTITION1_ARM_MODE.state == 4) {
                    logInfo("Alarm","Partition Armed STAY no delay. Not sure what this is.") 
                        val message = "The alarm is now\n\nArmed Stay\n\nRegards,\n\nopenHab"
                        sendMail(mailTo, "Alarm ARMED Stay no delay", message)     
                }]
    end