How to reuse code between UI rules?

But do you have equipment? This are Groups too.

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.

I have the semantic model, so far without tags and without metadata, and a non-semantic group for shutters, lights, presence sensors, etc.

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.

Thank you, this works. :+1:

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 haven’t read anywhere that you need to add tags or metadata to get a semantic model. :man_shrugging:t3:

Then you have added semantic tags.

The “Semantic Class” and “Semantic Property” are tags, semantic tags to be specific.

Hi,
it’s me again. :sweat_smile:

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


and then I’m scratching my head because I haven’t got a clue what to do. :grimacing:

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.

Wow, that’s a quick answer! :grin:
Okay, I’ll try that out. Thank you very much! :+1:t3:

Since it might not be 100% clear how to sweep the metadata here is a screenshot from my configuration:

It is optional to add configs to the config part, you may only need the value part.

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
  }

See OH 4.1: Tutorial to manage custom semantic tags for how to add it using a .yml file. There is an issue and I think a PR open to add it to the docs, not sure what the hold up is.

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.

Right now I have this

    var shutters = location.descendents.filter(i => 
        i.tags.includes('Opening') && 
        i.tags.includes('Control') &&
        !i.name.includes('Vorhang')
    );

I would like to use tags instead of name

Wouldn’t that be a bit too much effort for my use case?

It looks like that’s not the case for me, but I don’t know if it’s me or if it’s due to a bug.

After clicking on “Save” the tag is gone.

That’s exactly what I’ve tried to do.

Okay, then that’s what I’ll do. :slight_smile:

Press enter first. This is something I also stumbled over at some point.

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.


That’s why I didn’t save after pressing Enter, because I thought I had broken something

Using the word “Curtain”


still non-intuitiv to press enter but at least than the expected behavior is shown.

Can please someone confirm so that I can open a bug report?

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.

You are probably right, but “Blinds” is not a selectable option under Semantic-Class at this time.

And I only hit enter, I did not click on “Save”.