Can you use templates in an automation action condition?

The docs state you can use templates in the trigger condition and within actions, but I get the following error with the below automation (p.s. I’m not sure what line 19 refers to… it’s not related to automations and if I comment it out I still get the error).

Invalid config for [automation]: Entity ID {{ trigger.entity_id }} is an invalid entity ID for dictionary value @ data[‘action’][0][‘choose’][0][‘conditions’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 19).

- id: '1619905823458'
  alias: Lamp Colour
  description: ''
  trigger:
  - platform: state
    entity_id: light.booze_lamp,light.tv_lamp,light.sofa_lamp,light.intray_lamp,light.nook_lamp
    from: 'off'
    to: 'on'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: "{{ is_state('trigger.entity_id','on') }}"
      sequence:
      - service: light.turn_off
        target:
          entity_id: "{{ trigger.entity_id }}"
    - conditions:
      - condition: "{{ is_state('trigger.entity_id','off') }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: "{{ trigger.entity_id }}"
        data:
          transition: 2
          color_temp: "{{ states('input_number.lamps_colour') | int }}"
          brightness_pct: "{{ states('input_number.lamps_brightness') | int }}"
    default: []

Is the example you posted complete or reduced for posting here? The reason why I ask is because the second conditions in choose will never evaluate to true.

The automation is triggered only when one of the lights changes state to on. Yet the second conditions checks for the triggered entity’s state to be off. Given the State Trigger it has, that’ll never happen.

You would have to remove this constraint:

    from: 'off'
    to: 'on'

to permit the second conditions to ever be met.

Try this version:

- id: '1619905823458'
  alias: Lamp Colour
  description: ''
  trigger:
  - platform: state
    entity_id: light.booze_lamp,light.tv_lamp,light.sofa_lamp,light.intray_lamp,light.nook_lamp
  condition: []
  action:
  - variables:
      e: "{{ trigger.to_state.entity_id }}"
      s: "{{ trigger.to_state.state }}"
  - choose:
    - conditions: "{{ s == 'on') }}"
      sequence:
      - service: light.turn_off
        target:
          entity_id: "{{ e }}"
    - conditions: "{{ s == 'off') }}"
      sequence:
      - service: light.turn_on
        target:
          entity_id: "{{ e }}"
        data:
          transition: 2
          color_temp: "{{ states('input_number.lamps_colour') | int }}"
          brightness_pct: "{{ states('input_number.lamps_brightness') | int }}"
    default: []
2 Likes

Ah! Of course… I must have confused what I was trying to do. I’ve been staring at automations all day :slight_smile:

Thanks for your code, I’ve learnt something there. It looks like I had got my condition/conditions wrong

1 Like