DJI
(David Inwood)
October 13, 2023, 4:47pm
1
I think I’ve had this problem before, but I’ve got a template that seems to be getting the local time wrong. This is what I’ve got and right now it is outputting a time that is an hour longer than I expected.
{% if states('sensor.sun_solar_elevation')|float <= -6 %}
ILB in darkness conditions
{% elif as_timestamp(states('sensor.sun_next_dusk'),0) < as_timestamp(now(),0)+2*3600 %}
{{+ 'Dark within (h:m) ' ~ (as_timestamp(states('sensor.sun_next_dusk'),0) - as_timestamp(now(),0))|timestamp_custom("%H:%M",true)}}
{% else %}
{{"Light OK"}}
{% endif %}
123
(Taras)
October 13, 2023, 5:00pm
2
Change this:
timestamp_custom("%H:%M",true)
To this:
timestamp_custom("%H:%M",false)
1 Like
DJI
(David Inwood)
October 13, 2023, 5:02pm
3
Is it because I’m calculating the difference between 2 time zone aware entities, so the difference doesn’t need to use local time.
123
(Taras)
October 13, 2023, 5:09pm
4
If you make the suggested change, does the template produce the correct value?
Because it does for me.
DJI
(David Inwood)
October 13, 2023, 5:11pm
5
Yes it does, thank you. Just trying to understand hiw it works.
123
(Taras)
October 13, 2023, 5:18pm
6
The result of the subtraction is a timestamp representing a UTC date and time. The second option in timestamp_custom
determines how the supplied timestamp is interpreted (local or UTC).
DJI
(David Inwood)
October 13, 2023, 5:22pm
7
Thanks. I understand now.
Another option is to dump the timestamps and not try to shoehorn date and time into a time difference:
{%- set dusk = states('sensor.sun_next_dusk') | as_datetime %}
{%- if states('sensor.sun_solar_elevation') | float(0) <= -6 %}
ILB in darkness conditions
{%- elif dusk < now() + timedelta(hours=2) %}
{%- set int_min = ((dusk - now()).total_seconds() // 60) | int %}
Dark within (h:m) {{ int_min // 60 }}:{{ int_min % 60 }}
{%- else %}
Light OK
{%- endif %}
DJI
(David Inwood)
October 13, 2023, 5:33pm
9
Thanks, that is nice and clear.