Automation variables: Am I doing something wrong or are they not working as I expect them too

Hi!
I’m working on an automation. Some things are still missing, but as far as I have things in place, it’s working great.
Since it is still under development, I use helpers as standins for “the real things” while testing/developing, for example an input_boolean as a standin for a light or a presence sensor. This way I can easily test the behaviour without messy real world data :wink:

Since a few entities are used in different places (triggers, service calls, conditions) I’d love to keep the entities in a variable where I can replace them at a single location.

I saw this blogpost and it looked to me like my use case: 0.115: B-Day release! Media browser, tags, automations & WTH - Home Assistant

But it seems variables can`t be used for entitie ids… Is this right? Am I missing something? Am I doing something wrong?

Thanks!

This is a minimal example:
Working:

alias: My test
description: ""
trigger:
  - platform: state
    entity_id:
      - input_boolean.test_occupancy_room
condition: []
action:
  - service: input_boolean.toggle
    target:
      entity_id: input_boolean.test_light
    data: {}
mode: single

With variable, not working:

alias: My test
description: ""
variables:
  occupancy: input_boolean.test_occupancy_room
trigger:
  - platform: state
    entity_id:
      - "{{ occupancy }}"
condition: []
action:
  - service: input_boolean.toggle
    target:
      entity_id: input_boolean.test_light
    data: {}
mode: single

The error message is Message malformed: Entity {{ occupancy }} is neither a valid entity ID nor a valid UUID for dictionary value @ data['entity_id']

That blog post is over 4 years old… use current documentation for current information.

The documents contain many examples. In most cases, if there isn’t an example of what you are trying to do in the component or integration docs, it is not currently possible.

Variables set up under the script-level variables key are not available to the trigger. For that you need to use Trigger Variables.

Templates cannot be used everywhere. In regard to triggers, they are only allowed in a few places:

  • The value_template variable of Template triggers.
  • The value_template variable of Numeric State triggers.
  • Event triggers in the event_type and event_data variables.
  • The for variable of State, Numeric State and Device triggers.

The closest functional automation to your example would be:

alias: My test
description: ""
trigger_variables:
  occupancy: input_boolean.test_occupancy_room
trigger:
  - platform: event
    event_type: state_changed
    event_data:
      entity_id: "{{ occupancy }}"
condition: []
action:
  - service: input_boolean.toggle
    target:
      entity_id: input_boolean.test_light
    data: {}
mode: single

As explained in the docs linked above, Trigger Variables only accept Limited Templates, so variable values that require more sophisticated templating may not be possible.

Thank you for the information!