Why my time based binary sensor is an hour off?

Hello, I have created a binary sensor which should indicate when my power provider offers cheaper kWh.
My sensor is configured as follows

    eco_kwh:
      friendly_name: "Eco kWh"
      value_template: >
        {% if (now().month >= 5 and now().month < 11) and (now().hour >= 23 or now().hour <= 7) %}
          on
        {% elif (now().month >= 11 or now().month <= 4 ) and ((now().hour >= 2 and now().hour <= 8) or (now().hour >= 15 and now().hour <= 17)) %}
          on
        {% else %}
          off
        {% endif %}

The result of the above is a bit weird. The sensor turns on at the correct time but turns off one hour later that it should.

Any ideas? Am I missing something obvious?

You have ‘on’ defined as between 02:00:00 and 08:59:59, and between 15:00:00 and 17:59:59. Is that what you wanted?

1 Like

Thanks so much! You’ve said just the right words that helped me understand my error. My requirement is for the sensor to be on from 02:00 to 08:00 as well as 15:00 to 17:00. This translates to

now().hour >= 2 and now().hour <= 7
now().hour >= 15 and now().hour <= 16

as I wasn’t considering the minutes

1 Like