Hmmm, it seems that the action is returning a Java Collection instead of a JS Array. I should have thought of that.
There is a method in the helper.js of openhab-js that will convert it to a JS Array. However, it’ll still have Java Items in it, not the JS Item objects openhab-js usually gives you. So close attention to type is needed here.
Something like the following should be closer to correct.
var location = items.getItem(actions.Semantics.getLocation(items[event.itemName]).name);
var lights = location.members.descendents.filter(i => i.tags.includes("light") && i.tags.includes("switch"));
lights.forEach(light => light.sendCommandIfDifferent("ON"));
The change is on the first line. I get the name of the location Group item and use that to pull the JS Item object. From that point onward we are in pure JS and all the stuff provided by openhab-js should work as expected.
So far, when I want to switch off the lights in the living room, I have a “Lights_living_room.sendCommand(OFF)”. Apart from the command, is this approach correct/okay or is it better to use the semantic model?
Then you didn’t have a semantic model. If there are no tags, there’s no semantics.
Better it worse is going to be dependent on way more details than we have here and personal opinion. And again, it’s not either or. You might use a semantic model to build your overview pages in MainUI and keep your functional model and send command to Lights_living_room too.
I show how you works do it above without the Lights_livibg_room Group.
I don’t know if it’s the language barrier or my lack of understanding of the topic, but I tried to stick to the documentation as much as possible and therefore added all items to my “semantic” model via “Add Equipment from Thing”, which leads to a result like this:
I would like to adopt this approach, at least in part, and switch off all devices in the room via the location. Then I would only need one script for this and only transfer the room that is to be switched off.
So far so good, it looks like the metadata is my problem. Similar to you, I have light bulbs in the dimmers that, God knows why, need different values to achieve the same brightness. You wrote that this is done via the metadata, but when I open the menu I see this
You can use the bottom button „Enter custom namespace“. You can choose a namespace first and then enter the value in the next window. Just make sure that you use the same namespace on every item.
Oh cool, thanks!
I’ve created that now, but haven’t tested it yet because I’m currently having the problem that my non-semantic tag isn’t being saved.
I try to differentiate between shutters and curtains because the curtains should not be opened in the morning, that’s why I tried to create a non-semantic tag. Is it possible to create semantic tags apart from semantic-class and semantic-property?
Technically they are afaik both just tags. The difference is that the semantic tags are predefined but you can use any other tag you want for your own semantics. You won’t be able to use the specific semantics APIs in your rules though.
If you mean can you create your own new type of semantic tag for use in the model, then the answer is yes. There is not a UI for this yet nor has it been added to the documentation, I believe. However, if you go to the API explorer you will see there is a Tags endpoint. Using that endpoint you can get a list of the currently available semantic tags and see how the structure works. You can then use the same json structure to add a new semantic tag via that same endpoint. For example, I have a lot of the family’s personal devices in the semantic model (e.g., phones etc.) These don’t stay in a particular location but travel with the person, so I have created a person location for my model. It looks like this:
{
"uid": "Location_Person",
"name": "Person",
"label": "Person",
"description": "Any human resident of a location",
"synonyms": [],
"editable": true
}
But it’s important to remember that “regular” tags can only be added on the Item page when you click the “edit” icon under “non-semantic tags”. These tags can be anything.
But I’m this case it makes sense to create a new curtains semantic tag.
Yes, that solved the problem! But this should perhaps be mentioned somewhere because it is, at least in my opinion, a non-intuitive behavior.
And I guess I found a bug since I used the word “Blinds” the text disappears from the non-semantic tags, but it appears in the semantic class and the semantic property has disappeared.
I agree that it is not intuitive, maybe a little button saying „add Tag“ could help here.
The problem with the „Blinds“ tag is that it is already defined as a semantic tag. As said before I don’t think that these differ technically from any other custom tag when you create them. So I understand why this shows up as semantic tag after saving it. However I would agree that this behaviour is a bit strange. It would probably be better to prevent saving with a comment: „Sorry this is a reserved tag for the semantic model“ or something similar.