# \[SOLVED\] OpenHAB3 / Turn off all lights / How to use the semantic model to (easily) turn off all the lights?

**URL:** <https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734>\
**Category:** Beginners\
**Tags:** lights, openhab3, semantic-model\
**Created:** [January 3, 2021, 4:50pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734 "2021-01-03T16:50:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ringbuchblock](https://community.openhab.org/user_avatar/community.openhab.org/ringbuchblock/32/64212_2.png) [@ringbuchblock](https://community.openhab.org/u/ringbuchblock)\
**Post date:** [January 3, 2021, 4:50pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/1 "2021-01-03T16:50:17Z")

</div>

Hi,

I am new to OpenHAB and started to work with version 3.

I struggle with a few things and unfortunately I am a bit confused on which instructions still work for OH3 and which don’t. I’ll post separate questions for each of my problems.

This question is about **Semantic Model Usage**.

### Problem / Goal:

The documentation mentions that with the semantic model it should now easily be possible to “turn off all the lights” in whatever location.

> **[Semantic Model](https://www.openhab.org/docs/tutorial/model.html)**
>
> a vendor and technology agnostic open source automation software for your home

Unfortunately, I could not find out how this can be achieved exactly.

What I would expect:  
Have a button “Turn Off All Lights” or something similar with which I could simply trigger that all items of semantic type “light” are turned off.

Posts I read so far:

- [https://community.openhab.org/t/solved-turn-all-lights-on-off/](https://community.openhab.org/t/solved-turn-all-lights-on-off/)
- [https://community.openhab.org/t/toggling-all-lights-on-off-with-a-switch-and-rules/](https://community.openhab.org/t/toggling-all-lights-on-off-with-a-switch-and-rules/)
- [https://community.openhab.org/t/oh3-semantic-model-setup-via-tags-in-configuration-items-files/](https://community.openhab.org/t/oh3-semantic-model-setup-via-tags-in-configuration-items-files/)

* * *

Platform information:

- OS: ArchLinux
- openHAB version: 3.0.0-2

---

<div class="post-metadata">

**Author:** ![diddie17](https://community.openhab.org/user_avatar/community.openhab.org/diddie17/32/114904_2.png) [@diddie17](https://community.openhab.org/u/diddie17)\
**Post date:** [January 4, 2021, 1:54pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/2 "2021-01-04T13:54:22Z")

</div>

I’m only just making my way in OH3 myself, but this is all about groups.

If you structure the lighting into groups you can configure OH to turn off all members within a group when you turn off the group itself.

In terms of the button, there are multiple ways to do this, through sitemaps, the new UI (I haven’t got that far yet) or through Alexa or similar notifications.

I have two sets of lights in my office, down lights and wall lights. Both are members of the group “Office Lights”. When I tell OH (usually through Alexa) to turn on/off the office lights, both sets will turn on or off.

Thanks

---

<div class="post-metadata">

**Author:** ![ringbuchblock](https://community.openhab.org/user_avatar/community.openhab.org/ringbuchblock/32/64212_2.png) [@ringbuchblock](https://community.openhab.org/u/ringbuchblock)\
**Post date:** [January 4, 2021, 2:04pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/3 "2021-01-04T14:04:17Z")

</div>

Hi @diddie17,

thank you for your answer.

The thing is that I don’t want to introduce additional groups. I’d rather just use the semantic information which is already there.  
For example OH already knows what items are `lights` and those are properly shown e.g. under `Home >> Equipment >> Lights`.

 ![example-equipment-lights](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/a/d/adbb0a2c0849e384ff86f433a5b38335e179430a.png)

I just want to re-use that information and work with it.

I did some further digging.  
In the [demo environment](https://demo.openhab.org/#!/settings/rules/demo-3) I found a rule which seem to work with light equipment. Unfortunately, it is not working for me either. 😔 Not sure why though.

```auto
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |
        Lights?.members.forEach(light|
        	postUpdate(light, OFF)
        )

```

---

<div class="post-metadata">

**Author:** ![hafniumzinc](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@hafniumzinc](https://community.openhab.org/u/hafniumzinc)\
**Post date:** [January 4, 2021, 2:06pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/4 "2021-01-04T14:06:09Z")

</div>

> [@ringbuchblock](#):
>
> `postUpdate(light, OFF)`

What happens if you try with `sendCommand(light, OFF)` instead?

---

<div class="post-metadata">

**Author:** ![ringbuchblock](https://community.openhab.org/user_avatar/community.openhab.org/ringbuchblock/32/64212_2.png) [@ringbuchblock](https://community.openhab.org/u/ringbuchblock)\
**Post date:** [January 4, 2021, 2:12pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/5 "2021-01-04T14:12:18Z")

</div>

> [@hafniumzinc](#):
>
> What happens if you try with `sendCommand(light, OFF)` instead?

Yes, I already tried that as well. Sorry, forgot to mention that.

It generally doesn’t seem to work.  
I introduced a new dummy switch item `all_lights_off` and once the state toggles to `ON` it should trigger the rule.  
In order to check if anything happens at all I tried to update the `all_lights_off` state to `OFF` again but that also doesn’t work.

```auto
triggers:
  - id: "0"
    configuration:
      itemName: all_lights_off
      state: ON
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: script
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |
        postUpdate(all_lights_off, OFF)
        Lights?.members.forEach(light|
        	//postUpdate(light, OFF)
            sendCommand(light, OFF)
        )
    type: script.ScriptAction

```

* * *

edit:  
It works if I remove the lights thingy.

```auto
triggers:
  - id: "0"
    configuration:
      itemName: all_lights_off
      state: ON
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: script
    configuration:
      type: application/vnd.openhab.dsl.rule
      script: |
        postUpdate(all_lights_off, OFF)
    type: script.ScriptAction

```

I assume that the

```auto
        Lights?.members.forEach(light|
            postUpdate(light, OFF)
        )

```

simply doesn’t work for whatever reason.

---

<div class="post-metadata">

**Author:** ![ringbuchblock](https://community.openhab.org/user_avatar/community.openhab.org/ringbuchblock/32/64212_2.png) [@ringbuchblock](https://community.openhab.org/u/ringbuchblock)\
**Post date:** [January 4, 2021, 3:10pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/6 "2021-01-04T15:10:27Z")

</div>

> [@diddie17](#):
>
> but this is all about groups

I am afraid you are right. 😔

In the demo there is indeed [a group which is called `Lights`](https://demo.openhab.org/#!/settings/items/Lights/edit)…

 ![demo_group-lights](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/b/e/be58557f2d097a01bb85a479b407797b495a3638.png)  
… and then you can probably work with `<group-name>?.members.forEach` loops.

This is very sad. I had hoped that I could just use the semantic information which is already there.

---

<div class="post-metadata">

**Author:** ![diddie17](https://community.openhab.org/user_avatar/community.openhab.org/diddie17/32/114904_2.png) [@diddie17](https://community.openhab.org/u/diddie17)\
**Post date:** [January 4, 2021, 4:28pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/7 "2021-01-04T16:28:29Z")

</div>

You shouldn’t need to work through the group members, the power of the groups means that you can just turn on or off the group with a command like -

```auto
events.postUpdate("Lights", "OFF");

```

Having groups mans that you can have an “OfficeLights” group inside a “DownstairsLights” group inside an “AllLights” group and choose which one of those you turn on or off, just by turning on or off the group. No need to work through the members.

You can of course still turn off each of the individual lights by sending a command directly to it.

You can get a view of how I think the developers envisioned the model working through this tutorial - [Semantic Model](https://www.openhab.org/docs/tutorial/model.html)

---

<div class="post-metadata">

**Author:** ![hafniumzinc](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@hafniumzinc](https://community.openhab.org/u/hafniumzinc)\
**Post date:** [January 4, 2021, 4:32pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/8 "2021-01-04T16:32:37Z")

</div>

> [@diddie17](#):
>
> Having groups mans that you can have an “OfficeLights” group inside a “DownstairsLights” group inside an “AllLights” group

I think the point is that these groups would be **in addition** to those that you would have in your Semantic Model, because these groups themselves wouldn’t usually live within the Semantic Model.

So extra groups have to be created over and above the ones in the Semantic Model to control all the lights, which is what the OP is trying to avoid.

---

<div class="post-metadata">

**Author:** ![hafniumzinc](https://community.openhab.org/letter_avatar_proxy/v4/letter/h/b9bd4f/32.png) [@hafniumzinc](https://community.openhab.org/u/hafniumzinc)\
**Post date:** [January 4, 2021, 9:25pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/9 "2021-01-04T21:25:28Z")

</div>

Maybe this is useful?

> [@Run rules for semantic groups in OH3](https://community.openhab.org/t/run-rules-for-semantic-groups-in-oh3/112755/2):
>
> You can get all items from such a semantic tag. This is a part of a daily JS rule which checks for battery levels: var allBatteries = itemRegistry.getItemsByTag("Battery"); for (var i in allBatteries) { var state = allBatteries[i].getState(); ... Br, Stefan

Basically, you can grab all Items with a specific tag…

---

<div class="post-metadata">

**Author:** ![ringbuchblock](https://community.openhab.org/user_avatar/community.openhab.org/ringbuchblock/32/64212_2.png) [@ringbuchblock](https://community.openhab.org/u/ringbuchblock)\
**Post date:** [January 5, 2021, 2:47pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/10 "2021-01-05T14:47:06Z")

</div>

> [@hafniumzinc](#):
>
> Maybe this is useful?
> 
> > [@](#):
> >
> > You can get all items from such a semantic tag. This is a part of a daily JS rule which checks for battery levels: var allBatteries = itemRegistry.getItemsByTag(“Battery”); for (var i in allBatteries) { var state = allBatteries[i].getState(); … Br, Stefan

Yes, indeed!  
This was very helpful. 👍

I now ended up with the following ECMA rule:

```auto
triggers: []
conditions: []
actions:
  - inputs: {}
    id: "1"
    configuration:
      type: application/javascript
      script: |
        var items = itemRegistry.getItemsByTag("Light");
        for (var i in items) {
          var light = items[i];
         
          var state = light.getState();
          if (light.type == "Color") {
            // We need to remap HSB value to ON / OFF
            state = state.brightness > 0 ? "ON" : "OFF";
          }

          if (state == "ON") {
            events.sendCommand(light.getName(), OFF);
          }
        }
    type: script.ScriptAction

```

Unfortunately, I couldn’t figure out yet how to translate this into a DSL rule.

---

<div class="post-metadata">

**Author:** ![ringbuchblock](https://community.openhab.org/user_avatar/community.openhab.org/ringbuchblock/32/64212_2.png) [@ringbuchblock](https://community.openhab.org/u/ringbuchblock)\
**Post date:** [January 5, 2021, 4:37pm UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/11 "2021-01-05T16:37:09Z")

</div>

An alternative approach could be to run a rule which automatically assigns all the items to the corresponding dummy groups and stick with the additional groups.

```auto
# 'gRoot' is representing the root group which contains all items

# Iterate over all items of the root group including those of sub groups
gRoot?.allMembers.forEach[item |    

  # Verify whether item is tagged as light                                     
  if (item.tags.contains("Light")) {
    # Add to "lights" group
    gLights.addMember(item)
  }
]

```

That way one could still work with `gLigths.sendCommand(OFF)` / `gLigths.sendCommand(ON)` .

---

<div class="post-metadata">

**Author:** ![trumee](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@trumee](https://community.openhab.org/u/trumee)\
**Post date:** [May 26, 2021, 5:47am UTC](https://community.openhab.org/t/solved-openhab3-turn-off-all-lights-how-to-use-the-semantic-model-to-easily-turn-off-all-the-lights/112734/12 "2021-05-26T05:47:54Z")

</div>

I tried to run your ECMA rule but hit an error,

```auto

2021-05-26 05:44:43.827 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'b0b9f4f087' failed: <eval>:4:10 Expected ; but found :
  - inputs: {}
          ^ in <eval> at line number 4 at column number 10

```

Any idea what is wrong?
