If statement in entity template automation

Why does this throw an error?

- id: lichten_woonkamer_aan
  trigger:
    platform: state
    entity_id: binary_sensor.nc_motion_woonkamer_01_sensor,binary_sensor.nc_motion_woonkamer_02_sensor
    to: 'on'
  condition:
    condition: numeric_state
    entity_id: sensor.nc_motion_woonkamer_02_luminance
    below: 35
  action:
    service: light.turn_on
    entity_id: >-
      {% if is_state('input_boolean.movie', 'on') %}
        light.eettafel
      {% else %}
        light.eettafel,light.tv
      {% endif %}

Invalid config for [automation]: not a valid value for dictionary value @ data[‘action’][0][‘entity_id’] Got none.

I need the entity id’s to be variable. This works fine in data_templates. The template itself it valid, I checked in the developer tools.

You should use this format, which employs target, when entity_id contains a template:

  action:
    service: light.turn_on
    target:
      entity_id: "your template goes here"

For example:

  action:
    service: light.turn_on
    target:
      entity_id: "light.eettafel{{', light.tv' if is_state('input_boolean.movie', 'off') else ''}}"

Thank you very much! It’s working like a charm now.

  action:
    service: light.turn_on
    target:
      entity_id: >-
        {% if is_state('input_boolean.movie', 'on') %}
          light.eettafel
        {% else %}
          light.eettafel,light.tv
        {% endif %}

I think it would have also worked by putting the entity_id line under the data option:

    service: light.turn_on
    data:
      entity_id: >-
        {% if is_state('input_boolean.movie', 'on') %}
          light.eettafel
        {% else %}
          light.eettafel,light.tv
        {% endif %}

Finity, Taras, given your comments: I’ve been running HA for a bit over 2 years now, but this is one thing that has always confused me: when to use target and when to use data. In some cases it’s interchangeable, in others it’s the one or the other. I tend to copy from existing working snippets, but intuitively I’ve never quite understood the reasoning. Perhaps you know more.

I empathize with you and anyone else who may be confused by it. It’s largely due to the fact that the format has evolved over time and the old ways were never discontinued in order to maintain backward compatibility (i.e avoid a Breaking Change). In addition, some integrations have always supported data but don’t support target.

Generally speaking, the new way is to use target to specify which entity is being affected (using entity_id, area_id and/or device_id) and data to specify how it will be affected.

For example, this demonstrates how to gradually turn on two lights to 50%.

- service: light.turn_on
  target:
    entity_id:
      - light.kitchen
      - light.bathroom
  data:
    brightness: 125
    transition: 15
1 Like

Here’s the history:

first there was

service: ....
entity_id: ... no template ...

But then someone wanted to template the entity_id. And remember at this point you could only template inside data and you had to change data to the name data_template…

service: ....
entity_id: ... no template ...
service: ....
data:
  entity_id: ... no template ...

or

service: ....
data_template:
  entity_id: ... template ...

Then the month of WTH happend and people were like "Why do I have to specify data_template, so we are down to just:

service: ....
entity_id: ... no template ...
service: ....
data:
  entity_id: ... template or no template ...

Then blueprints were added. These required a way to swap out entity_id for devices or areas. So target was invented. Target accepts area, device_id, and entity_id. Now we have…

service: ....
entity_id: ... no template ...
service: ....
data:
  entity_id: ... template or no template ...
service: ....
target:
  entity_id: ... template or no template ...

EDIT: For what it’s worth, target will always work for all service calls. You should always just use this method from here on out.

6 Likes

@123 @petro thank you both for 2 really great answers. This is going into my bookmarks. :beers:

1 Like