Using room temperature in rules and triggers

I have a room with several devices that provide temperature measurements. In the “Locations” tab, I can therefore see the room’s temperature.
Is there a simple way of getting this in a rule/script (I am thinking javascript), without manually creating a group of all temperature-providing items in the room?

I guess I could do it with a double loop, first over all devices (members of the room group) and then over the endpoints (items in the device groups) and somehow check if they provide temperature … but I hope there is a better way…

And what if I wanted to have a rule that triggers when the room temperature changes - can this be done?

Is there a particular reason that you want to avoid this? Groups are the simplest way there is to average values from multiple devices. Anything you do with rules will be more complicated than the built-in method.

Trigger on the group or group items changing. You want the rule to trigger on every change (regardless of values), then have IF statements in the rule to determine if any actions should be taken.

Mostly, it feels wrong to have to specify this manually, when the information is there in the semantic model already.
But also practically: if I move a device to another room, there will be one more thing to update, and therefore also potential for inconsistencies…

But let’s say I create a group (for all thermometers in the given room) - is there a way to add/remove members by a script/rule, so I could run it once a day to keep things up to date?

I can’t speak to the semantic model, since I don’t use it. I see Rich typing, and he might have a solution for you.

Do you actually move your temperature sensors once a day?

No, obviously not - I do it so seldom that there is a very good chance that I would forget about updating the group in the meantime…

Depends on your definition of “simple”. The simplest by far would in fact be what @rpwong proposes. Create a Group for the room and add the temperature readings as members of the Group.

Depends on the language involved. In JS Scripting there is a descendants property on Group Items which gets all the direct members and the members of subgroups. Figuring out which Items provide a temperature should be simple. If you are using the semantic model, all your temperature Items will have “Temperature”. Ideally thermometers would have both that and “Measurement” tags. So just filter the descendants down to just those that have those two tags.

var tempItems = items['RoomItem'].descendants.filter(item => item.tags.includes('Measurement') && item.tags.includes('Temperature'));
var avg = tempItems.map(item => item.numericState).reduce((sum, a) => sum + a, 0) / tempItems.length;

Then your only choice is to add each Item as a trigger to the rule or add them to a Group and use a member of group trigger on the rule.

At this point, the semantic model is only useful to build the Overview Page, HABot, and for some simple operations from rules (e.g. get the Group Item for the Location or Equipment that a Point Item belongs to). Nothing else uses the semantic model yet.

Theoretically but I’m not certain off hand the best way to do it. That’s probably way overkill unless for some reason you are constantly moving these devices.

To move the Item to another location will require you to change it’s Group membership. You’ll see both Groups right there when you go to do that. That should be plenty to jog your memory. It’s not like you need to remember to go somewhere else to update it too. You’ll see that the Item is a member of both Groups. By the name of the Groups it should be obvious even to future you that you need to change them both to the new location.

Thanks a lot for the suggestions :+1:

Is there an overview somewhere about what part of the semantic model is available in rules, including the syntax to get them?

In the meantime, I actually found a couple of posts about dynamically updating groups, but you (and Russ) probably have a point about this being an overkill, so I will accept your answer as an solution.

For Rules DSL: Actions | openHAB

For other languages see the docs for that add-on.