Data_template and light/switch.turn_on not working

The rest of the automation is working fine, but these parts do not. Nothing is happening although the conditions are true.

And also, is there a way to clean up this cone?

  - service: switch.turn_on
    data_template: 
      entity_id: >
        {% set sekunder = now().hour * 3600 + now().minute* 60 + now().second %}
        {% if sekunder < 71100 %}
          switch.lampa_eriks_fonster_switch
          switch.lampa_eriks_jordglob_switch
        {% endif %}
  - service: light.turn_on
    entity_id: light.eriks_myshorna
    data_template: 
      color_temp: >
        {% set sekunder = now().hour * 3600 + now().minute* 60 + now().second %}
        {% if sekunder < 71100 %}
          447
        { else }       
          0
        {% endif %}            
      brightness: >
        {% if sekunder < 71100 %}
          84
        { else }       
          0              
        {% endif %}

think you’d need an {% else %} to cater for when sekonder is more than 71100 as your automation will fail then since you’ll try and switch on a light but won’t provide the entity id.
Or better still you need to use a service_template instead

1 Like

Thanks for the fast reply @lolouk44 .

You’re right about the {% else %} statement, but in this case the conditions are true. Still noting is executed.

Your suggestion about using service: service_template looks interesting and might be a better solution. I’ll look into that. Thanks!

Have you tried puting the two switches in the same line, separated by a comma?

      switch.lampa_eriks_fonster_switch, switch.lampa_eriks_jordglob_switch
1 Like

Think the issue is you have 2 items to turn on, you need to list them with a - or you need to write 2 separate actions. The latter will be easier to ensure you have no indentation issues created by your if statements

I have made some progress

This is working, i.e. if the light is on, then dim it, otherwise turn of the light (keep the light turned of):

  - service: homeassistant.turn_on
    entity_id: light.taklampa_ovre_trapphall_level
    data_template:
      brightness: >
        {% if is_state('light.taklampa_ovre_trapphall_level' , "on") %} 13
        {% else %} 0
        {% endif %}

I also tried to use service_template as in the code below and it is also working as expected:

  - service_template: >-
      {%- set sekunder = now().hour * 3600 + now().minute* 60 + now().second -%} 
        {%- if sekunder < 77400 -%}
          homeassistant.turn_on
        {%- endif -%}
    entity_id: switch.lampa_ilias_sang_switch , switch.lampa_annas_sang_switch  

The only problem that’s remaining is when I try to turn off several items in the same automation. So the code below is now working as expected. I have tried with - in front of each device without success.

  - service: homeassistant.turn_off
    data_template: 
      entity_id: >-
        {% if 1 == 1 %} #condition is true
          - light.light1
          - light.light2
          - light.light3
          - light.light4
          - light.light5
          - media_player.media1
        {% endif %}

In some of my automations I have several items with kind of long names, so putting everything coma separated in a single row would make my code very hard to read.

Any suggestions concerning that last issue would be very welcome!