teadragon7
(Teadragon7)
September 13, 2024, 12:14am
1
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
tom_l
September 13, 2024, 12:16am
2
Try this:
{{ states('sensor.jewish_calendar_upcoming_candle_lighting_3')|as_datetime|as_local > today_at('19:00:00') }}
teadragon7
(Teadragon7)
September 13, 2024, 12:29am
3
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
tom_l
September 13, 2024, 1:03am
4
teadragon7:
> '7:00'
Keep in mind that is 7am, not 7pm (19:00).
1 Like
mekaneck
(Rick Auch)
September 13, 2024, 4:53am
5
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
teadragon7
(Teadragon7)
September 15, 2024, 11:48pm
7
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!
mekaneck
(Rick Auch)
September 16, 2024, 1:04pm
8
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.