Automation - list of states in single automation?

I’m fairly new to text-configurable part of HA. So far I’ve been mainly doing things through GUI, but now I’d like to take a step onwards.

I’ve sucessfully implemented a way for HA to recognize my Webex call state and I’ve created automations which turn on or off a Hue light, based on if I’m in a call or not.

The only thing is, that Webex has three “active” and two “non-active” states that apply to me. So, based on what I know how to do now, I created a set of automations, each with another combination of states.

There is probably a very simple way to combine all those states together, right? :slight_smile:

Below an example of my current automation. In there if Webex state changes from “active” to “call” the light turns on. Other possible pre-light state is “idle”. And other possible post-light states are “meeting” and “presenting”.


- id: '1616580730730'
  alias: Webex Status - busy
  description: Turn on hallway light, depending on Webex status
  trigger:
  - platform: state
    entity_id: sensor.webex_status
    from: active
    to: call
  condition: []
  action:
  - type: turn_on
    device_id: 51561d5881cd11ebbac78f65d5043d11
    entity_id: light.hallway_3
    domain: light
    brightness_pct: 100
  mode: restart

I’ve got 6 automations configured currently. How could I get them down to two? :grinning:

I think that you can make all this into one by using the choose type action. First make the trigger any change of state of the webex_status. Please see the sample below. Its not complete but I hope you get the idea. If anything, please do ask.

trigger:
  - platform: state
    entity_id: sensor.webex_status
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.webex_status
            state: active
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hallway_3
      - conditions:
          - condition: state
            entity_id: sensor.webex_status
            state: call
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hallway_3
      - conditions:
          - condition: state
            entity_id: sensor.webex_status
            state: idle
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.hallway_3
    default: []
1 Like

This looks like what I’d like to do, yes.
I’ll test it out later and see if it works.

Thanks a lot.

Cheers,
Vlado

1 Like

Your original automation used a State Trigger that monitored a specific state-change, namely:

    from: active
    to: call

If the from state of the state-change is important, then you should be aware that the State Trigger in sheminasalam’s suggestion isn’t constrained that way and you’ll need to check it in choose's conditions (using trigger.from_state.state and trigger.to_state.state).

If from state is not important then the suggested automation should work well for you.

OK, I replaced my existing automations with your example and added missing states.

Unfortunately now I receive an error stating this:

Logger: homeassistant.components.homeassistant
Source: components/homeassistant/__init__.py:122
Integration: Home Assistant Core Integration (documentation, issues)
First occurred: 6:10:19 PM (4 occurrences)
Last logged: 6:16:16 PM

Error loading /config/configuration.yaml: while parsing a block collection in "/config/automations.yaml", line 1, column 1 expected <block end>, but found '?' in "/config/automations.yaml", line 58, column 1

Line 58 is where the “condition: [ ]” code is.
Since the line seems irrelevant I tried to simply remove it, but the error actually repeats, again on line 58, which is now, of course next line in the code.

I’m probably doing something really stupid here :slight_smile:

please share the full automation yaml. It could be some syntax issues.

- id: '1615458114375'
  alias: Close bedroom blind when light is on
  description: At night, close the blind in the bedroom, when the light is turned
    on.
  trigger:
  - platform: device
    type: turned_on
    device_id: 5154065e81cd11eba8db436ac4799c7a
    entity_id: light.spalnica_1
    domain: light
  - platform: device
    type: turned_on
    device_id: 5154408981cd11eb866a35e039698f86
    entity_id: light.spalnica_2
    domain: light
  condition:
  - condition: sun
    after: sunset
  action:
  - device_id: 1971787c7d0f11eb9e4b1778a078c4e5
    domain: cover
    entity_id: cover.bedroom_blind
    type: set_position
    position: 0
  mode: restart
- id: '1615458456724'
  alias: Open bedroom blind when light is off
  description: At night, open the blind in the bedroom, when the light is turned off.
  trigger:
  - platform: device
    type: turned_off
    device_id: 5154065e81cd11eba8db436ac4799c7a
    entity_id: light.spalnica_1
    domain: light
  - platform: device
    type: turned_off
    device_id: 5154408981cd11eb866a35e039698f86
    entity_id: light.spalnica_2
    domain: light
  condition:
  - condition: sun
    after: sunset
  action:
  - device_id: 1971787c7d0f11eb9e4b1778a078c4e5
    domain: cover
    entity_id: cover.bedroom_blind
    type: set_position
    position: 100
  mode: restart


