Single Automation for turning lights on/off based on state change

Hi,

I’m trying to clean up my automations since there have been a lot of changes lately to the features in automations.

I want to combine some on/off automations into a single one.
I’m using zigbee2mqtt for my switches.
Zigbee2mqtt will set the _action entitiy for example for an Ikea switch to on/off based on which button was pressed and immediately set it back to empty.
This is enough to trigger an automation.
Currently I have 2, one for turning the lights on and one for turning the lights off based on the button press.

The problem is when I try to combine on/off into a single automation and use the “choose” feature, this seems to not work reliably, (only 3 out of 4 times).

I’m assuming this is because the states are not cached, when I use them inside the choose section, and _action is already been set back to empty?

Is there a way to determine which trigger caused the automation to activate inside the choose section?

alias: new on-off
description: ''
trigger:
  - entity_id: sensor.ikea_switch_action
    platform: state
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.ikea_switch_action
            state: 'on'
        sequence:
          - entity_id: 'light.stehlampe1'
            service: light.turn_on
          - service: mqtt.publish
            data:
              topic: zigbee2mqtt/tv_lampen/set
              payload: '{"state": "on"}'
      - conditions:
          - condition: state
            entity_id: sensor.ikea_switch_action
            state: 'off'
        sequence:
          - entity_id: 'light.stehlampe1'
            service: light.turn_off
          - service: mqtt.publish
            data:
              topic: zigbee2mqtt/tv_lampen/set
              payload: '{"state": "off"}'
    default: []
mode: single

Try this:

      - conditions:
          - condition: "{{ trigger.to_state.state == 'on' }}"

Likewise for off.

1 Like

Thank you, that was exactly what I was searching for in the documentation but unable to find.

I also needed to explicitly create both trigger, otherwise a trigger also occurred when the state was set back to empty by zigbee2mqtt.

The final working automation

alias: new on-off
description: ''
trigger:
  - entity_id: sensor.ikea_switch_action
    platform: state
    to: 'on'
  - platform: state
    entity_id: sensor.ikea_switch_action
    to: 'off'
condition: []
action:
  - choose:
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.state == ''on'' }}'
        sequence:
          - entity_id: 'light.stehlampe1'
            service: light.turn_on
          - service: mqtt.publish
            data:
              topic: zigbee2mqtt/tv_lampen/set
              payload: '{"state": "on"}'
      - conditions:
          - condition: template
            value_template: '{{ trigger.to_state.state == ''off'' }}'
        sequence:
          - entity_id: 'light.stehlampe1'
            service: light.turn_off
          - service: mqtt.publish
            data:
              topic: zigbee2mqtt/tv_lampen/set
              payload: '{"state": "off"}'
    default: []
mode: single
2 Likes

It’s in the docs, just hard to find unless you know what you are looking for:

Ah interesting, thanks