Can state condition accept "entity_id" as variable?

Hi,

I have a blueprint that checks the state of two entities like so:

- condition: state
  entity_id:
    - !input primary_lights
    - !input secondary_lights
  state: "off"

This works with no issues.
If I combine the two inputs into a variable and then use the variable in the condition I get an error of “entity ID is not valid”

variables:
  primary_light_entities: !input primary_lights
  secondary_light_entities: !input secondary_lights
  all_light_entities:
    - "{{ primary_light_entities }}"
    - "{{ secondary_light_entities }}"

- condition: state
  entity_id: "{{ all_light_entities }}"
  state: "off"

I know that the all_light_entities variable is correct because I can use it in a service call with no problems e.g.

    sequence:
    - alias: "Turn off all lights"
      service: light.turn_off
      target:
        entity_id: "{{ all_light_entities }}"

Can anyone shed some light on this?
Are variables not allowed in the entity_id field for a state condition? If so, why? Is this a bug?

Thanks.

you’re providing 2 lists to all_light_entities. You need to merge or add the lists together.

  all_light_entities: "{{ primary_light_entities + secondary_light_entities }}"

if you don’t add the lists together, you’re essentially feeding the following yaml:

  entity_id:
  - 
    - x.y
    - x.z
  -
    - a.b
    - a.c

which is why it’s not working.

Thanks @petro
I’ve made the change and I have checked that I am definitely passing a list of entity ids to the entity_id in the condition, but I am always getting the error:

Entity ID {{ all_light_entities }} is an invalid entity ID for dictionary value @ data['action'][0]['choose'][0]['conditions'][1]['entity_id']. Got None

I have even tried setting a variable to a single entity_id and I get the same error e.g.

variables:
  primary_light_entity: light.kitchen_ceiling

action:
- choose:
  - alias: "Single button press"
    conditions:
      - condition: template
        value_template: "{{ trigger.platform == 'event' and trigger.event.data.command == 'single' }}"
      - condition: state
        entity_id: "{{ primary_light_entity }}"
        state: "off"

and I get

Entity ID {{ primary_light_entity }} is an invalid entity ID for dictionary value @ data['action'][0]['choose'][0]['conditions'][1]['entity_id']. Got None

but if I remove the variable and put the entity_id string there it works e.g.

action:
- choose:
  - alias: "Single button press"
    conditions:
      - condition: template
        value_template: "{{ trigger.platform == 'event' and trigger.event.data.command == 'single' }}"
      - condition: state
        entity_id: light.kitchen_ceiling
        state: "off"

It is as though the condition does not accept a variable for the list of entities.

You can’t use a template in the entity_id field for a state condition.

Thanks. When you say it out loud like that it all makes sense :thinking:
I guess some early success with blueprints made me start ignoring the rules :joy:

Hi @petro
I came across this in the 2021.3 release notes: 2021.3: My Oh My - Home Assistant

It suggests that variables can be used in triggers from 2021.3 onwards. Do you know if this is only for MQTT triggers? The release notes make it sound like variables can be used in all triggers.

I am trying to provide a list of entity_ids to my state trigger, where the id’s are added as part of the blueprint input, but I just can’t seem to find a way of doing something simple like this:

trigger:
  - platform: state
    entity_id: "{{ light_entities }}"

Where light_entities is list of entities either as a comma ‘,’ separated string or as a list.

I’m finding this whole area very confusing and the documentation is so vague :frowning_face:

I’m not sure what’s vague about it? It’s available exactly how it’s written in what you posted. Put the light_entities list inside the trigger_variables section.

I’m not sure why’d you even bother doing that though, you’re still maintaining a list, so just put the list in the trigger.

I’m not an expert on HA like you and am a relative newbie, so still trying to understand its various features and quirks. The documentation often relies on you being an advanced user to understand it. That’s all I mean.

Regarding the post. I am getting the following error when reloading the automtations and it has me stumped:

2021-06-04 13:32:11 ERROR (MainThread) [homeassistant.components.automation] Blueprint Test generated invalid automation with inputs OrderedDict([('light_target', OrderedDict([('entity_id', ['light.dining_ceiling', 'light.family_ceiling', 'light.hall_ceiling'])]))]): Entity ID {{ light_entities }} is an invalid entity ID for dictionary value @ data['entity_id']. Got None

Are you able to shed any light on what I am doing wrong.

The full blueprint I am using to try an understand the problem is below. Notice the commented out attempts at using different ways to achieve the result I want. Only the entity_id: "light.hall_ceiling, light.dining_ceiling" works for me, but I can’t use this as it is not using the blueprint input.