- id: '1616494858208'
  alias: Webex Status Vlado
  description: Turn hallway light on or off, depending on Vlado's Webex status
  trigger:
  - platform: state
    entity_id: sensor.vlado_webex_status
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: active
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hodnik_3_kabinet
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: call
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hodnik_3_kabinet
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: presenting
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hodnik_3_kabinet
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: idle
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.hodnik_3_kabinet
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: active
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.hodnik_3_kabinet
    default: []

Please copy this to automation.yaml and try. there were syntax issues, hopefully it have been rectified.

- id: '1615458114375'
  alias: Close bedroom blind when light is on
  description: At night, close the blind in the bedroom, when the light is turned
    on.
  trigger:
  - platform: device
    type: turned_on
    device_id: 5154065e81cd11eba8db436ac4799c7a
    entity_id: light.spalnica_1
    domain: light
  - platform: device
    type: turned_on
    device_id: 5154408981cd11eb866a35e039698f86
    entity_id: light.spalnica_2
    domain: light
  condition:
  - condition: sun
    after: sunset
  action:
  - device_id: 1971787c7d0f11eb9e4b1778a078c4e5
    domain: cover
    entity_id: cover.bedroom_blind
    type: set_position
    position: 0
  mode: restart
- id: '1615458456724'
  alias: Open bedroom blind when light is off
  description: At night, open the blind in the bedroom, when the light is turned off.
  trigger:
  - platform: device
    type: turned_off
    device_id: 5154065e81cd11eba8db436ac4799c7a
    entity_id: light.spalnica_1
    domain: light
  - platform: device
    type: turned_off
    device_id: 5154408981cd11eb866a35e039698f86
    entity_id: light.spalnica_2
    domain: light
  condition:
  - condition: sun
    after: sunset
  action:
  - device_id: 1971787c7d0f11eb9e4b1778a078c4e5
    domain: cover
    entity_id: cover.bedroom_blind
    type: set_position
    position: 100
  mode: restart


- id: '1616494858208'
  alias: Webex Status Vlado
  description: Turn hallway light on or off, depending on Vlado's Webex status
  trigger:
  - platform: state
    entity_id: sensor.vlado_webex_status
  condition: []
  action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: active
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hodnik_3_kabinet
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: call
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hodnik_3_kabinet
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: presenting
        sequence:
          - service: light.turn_on
            data: {}
            target:
              entity_id: light.hodnik_3_kabinet
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: idle
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.hodnik_3_kabinet
      - conditions:
          - condition: state
            entity_id: sensor.vlado_webex_status
            state: active
        sequence:
          - service: light.turn_off
            target:
              entity_id: light.hodnik_3_kabinet
    default: []
1 Like

OK, that did the trick. It took me extra time, to see why the light turned on immediately, while I was not on a call. :slight_smile:
Once I fixed that, everything works, like it’s supposed to.

I see that indents were all that was wrong. I have to check and see if I can learn how to position them properly on my own.

Thanks a lot, kind sir. I’m in your debt. And I think you put me on my way to optimize the first two automations into one as well :wink:

1 Like

The choose in your automation contains two identical conditions (one at the beginning and the other at the end of the choose):

          - condition: state
            entity_id: sensor.vlado_webex_status
            state: active

The second instance will never be evaluated because the first one will always match the active value.

2 Likes

Yes, thanks. That was the problem that I was referring to in previous reply, when the light came on immediately :slight_smile:
Status “active” in WebEx actually means that you’re active at your PC but not on call or in meeting. So that’s actually when the light should be off. I changed this and now it’s working exactly how it’s supposed to.

If the light should be turned on only when the sensor’s state is call or presenting, and off for any other state (idle or active) then it can be done like this:

- id: '1616494858208'
  alias: Webex Status Vlado
  description: Turn hallway light on or off, depending on Vlado's Webex status
  trigger:
  - platform: state
    entity_id: sensor.vlado_webex_status
  condition: []
  action:
  - service: "light.turn_{{ 'on' if trigger.to_state.state in ['call', 'presenting'] else 'off' }}"
    entity_id: light.hodnik_3_kabinet

OK, this is even denser than the previous code.
I’ll try this one out as well, thanks.