How to make a rule to control 3 switch (impulse controlled) items displayed as one mapped to 3 in sitemap

Main goal - control 3 opening gates with only 1convenient item displayed in my sitemap.

For that purpose I created the following sitemap item:

Switch item=Bramy_przelacznik mappings=[1="Garażowa", 2="Wjazdowa",3="Furtka"]

Here are my 3 items:

Switch Brama_Garazowa_wlacznik				"Brama Garażowa"		(Parter_Garaz)		{knx="1/1/1"}
Switch Brama_Wjazdowa_wlacznik				"Brama Wjazdowa"		(gOgrod)			{knx="2/2/2"}
Switch Furtka_Elektrozaczep		    		"Furtka"	        	(gOgrod)			{knx="4/4/4"}

Number Bramy_przelacznik					"Brama"		<garage>

And this is my rule which I created some time ago and was using it for some time:

        rule "Otwieranie Bram, Furtki"
    when   
   		 Item Bramy_przelacznik received command
    then   
 			if(Bramy_przelacznik.state == 1) {
 				sendCommand(Brama_Garazowa_wlacznik,ON)
 				//postUpdate(Brama_Garazowa_wlacznik,OFF)   
 				logInfo("rules", "Otwieranie Bramy Garazowej zostalo aktywowane")
 			}
 			if(Bramy_przelacznik.state == 2) {
 				sendCommand(Brama_Wjazdowa_wlacznik,ON)
 				//postUpdate(Brama_Wjazdowa_wlacznik,OFF)
 				logInfo("rules", "Otwieranie Bramy wjazdowej zostalo aktywowane")
 			}
            if(Bramy_przelacznik.state == 3) {
 				sendCommand(Furtka_Elektrozaczep,ON)
 				//postUpdate(Brama_Wjazdowa_wlacznik,OFF)
 				logInfo("rules", "Otwieranie Furtki zostalo aktywowane")
 			}
    end

I can frankly say - it’s working, but not so well.

First - when I click on one of the buttons, let’s say on ‘1’, then button is displayed as pushed, gate is opening, but when I want to stop or close it, then I need to click 2 times - once so it changes from on to off and again to change it’s state from off to on.
It becomes a pain when you have 3 like that as I have.

Second - when I am at or near my house I use wifi for connection. Outside it or near one of my gates connection is switched to 3g/LTE. Usually due to that switchover from wifi to mobile I loose the connection to openhab and clicking on the button has no effect. After connection is recovered I am not sure if gate will be opening or closing at that time.

I would like to operate it more like when using a pulse button as this is how my gate operates.
Best would be to have a buttons that are always white(OFF state) and every sends an ON impulse only. Perhaps, I could get rid of the second problem as well with that.

Any ideas how to update it ?

Your rule has an important problem, because it triggers on the Item receiving a command.
Your Number Item will autoupdate from the command by default, but it takes time. If you test the .state of the Item inside that rule, it may not be the state that matches the last command, and it may even change state part way through the rule.

You could trigger the rule from received update
Or you keep the command trigger, but use receivedCommand inside the rule instead of the unreliable item.state. I’d do it that way.

I don’t understand what you outputs need, so it’s difficult to say how to get the effects you want. Are they a bit like push-for-start , push-for-stop buttons?
Would it help to use the expire binding to convert simple ON commands to ON-delay-OFF ?
Would it help to add a UI button 0 for a ‘STOP’ function?

1 Like

You might take a look at this tutorial from @KidSquid that describes his setup for controlling two garage doors and an automated gate. Search for the word momentary to see how he implemented a momentary contact switch (like a pushbutton that is only ON while pressed.) He used the expire binding with a Switch item along with the autoupdate option set to false to achieve this.


There is also the following post asking for help with the implementation of a pushbutton:

1 Like

Thanks for the replies!

answer from rossko57 helped me to update my rule so it’s working more reliable and another answer from scottk with the suggested link gave me a nice expire rule to implement.
I was playing around with expire binding as suggested, but 1s is too slow and you see those buttons strangely changing states.

After initial testing it looks to be working great - finally :slight_smile:

Here is my working solution with 2 rules:

rule "Otwieranie Bram, Furtki"
    when   
   		 Item Bramy_przelacznik received command
    then   
 			if(receivedCommand == 1) {
 				sendCommand(Brama_Garazowa_wlacznik,ON)
 				logInfo("rules", "Otwieranie Bramy Garazowej zostalo aktywowane")
 			}
 			if(receivedCommand == 2) {
 				sendCommand(Brama_Wjazdowa_wlacznik,ON)
 				logInfo("rules", "Otwieranie Bramy wjazdowej zostalo aktywowane")
 			}
            if(receivedCommand == 3) {
 				sendCommand(Furtka_Elektrozaczep,ON)
 				logInfo("rules", "Otwieranie Furtki zostalo aktywowane")
 			}
    end

rule "Impulse buton timeout for gates"
    when
        Item Bramy_przelacznik received command
    then
        createTimer(now.plusMillis(100), [ | Bramy_przelacznik.postUpdate(0) ] )
    end