Trying to figure out why my Automation only partly works

Hey all,
I’ve been staring at this automation for the last week or so in my free time, and I can’t for the life of me figure out what I did wrong. I’m certainly no expert, but I thought I understood how things worked. I’m betting it is something stupid but I can’t see it for looking at it. I was just wondering if anyone can see an obvious mistake that I’ve done.

The Intent of this is to turn on my out door Christmas lights at 5pm and turn them off at 11pm. At the moment it seems that turning them off at 11pm seems to work but turning them on doesn’t.

alias: Control Christmas Outdoor Lights
triggers:
  - event: start
    trigger: homeassistant
  - value_template: "{{ states('sensor.time') == '17:00' }}"
    trigger: template
  - value_template: "{{ states('sensor.time') == '23:00' }}"
    trigger: template
actions:
  - service_template: |
      {% if '17:00' <= states('sensor.time') > '23:00' %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    entity_id:
      - switch.tp_link_smart_plug_cbb6_porch
      - switch.tp_link_smart_plug_cbb6_lawn

Thanks to anyone in advance for any comments/suggestions.

Try < ‘23:00’ in the if statement instead of >

Is it a deal breaker for you if it goes off at midnight?
If not a simple-er way is change the 23:00 trigger to 00:00. Change the action if to look for a time > 17:00.
Then at midnight the time is less that 17:00 again and it turns off.

I suspect there is a problem with time formatting there, but I don’t use that enough to positively identify that for you.

It’s probably turning off OK because it only hits the else. (guessing)

You are comparing strings here, not times and the time should be less than 2300 not greater than.

A better way to do it:

alias: Control Christmas Outdoor Lights
triggers:
  - event: start
    trigger: homeassistant
  - trigger: time
    at: "17:00"
  - trigger: time
    at: "23:00"
actions:
  - action: >
      {% if 17 <= now().hour < 23 %}
        switch.turn_on
      {% else %}
        switch.turn_off
      {% endif %}
    target:
      entity_id:
        - switch.tp_link_smart_plug_cbb6_porch
        - switch.tp_link_smart_plug_cbb6_lawn

DOH, how did I miss that. Much better now… Thank you for the 2nd pair of eyes :slight_smile:

1 Like

Thanks to everyone ! Both answers worked.

I wasn’t aware that the compare was a string. I will make note of that for future automations !

at the end of the day the > vs < was what was causing the major issue.

Much appreciate the extra pairs of eyes to see what I couldn’t see staring me right in the face. !