Given how you’ve defined the rule:
data.itemName
That’s the Item that triggers the rule. I think a GroupStateUpdateTrigger corresponds with Rules DSL
"Member oftrigger. So
data.itemName` will be the name of the Item that was updated that caused the rule to trigger.
This will be pretty much the same as Rules DSL. Assuming your Items that trigger this rule always end with “Playpause”
let itemRoot = data.itemName.replace('Playpause', '');
If not, it might be worth while renaming the Items to insert a marker and use split()
. For example if the Item names follow SqueezeBedroom_Playpause
let itemRoot = data.itemName.split('_')[0];
Assuming that’s all you need the root of the Item name for you don’t need the above and just need a replace.
let itemName = data.itemName.replace('Playpause', 'Power');
Using openhab-js you’ll want to pull the Item. Combining everything
items.getIten(data.itemName.replace('Playpause', 'Power').sendCommand('OFF');
So, based on what you describe you want to have happen it’s really just a one liner:
const {log, rules, triggers} = require("openhab");
rules.JSRule({
name: "ECMA2021Script - Squeezebox Watchdog",
description: "switch squeezebox off on pause",
triggers: [
triggers.GroupStateUpdateTrigger('gSqueezePlaypause')
],
execute: data => {
Items.getItem(data.itemName.replace('Playpause', 'Power').sendCommand('OFF');
});
},
tags: []
});
When a member of gSqueezePlaypause is updated, it will send the OFF command to the corresponding Power Item.
That’s probably not what you want. You probably only want some updates or commands to the Playpause Item to cause the OFF command to be sent to the Power Item but you’ve not provided enough info to understand what.
Based on what you are asking for I don’t quite understand the code. Some problems and potential problems I see:
-
What’s the forEach
there for? Do you really mean to turn off all the Items when the rule is triggered?That different from what you said but could be handled easily enough. Put all your Power Items into a Group and sendCommand to that Group. Again it’s a one liner only this time it’s items.getItem('gSqueezePower').sendCommand('OFF');
-
You should pull gSqueezePlaypause inside the rule. I don’t think that handle you get on the Item will remain good forever so you may need to refresh it when the rule runs.
-
playeritems.members.state
doesn’t make any sense at all. playeritems
is a Group Item. A Group Item has members
and, if it has an aggregation function also has a state
. members
doesn’t have a state. members
is an Array
of Items.
-
Again, playeritems.members
is an Array
of Item
Objects. It’s not a String
so there is no replace
on it.
See JavaScript Scripting - Automation | openHAB for the complete reference for Items
and Item
(which are the only two things you are using here). I don’t think the main problem here is syntax but a misunderstanding of what members
is.
For how to manipulate the members of the Group, see generic JavaScript references and tutorials for how to process an Array (e.g. findFirst, sort, map, reduce, forEach, etc.). members
is just a plain old JavaScript Array
. It holds Item
Objects (see JavaScript Scripting - Automation | openHAB).