Alarm switch: How combine physical switch and "software" switch? rule

Hello,
actuall I have a simple (physical) switch to activate the house alarm.
The code is this:

rule "Alarm"
when Item DoorTerrace changed from CLOSED to OPEN or Item DoorLivingroom changed from CLOSED to OPEN
then
    if( Alarmswitch.state == CLOSED )
    {
       logInfo("contact.rules", "!!! ALARM !!! ")
       sendPushoverMessage(pushoverBuilder("Alarm...).withApiKey("d7sdzf86zh...."))
    }
end

Hint: The physical switch (“Alarmswitch.state”) it not a push button!

Now I want to have the option to disable (or activate) the alarm from mobile phone (traveling etc.) with a “switch” item in openHAB.

So:

  • When the physical switch is “on” (=closed; alarm active) the soft-switch should be able to deactivate the alarmsystem.
    (and after a physical switch-off => switch-on the alarmsystem should be activated again)

And:

  • When pysical witch is off (open; deactivated), the soft switch should enable the alarmsystem.
    (and after a physical switch-on => switch off the alarmsystem should be disabled again).

Hope the explanations are clear :wink:

Will be great if anyone give me a hint how to to proceed here … no so good in rules etc. :frowning:

Based on the description, physical switch on or off, use the Soft_Switch to control the alarm.

rule "Alarm"
when Item DoorTerrace changed from CLOSED to OPEN or Item DoorLivingroom changed from CLOSED to OPEN
then
    if(Soft_Switch.state == ON)
    {
       logInfo("contact.rules", "!!! ALARM !!! ")
       sendPushoverMessage(pushoverBuilder("Alarm...).withApiKey("d7sdzf86zh...."))
    }
    else 
    {
       sendPushoverMessagesend(pushoverBuilder("Alarm deactivate) withApiKey("xxxxx"))
    }
end

Here’s where I’m a bit confused. Is the “switch-on the alarmsystem” the same as the Soft_Switch or something different?

Hi,
as written, actuall I only have a physical (hardware) switch to activate or deactivate the alarm system.

Now I wish to be able to switch the alarm system on and off with an additionally “software” switch.
They are different “switches”, because the existing one is real, the new one is a software switch in openHAB only.
Both will do the same at the end: Activate or deactivate the alarm-system (independently).

I assume OH can read the state of the physical switch, and the alarm can be activated/deactivated regardless of it’s state via the Soft_Switch.

Can OH communicate with the alarm, not the physical switch, and know if it’s active or not?

“Can OH communicate with the alarm, not the physical switch, and know if it’s active or not?”

No, the “alarm-system” is the oH himself, as you can see in the rule description here:
if(AlarmSwitch.state == ON)

oH will send an Pushover-Message to our smartphones when (actual) the physical alarm-switch is closed AND one of the door inside opens. Thats all!

Maybe it’ “easier” for a oH rule, if the switch is a push button?

I can change this, the switch was “historical” …

You might find it easier to think about if you make an Item representing “Alarm Active”, not bound to any real device.

You can then have rules that test to see if Alarm Active is on when a door opens.

A rule that says when the wall switch changes, change the Alarm Active state.

There’s no reason you can’t put this Alarm Active directly into your sitemap as a switch, so that you can both see and change it.

Just starting to change my physical on/off switch into a push button.
Also I want to add an LED on a GPIO to see if the Alarm is activated.

Found this example page where the GPIO, LED and switch was described.

This two lines should be added into the items file:

Switch GPIO_LAMP "Lamp" (LivingRoom) { gpio="pin:23 force:yes" }
Contact GPIO_BUTTON "Button [%s]" (LivingRoom) { gpio="pin:24 activelow:yes" }

But where is “connection” between the switch on GPIO24 and the LED on GPIO23 which says:
If GPIO_Button was pressed, switch on the GPIO_LAMP?
I expect and thought there must be a rule ? I overlook something?

Not sure what you’re saying? The GPIO Items are unconnected as they stand, and a rule would be needed to make things happen in response to button pressing. There is no example rule in the linked posting, but it would be straightforward.

For your alarm project, you could rename the LED Item as “Alarm Active” and use it in your rules and sitemap directly. That is your “soft switch” could be the LED Item.

For the button, you could have a rule triggered from changed to CLOSED that changed the state of “alarm active”

rule "Alarm setting toggle"
   when Item myPushbutton changed to CLOSED
then
    if( Alarmswitch.state == ON )
    {
       Alarmswitch.sendCommand(OFF)
    } else {
       Alarmswitch.sendCommand(ON)
    } 
end

Later on, you could add cleverness that stopped the alarm being set if a door was open etc.

In my example I show, these two lines (switch GPIO_Lamp …, contact GPIO_Button …), are they “enough” to switch a LED (on GPIO 23) with a physical switch (on GPIO 24) , that was my question. I thought “no, thats not enough to work”. But this was only an (foreign) example i found and try to understand!)


My case:

What I want is a physical push button and a “soft” button in openHAB to activate/deactivate the alarm.
And a LED on a GPIO should show if the alarm is activated or not.

For the physical push-button I have to create a contact item (my name: “AlamHardbutton”) in my contacts.item -file. (correct?)
For the soft-switch I have to create an switch-item on my sitemap (correct?) (this switch-button will/can also show if Alarm is actuall activated or deactivated (correct?))

And the rule (which need to be created) have to check (monitor) the pyhsical button and the soft-button (if activated), than decide: Alarm state is switched from: on=>off or from: off=>on.
And if alarm status is “on”, the LED on GPIO should be get powered on.

Puhhh, I think I need help … my wife expect that the alarm is working soon again because we will go to holiday next week …

Try this and see if it works for the push button.

Items:

Switch GPIO_LAMP "Lamp" (LivingRoom) { gpio="pin:23 activelow:yes initialValue:high force:true" }
Contact GPIO_BUTTON "Button [%s]" (LivingRoom) { gpio="pin:24 activelow:yes" }

Rule for push button:

rule "Alarm"
when
    Item GPIO_BUTTON changed to ON
then
    GPIO_LAMP.sendCommand(if(GPIO_LAMP.state == ON) ON else OFF)
end

Thanks a lot!

Now I can switch on/off the LED (GPIO22) with the soft-button (“switch” in oH), the GPIO_Buttom (GPIO17) I see in the log (so it works physically), but the LED did not change status.

Also not sure if your written statement is correct:

GPIO_LAMP.sendCommand(if(GPIO_LAMP.state == ON) ON else OFF)

Should it not:

GPIO_LAMP.sendCommand(if(GPIO_LAMP.state == ON) OFF else ON)…

I try both versions but both did not change anything on the LED (or switch state in oH)

Hmmm…

What happens when you try it this way?

GPIO_LAMP.sendCommand(if(GPIO_LAMP.state == OFF) ON else OFF)

Alternate if the above does not work as needed.

rule "Alarm"
when
    Item GPIO_BUTTON changed
then
    if (GPIO_LAMP.state == ON) {
        GPIO_LAMP.sendCommand(OFF)
    }
    else if (GPIO_LAMP.state == OFF) {
        GPIO_LAMP.sendCommand(ON)
    }
end

Nothing changed …

In my sitemap I have added these two lines:

Switch item=GPIO_LAMP
Text item=GPIO_BUTTON

In a new created alarm.rules I have this:

rule "Alarm"
when
    Item GPIO_BUTTON changed to ON
then
    GPIO_LAMP.sendCommand(if(GPIO_LAMP.state == OFF) ON else OFF)
end

And in my new created alarm.items I have these two lines:

Switch GPIO_LAMP "Lamp" (LivingRoom) { gpio="pin:22 activelow:yes initialValue:high force:true" }
Contact GPIO_BUTTON "Button [%s]" (LivingRoom) { gpio="pin:17 activelow:yes" }

In the log I see:

2018-12-27 17:42:47.399 [vent.ItemStateChangedEvent] - GPIO_BUTTON changed from OPEN to CLOSED

2018-12-27 17:42:47.880 [vent.ItemStateChangedEvent] - GPIO_BUTTON changed from CLOSED to OPEN

Try the alternate rule as the above one liner is more for controlling a pushbutton/relay.

Rule

rule "Alarm"
when
    Item GPIO_BUTTON changed
then
    if (GPIO_LAMP.state == ON) {
        GPIO_LAMP.sendCommand(OFF)
    }
    else if (GPIO_LAMP.state == OFF) {
        GPIO_LAMP.sendCommand(ON)
    }
end

OK, now the LED is going on, but only as long as the button is pressed. After releasing the LED is going off again (same with the switch-ion in oH).

When using the soft-switch, the LED is going on (or off) permanently.

Yes, it’s a push button for toggling. I think I see what you are wanting so remove the if on the else if part of the rule like below.

rule "Alarm"
when
    Item GPIO_BUTTON changed
then
    if (GPIO_LAMP.state == ON) {
        GPIO_LAMP.sendCommand(OFF)
    }
    else (GPIO_LAMP.state == OFF) {
        GPIO_LAMP.sendCommand(ON)
    }
end

Just thought it work …
I can switch the LED off with the phy. button, but not switch on again.
When pressing, the LED is flicker, but every time the LED is “off”. A timing issue from the GPIO?

I add “debounce:50” in the alarm.item -part, but nothing changed:
Contact GPIO_BUTTON “Button [%s]” (LivingRoom) { gpio=“pin:17 debounce:50 activelow:yes” }

Are you just wanting the LED to turn on/off, and remain that way (similar to the soft switch), whenever you press the button?

Yes :wink:
Should remain the status … LED burns whole time if “ON” …