Using thing names variables to trigger other things with the same name

  • Platform information:
    • OS: ubuntu 24.04
    • openHAB version: 4.2.1
  • Issue of the topic:
    I am trying to write the following rule

let’s say you have several lights in your instllation, and some master lights

let’s call them light7_master, light8, light9_master, light7, light9 and light9 ( or master_light7 and master_light9, whichever is easier to code)

now i want to write a code that whenever a master light is triggered it triggers the same state to the non master lights with the same name/number

example

i trigger light7_master to on, then light7 should also be triggered to on
if i trigger light 8 only that light should turn on or off
and when i trigger master_light9 then all other light9 things should also trigger to the same state as master_light9

in addition it would be very usefull to have a code that isn’t pre defining the names, but rather uses variables in combination with the word master

so that the code is not light7, light9 sepecifi, but rather uses the naming convention used with the word master
so that i don’t need to write different rules for light7 and light 9 …

However, I have no idea on how to accomplish this…

  1. Take a look at the Follow profile, or
  2. Put the “masters” into a “Masters” group, and create a rule that triggers on this group members.

Then that rule simply takes in the name of the triggering item, and remove “_master” and propagate the command.

Example in JRuby

rule "Propagate command from master to slaves" do
  changed Masters.members, to: [ON, OFF]
  run do |event|
    slave_name = event.item.name.sub("_master", "")
    items[slave_name].command event.state
  end
end

You need to be consistent with your naming, and have _master as suffix, not prefix.

ok, thx for this, I will have a look.

But to auto populate the master group, I will need to write another script/rule probably, because i am looking to make it as autonomous as possible

You could write another rule, or you could just make sure to set the parent group as you go, whenever you add a master item

just to make sure, i just create a new script, select Ruby and paste the code, i already created a group called Master,

then i run the script, and it appears as idle in the rules, but it doesn’t work …
since i am fairly new to openhab i am wondering if i am doing something wrong

I assume you’ve installed the jruby automation addon.

The rule I typed above is for a file-based rule. If you are doing it on the UI (web browser), you need to set up the trigger on the UI and just paste the “body” of the rule in the UI Action part

    slave_name = event.item.name.sub("_master", "")
    items[slave_name].command event.state

For more details see File: USAGE — openHAB JRuby

You can also use blockly if you are not used to coding / programming.

(post deleted by author)

See Design Pattern: Associated Items for a generic approach to writing rules that construct the names of Items needed instead of hard coding them. This covers several languages (@JimT, if you want to add any jRuby examples to the design pattern posts I’m up for that, the only reason I’ve not done so is I don’t know jRuby well enough to write them myself yet).

The example @JimT shows is one way to implement that design pattern but it might help to read more about the approach and see it in other languages too in order to understand how it works.

Once you get the rule written correctly, it will run when any member of Masters changes from ON to OFF or OFF to ON. You don’t run it manually. The vast majority of the time it will show as idle. OH rules are event driven. X happens so do Y.