Comparing Time Stamp Sensor to Set Time

Hello!

I have the following sensor


which i am trying to use to create a Binary sensor that will return true if the sensor returns a time after 7pm. I have the following

{{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime > today_at('7:00:00') }}

which should currently be returning false but is returning true. I tried using ‘19:00:00’ instead and no luck. I think that I have to convert sensor.jewish_calendar_upcoming_candle_lighting_3 to local time but I am unsure how.

Any help would be appreciated

Try this:

{{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local > today_at('19:00:00') }}

Thank you for quick response!

Unfortunately that still returns true

I did some deconstructing

{{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local }}
{{today_at('19:00:00') }}

returns
2024-09-13 18:39:00-04:00
2024-09-12 19:00:00-04:00

the date doesn’t match up such is what is throwing things off

Edit:
writing it out helped me find the solution

{{ (states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local).strftime('%-I:%M') > '7:00'}}

once i removed the date it all worked out!

@tom_l thank you for pointing me in the right direction

Keep in mind that is 7am, not 7pm (19:00).

1 Like

And also note that you are doing string comparison so it’s not likely doing what you want it to do.

For example, the following will render false:

{{ '7:00' < '10:00' }}

String comparison could work if you ensure the same time format between the two values. This will render true like you’d expect:

{{ '07:00' < '10:00' }}
{{ (states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local).strftime('%H:%M') > '07:00'}}
…or compare time objects instead of strings…
{{ (states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local).time() > today_at('07:00:00').time() }

Or if you do want to keep the dates included in the comparison but just wanted to shift it one day, you can do this:

{{ states('sensor.jewish_calendar_upcoming_candle_lighting_3') | as_datetime > today_at('07:00:00') + timedelta(days=1) }}

Edit: changed strftime hour format from %I to %H to get the 24-hour format.

1 Like

Thanks for you help but your proposed solution assumes ‘7:00’ is AM but I need PM or ‘19:00’. When I use ‘19:00’ your templates don’t work as expected. Since my sensor will never dip into the AM my solution will work for my use case. I took your advice and I confirmed:

{{ (states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local).strftime('%-I:%M') }}

Returns ‘6:26’ and that:

{{ (states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local).strftime('%-I:%M') > '7:00'}}

Returns ‘false’ (as expected) and that:

{{ (states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local).strftime('%-I:%M') > '6:00'}}

Returns ‘True’ (as expected)

Thanks!

In the first template I suggested (which does string comparison and ignores dates) I mistakenly used the wrong hour formatting. I’ve edited my reply above with %H as the correct 24-hour formatting.

The other two templates (which use time objects and datetime objects respectively) will work just fine with 24-hour times.