I want to create a switch that sends 2 sendCommand actions (1 for the ON and another for the OFF)
I have this working as follows currently:
.items
Switch test_on_off_dimmer
.rules
rule "Buttons on" when Item test_on_off_dimmer received update ON then {sendCommand(VB3_1A, ON)} end
rule "Buttons off" when Item test_on_off_dimmer received update OFF then {sendCommand(VB3_1B, ON)} end
However this takes 2 rules for every switch I want to create like that. So my question:
Is there a smarter way to code the above? Can I immediately integrate the SendCommend actions in the switch definition code?
I love the flexibility of OH but still have to learn a lot about clean and efficient coding
First, you will need only one rule. I would prefer to trigger on received command, because received update will trigger even if only a postUpdate() was sent (and postUpdate should not switch any lights):
rule "Button test_on_off_dimmer "
when
Item test_on_off_dimmer received command
then
if (receivedCommand == ON) {
sendCommand(VB3_1A, ON)
}
else {
sendCommand(VB3_1B, ON)
}
end
Further, if changing the item names, you could use a more general rule to switch many items individually. Put all items in a group and select the right ones from the name of the trigger item to send the command. I think there was a design pattern for that, but I didn’t find it yet.
Thanks, very valid point about the receive update vs receive commend especially because I want to link status separately to status of a light being on or off without triggering a command at that point, so the receive update would possibly create loops and strange things later on. Thanks.
Yes I thought about 1 rule with IF statement as well. I’m not sure what is more resource efficient 1 rule trigger + if statement or 2 rule triggers but code wise it doesn’t make a huge difference.
I was hoping there was a way to do it without any rules (in the item definition).
Never ever worry about making rules resource efficient until you after you know you have a performance problem. It will result in unnecessarily complex and hard to maintain code and a lot of unneeded effort on your part to code it. It is, IMHO, a complete waste of time.
(NOTE: I’m only talking about the context of Home Automation rules in openHAB. This general rule does not necessarily apply to all programming.)
I don’t know if I understand what you are trying to do because your example isn’t sending two commands at all. It is just sending the one command to VB3_1A. In which case, why not just use VB3_1A in the first place? I don’t see what value test_on_off actually provides.
However, if your example is contrived to the point that it doesn’t illustrate what you are really trying to do, then @Udo_Hartmann is on the right track with Groups. But you can use the Group directly:
For example, assuming all the Items to be commanded are of the same type (let’s use Switch since it is what your example uses) you can define the Group as:
Group:Switch:OR(ON,OFF) test_on_off_dimmer
If you put test_on_off_dimmer on your sitemap as a Switch or sendCommand to it from a Rule, all the members of that Group will receive the command. The state of the Group as I defined it above will be ON if one or more members of the Group are ON and OFF otherwise.
No it sends to VB3_1A in case of ON and to VB3_1B in case of OFF so its to 2 different switches
I’m not sure the group will be useful here because I have a lot of these 1 to 2 switches but always different ones. But I will definitely look into it in detail because I think there might be other use cases, so I do absolutely appreciate the help and info. Thanks a lot.
They are switch items linked to the Nikobus binding (acting as buttons)
This is how they are set up
Switch VB3_1A {nikobus="#N8103CF[0A0E-1]"}
Switch VB3_1B {nikobus="#NC103CF[0A0E-1]"}
(and I had to configure them anyhow for feedback purposes of the Dimmer function, so they are set up anyhow)
They are actually part of the control of a dimmer in Nikobus. The A button turns the dimmer off, the B button turns it on (to the same state as it had last time and the actual remaining part is configured as a dimmer
Dimmer NB_D2_O1_buttonsbed “Buttons bed” (gLichtBoven) [“Lighting”,“LichtBoven”] {nikobus=“0A0E:1”}
What I like is the on and off functionality, especially the on putting the dimmer to the last state (I could link a new switch directly to the Dimmer NB_D2_O1 but that would put the dimmer at 100% when switched on)
What I’m actually trying to do is save real estate in Habpanel
I want to combine the on/off with the slider functionality. The most straightforward way is with 2 buttons (one button linked to the VB3_A and the other to the VB3_B)and a slider in habpanel. This gives me the below
But much nicer visually is creating a toggle switch that when pressed when the light is on send the off command as sendcommand to the VB3_A one and otherwise to the VB3_B
So that’s the full context of what I’m trying to do. As I have this pretty much for every dimmed light I was looking for the most cleanest and efficient way to do it
OK, if you were using HTTP, MQTT, Exec, or any number of other similar bindings you could make this work without rules. With the Nikobus binding you have to use Rules.