Code not working as expected

Hello. Can someone check my code? Right know, light.k_1 comes on any part of the day.

What I want to happen is to have light.k_1 to come on between 10am - 4pm. Any other time will be light.k_1 & light.k_5.

- alias: motion kitchen lights on
  trigger:
    platform: state
    entity_id: binary_sensor.motion_kitchen_1_occupancy
    to: 'on'
  action:
    service: light.turn_on
    data_template:
      entity_id: >
        {% if now().hour > 16 %}
         light.k_1, light.k_5
        {% elif now().hour < 10 %}
         light.k_1, light.k_5
        {% else %}
         light.k_1
        {% endif %}

doh. Nevermind. It is working as it should. Light.k_1, Light.k_5 come on after 5pm and at 4.

1 Like

hey just to let you know, you can use or to combine your if elif statement into 1.

        {% if now().hour > 16 or now().hour < 10 %}
         light.k_1, light.k_5
        {% else %}
         light.k_1
        {% endif %}

shrinks up that template a bit.

You could also write it opposite for an easier read:

        {% if 10 <= now().hour <= 16 %}
         light.k_1
        {% else %}
         light.k_1, light.k_5
        {% endif %}
1 Like