I’m working with a template where I output a time calculated as sunset minus / plus a specified offset.
Firstly the below gives me “21:17:32”
{% set offset = "00:30:00" %}
{% set offset = as_timestamp(strptime(offset, "%H:%M:%S")) %}
{% set sunset = as_timestamp(states.sun.sun.attributes.next_setting) %}
{{ (sunset) | timestamp_custom('%H:%M:%S') }}
Next, if I take off the offset, I get “19:47:32”
{% set offset = "00:30:00" %}
{% set offset = as_timestamp(strptime(offset, "%H:%M:%S")) %}
{% set sunset = as_timestamp(states.sun.sun.attributes.next_setting) %}
{{ (sunset - offset) | timestamp_custom('%H:%M:%S') }}
Finally, if I add the offset, I get “21:47:32”
{% set offset = "00:30:00" %}
{% set offset = as_timestamp(strptime(offset, "%H:%M:%S")) %}
{% set sunset = as_timestamp(states.sun.sun.attributes.next_setting) %}
{{ (sunset + offset) | timestamp_custom('%H:%M:%S') }}
For me, if the sunset is “21:17:32” I would expect (sunset + offset) to be “21:47:32” which is it … but I would expect (sunset - offset) to be “20:47:32”, not “19:47:32” … it seems to be an hour off.
I’m sure I’m doing something silly, but if anyone can help, I’d really appreciate it.
set myoffset = as_timestamp(strptime(myoffset, "%H:%M:%S"))
it returns 00:30:00 with a date of 1st jan 1901…
you don’t want to subtract that date, you just want to subtract 30 min.
Best is to convert the 30 min into seconds, not a datetime object.
Change it into:
{% set offset = offset.split(":")[0] | int * 3600 + offset.split(":")[1] | int * 60 + offset.split(":")[2] | int %}
and then you get the correct results:
{% set offset = "00:30:00" %}
{% set offset = offset.split(":")[0] | int * 3600 + offset.split(":")[1] | int * 60 + offset.split(":")[2] | int %}
{% set sunset = as_timestamp(states.sun.sun.attributes.next_setting) %}
{{ (sunset - offset) | timestamp_custom('%H:%M:%S', true) }}
Is there a requirement to represent offset in HH:MM:SS format?
If no such requirement exists, then the template is simplified if you specify offset as an integer value. Either define it directly in seconds or, if you wish to maintain a reference to its value in minutes, multiply minutes by 60.
Thanks @123 I was thinking just that. The only reason I went with my template is that @jamespreedy might have an input_datetime date_input to capture the the offset time from the user…