How to add if for one light in action

  action:
  - service: homeassistant.turn_on
    entity_id: >
      - light.kitchen_glass
      - light.kitchen
      - switch.socket1
      {% if states('sun.sun') = 'below_horizon' %}
      - light.fibaro_system_fgd212_dimmer_2_level_4
      {% endif %}

please, help!
how fix it?
I got error:
Invalid config for [automation]: not a valid value for dictionary value @ data['action'][0]['entity_id']. Got None. (See /config/configuration.yaml, line 327). Please check the docs at https://home-assistant.io/integrations/automation/

You don’t need a template. This is where you would use a condition in the action block.

  action:
  - service: homeassistant.turn_on
    entity_id: 
      - light.kitchen_glass
      - light.kitchen
      - switch.socket1
  - condition: state  # from sunset until sunrise
    entity_id: sun.sun
    state: 'below_horizon'
  - service: homeassistant.turn_on
    entity_id: light.fibaro_system_fgd212_dimmer_2_level_4

Though if you really want to use a template you could try this:

  action:
  - service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% if states('sun.sun') == 'above_horizon' %}
          - light.kitchen_glass
          - light.kitchen
          - switch.socket1
        {% else %}
          - light.kitchen_glass
          - light.kitchen
          - switch.socket1
          - light.fibaro_system_fgd212_dimmer_2_level_4
        {% endif %}

Note the use of “data_template” and “==” not “=”.

== is a test (does x == y)

= assigns a value ( set x = y)

Second one won’t work, you can’t output a list in jinja.

First one is the solution.

1 Like

I was wondering about that, which is why I said “try”.

Not even a comma separated list?

Comma separated string output works with some services and not others, best to avoid, but probably would work in this one to be fair.

1 Like

Ok so, @dmitry.mukhach to be clear, this template below might work, but it would still be best to use the first solution I provided that uses the condition in the action block.

  action:
  - service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% if states('sun.sun') == 'above_horizon' %}
          light.kitchen_glass, light.kitchen, switch.socket1
        {% else %}
          light.kitchen_glass, light.kitchen, switch.socket1, light.fibaro_system_fgd212_dimmer_2_level_4
        {% endif %}
1 Like

No, this not work :frowning:

  action:
  - service: homeassistant.turn_on
    data_template:
      entity_id: >
        {% if states('sun.sun') == 'above_horizon' %}
          light.kitchen_glass, light.kitchen, switch.socket1
        {% else %}
          light.kitchen_glass, light.kitchen, switch.socket1, light.fibaro_system_fgd212_dimmer_2_level_4
        {% endif %}

It was validated but wen run - nothing turn on.

So use the condition.

Thanks a lot! With condition works :wink:

1 Like