Rule for all switches

Hi,

Everyswitch that I will have will be mqtt based. Is there a variable that I could use in rules that, when a switch’s state is updated, that rule runs ?

Thanks

If a bindings gets a command or a status update from its bus, it will update the corresponding item. In openHAB you can let trigger rules dependent on updates or changes. The main difference is, if there is an update from oldstate ON to newstate ON, update will trigger, but changed will not. You can narrow the updates/changes down with a given state:

rule "my rule"
when
    Item myItem received update //will trigger whenever the item myItem receives an update
or
    Item myItem received update ON //will trigger whenever the item myItem receives an update to state ON
or
    Item myItem changed to OFF //will trigger if item myItem changes from whatever to OFF
or
    Item myItem changed from ON //will trigger if item myItem changes from ON to whatever
or
    Item myItem changed from OFF to ON //will trigger if item myItem changes from OFF to ON
then
    // do something...
end

Please keep in mind that even a switch item has more than two states, namely OFF, ON and uninitialized, so if you let trigger with changed from OFF to ON the rule will not trigger the first time, the item is updated, 'cause it changes from uninitialized to ON (or OFF)

As a third trigger, opanHAB can trigger to commands (but not simple status messages):

rule "my rule"
when
    Item myItem received command //trigger only, if  it is a command
then
    // do something
end

The main difference between update and command is the action in a rule. You can do a
sendCommand(item,value) which will send a command to the item (and this command will be sent from the item to each binding which is bound to it)
In contrast postUpdate(item,value) will cause an update of the item, this will not be sent to the bindings. But the UI will display this update for instance.

Thanks for your reply. Udo_Hartman

I think that is formulated my question badly.

What I mean is, how can I have a rule that will get all switches… like this

rule "my rule"
when
AllItems received update …

But, since, i read that I could use groups to do what I want.

Not sure yet

A little bit dangerous to trigger all updates, but sure you can putt all items in a group and trigger on updates to the group. What would be the scope?

All Switches will have a unique ID and dont want to set a per ID/switch rule.
So I was thinking a general rule that would grab the id of the switch and do a action apon it.

maybe i’m not doing it right :wink:

Hi,
Take a look at the following post:

More specifically, under the section called “Group and Filter” you will find a rule called “Override Lights” that I believe shows how to do what you want (that is, pick out which switch was operated out of a group of switches).

Indeed, the posting that @KjetilA links to does indeed show how you can get the most recently updated Item from a group but I want to point out a couple of caveats.

  • All Items need to be part of the same group and the rule need to trigger on Item MyGroup received update
  • However, when you do that the rule will be triggered multiple times per update to an Item. If this is a major problem you can trigger the rule on updates or commands received from each individual Item instead
  • This approach is a bit of a hack and if changes to Items in the group update too close together you will miss the earlier updates
  • You must have persistence set up and configured on all of the Items in the group

Yes, I think that tests are in order :slight_smile:
I already have persistense setup. I will add the switches to a pre-determined group and add that group to the persistence setup.

Thank you for all your help ! really appreciate it !