# Using room temperature in rules and triggers

**URL:** <https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708>\
**Category:** Scripts & Rules\
**Tags:** rules, javascript, temperature, semantic-model\
**Created:** [October 26, 2023, 5:53pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708 "2023-10-26T17:53:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkaut](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@mkaut](https://community.openhab.org/u/mkaut)\
**Post date:** [October 26, 2023, 5:53pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708/1 "2023-10-26T17:53:43Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rpwong](https://community.openhab.org/user_avatar/community.openhab.org/rpwong/32/36036_2.png) [@rpwong](https://community.openhab.org/u/rpwong)\
**Post date:** [October 26, 2023, 6:22pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708/2 "2023-10-26T18:22:22Z")

</div>

> [@mkaut](#):
>
> without manually creating a group of all temperature-providing items in the room?

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.

> [@mkaut](#):
>
> And what if I wanted to have a rule that triggers when the room temperature changes - can this be done?

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.

---

<div class="post-metadata">

**Author:** ![mkaut](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@mkaut](https://community.openhab.org/u/mkaut)\
**Post date:** [October 26, 2023, 6:44pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708/3 "2023-10-26T18:44:37Z")

</div>

> [@rpwong](#):
>
> > [@mkaut](#):
> >
> > without manually creating a group of all temperature-providing items in the room?
> 
> 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.

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?

---

<div class="post-metadata">

**Author:** ![rpwong](https://community.openhab.org/user_avatar/community.openhab.org/rpwong/32/36036_2.png) [@rpwong](https://community.openhab.org/u/rpwong)\
**Post date:** [October 26, 2023, 7:19pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708/4 "2023-10-26T19:19:22Z")

</div>

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.

> [@mkaut](#):
>
> 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?

Do you actually move your temperature sensors once a day?

---

<div class="post-metadata">

**Author:** ![mkaut](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@mkaut](https://community.openhab.org/u/mkaut)\
**Post date:** [October 26, 2023, 7:24pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708/5 "2023-10-26T19:24:29Z")

</div>

> [@rpwong](#):
>
> 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…

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 26, 2023, 7:37pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708/6 "2023-10-26T19:37:44Z")

</div>

> [@mkaut](#):
>
> 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?

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.

> [@mkaut](#):
>
> 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…

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.

```auto
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;

```

> [@mkaut](#):
>
> And what if I wanted to have a rule that triggers when the room temperature changes - can this be done?

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.

> [@mkaut](#):
>
> Mostly, it feels wrong to have to specify this manually, when the information is there in the semantic model already

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.

> [@mkaut](#):
>
> 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

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.

> [@mkaut](#):
>
> 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…

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.

---

<div class="post-metadata">

**Author:** ![mkaut](https://community.openhab.org/letter_avatar_proxy/v4/letter/m/2bfe46/32.png) [@mkaut](https://community.openhab.org/u/mkaut)\
**Post date:** [October 26, 2023, 7:55pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708/7 "2023-10-26T19:55:12Z")

</div>

Thanks a lot for the suggestions 👍

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.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 26, 2023, 8:43pm UTC](https://community.openhab.org/t/using-room-temperature-in-rules-and-triggers/150708/8 "2023-10-26T20:43:19Z")

</div>

> [@mkaut](#):
>
> Is there an overview somewhere about what part of the semantic model is available in rules, including the syntax to get them?

For Rules DSL: [Actions | openHAB](https://www.openhab.org/docs/configuration/actions.html#semantics)

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