Templating in Automation's Trigger IDs

Why something like this doesn’t work?

trigger: state
entity_id:
  - schedule.christmas_lights_schedule
id: "{{ states('schedule.christmas_lights_schedule') }}"

The result is "{{ states('schedule.christmas_lights_schedule') }}" while I’m expeting on or off.

Can’t see anything in the docs about using templates in trigger IDs - I suspect they’re just strings.

1 Like

Strings only.

@alex3025

You can get the entity_id that triggered from a template, without needing to add a trigger id

trigger.to_state.entity_id

Oh, sad moment…

So should I use {{ states('trigger.to_state.entity_id') }} to get the state of the schedule?

you have 2 ways,

trigger.to_state.state

or

states(trigger.to_state.entity_id)

So I could do something like this? (as automation condition)

condition: template
value_template: "{{ trigger.to_state.state == 'on' }}"

sure, but why are you doing that? You can just explicitly say that in the trigger.

trigger: state
entity_id:
  - schedule.christmas_lights_schedule
to: 'on'

I want to use a single schedule trigger to trigger on and off statuses

triggers:
  - trigger: state
    entity_id:
      - schedule.christmas_lights_schedule
conditions: []
actions:
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: template
                value_template: "{{ trigger.to_state.state == 'on' }}"
        sequence:
          - type: turn_on
            device_id: 610aa9a1e720ddf89d6a4d3c11341317
            entity_id: d99fcc55ff51d75e397bc330299eeefc
            domain: switch
            enabled: true
mode: restart

what’s the off going to look like?

alias: example 
description: Turn christmas lights on and off as per schedule 
triggers:
  - trigger: state
    entity_id:
      - schedule.christmas_lights_schedule
conditions: []
actions:
  - action: "switch.turn_{{ trigger.to_state.state }}"
    target:
      entity_id: switch.your_switch 
mode: single
1 Like