blueprint:
  name: Test
  description: |
    Put description here
  domain: automation
  input:
    light_target:
      name: Lights
      description: The light(s) to control
      selector:
        target:
          entity:
            domain: light
#
# The automation
#
variables:
  lights: !input light_target
  light_entities: "{{ lights.entity_id }}"
  # light_entities: "{{ lights.entity_id|join(',') }}"

# Trigger mode
mode: single

# Triggers
trigger:
  - platform: state
    entity_id: input_boolean.test
  - platform: state
    # entity_id: "light.hall_ceiling, light.dining_ceiling"
    entity_id: "{{ light_entities }}"

# Conditions
condition:
  - "{{ trigger.entity_id in lights.entity_id }}"

# Actions
action:
  - service: notify.mobile_app_kaboom
    data:
      message: test passed

To put a little more context around the above.
My aim is to listen to HomeKit events and catch state changes to a limited list of lights so that I can turn off occupancy sensors when Siri is asked to turn the light on.

I didn’t want to trigger on every single HomeKit event, so was hoping to be able to set a trigger only for the lights using occupancy sensors.

Petro’s post, the Solution post, explains you cannot use a template for a State Condition’s entity_id. However, the most recent blueprint you posted still contains a templated entity_id option for a State Trigger (equally disallowed):

trigger:
  - platform: state
    entity_id: input_boolean.test
  - platform: state
    # entity_id: "light.hall_ceiling, light.dining_ceiling"
    entity_id: "{{ light_entities }}" 
  1. You’re using variables section not trigger_variables.
  2. You don’t need to even use that. Just use the !input light_target as the entity_id
  - platform: state
    entity_id: !input light_target

It’s allowed with trigger_variables and the limited scope of trigger templates. I have no idea if it can be used in blueprints but it exists.

@petro Interesting. I started with what you suggested:

  - platform: state
    entity_id: !input light_target

but I get the error:

2021-06-04 14:56:12 ERROR (MainThread) [homeassistant.components.automation] Blueprint Test generated invalid automation with inputs OrderedDict([('light_target', OrderedDict([('entity_id', ['light.dining_ceiling', 'light.family_ceiling', 'light.hall_ceiling', 'light.landing_ceiling'])]))]): Entity ID entity_id is an invalid entity ID for dictionary value @ data['entity_id']. Got None

It only works if I have one entity in the light_target
That’s what started me trying out other methods and led me to the 2021.3 release notes post.

With this:

- alias: trigger variables test 1
  trigger_variables:
    lights: ['light.hallway_light', 'light.bedroom_lamp', 'light.foyer_light']
  trigger:
  - platform: state
    entity_id: '{{ lights }}'
  action:
  - service: notify.persistent_notification
    data:
      title: Trigger variable test
      message: "{{ trigger.to_state.name }} is {{ trigger.to_state.state }}"

Running Check Configuration generates this:

Logger: homeassistant.config
Source: config.py:443
First occurred: 10:00:28 (1 occurrences)
Last logged: 10:00:28

Invalid config for [automation]: Entity ID {{ lights }} is an invalid entity ID for dictionary value @ data['entity_id']. Got None. (See /config/configuration.yaml, line 9).

I get the same result even if I reduce the trigger_variable to just this:

  trigger_variables:
    lights: 'light.hallway_light'

It’s unhappy seeing a templated entity_id in a State Trigger.

(Tested with version 2021.6.0)

That’s what I mean about the documentation. For example, as a newbie it is very difficult to find clear information on variables & trigger_variables and where they can/cannot be used. Or even why they are different.
Don’t get me wrong I still think HA is the best solution out there, but it has a very steep learning curve and documentation that assumes you are already on top of the curve.

Looks like it’s only available for event and mqtt triggers as specified by the docs.

Yeah my understanding is that trigger_variables can be used only with options that already support templates (in this case, the so-called “limited templates”).

Not just new users. I’ve been at it for over 2 years and those two specific features were recently introduced and are very thinly documented.

I think you’re thinking to far into this. The docs explain exactly what they are used for, you’re just not used to the docs. trigger_variables are used for triggers that use trigger_variables. I’m not sure what more can be explained. I guess it’s missing “This can only be used with triggers”, however the individual triggers specify that they can use it. This is only listed in Event and MQTT triggers, therefore it can only be used for those triggers.

As for variables, those fields are described in the automation action docs and script docs.

There’s not much to it. It’s a single field, and referenced in both triggers that use it. If you think you can write that section better, by all means, go for it.