Odd Template Output

Hiya, thanks in advance for your help.

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.

Thanks!

I think the issue is this:

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) }}

20:46:35 for me here in the UK…

Ahhhhhhhhhhhhh, that makes perfect sense.
Thank you so much! :pray:t2:

Screenshot 2020-06-12 at 2.27.59 pm

:tada:

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.

          {% set offset = 30 * 60 %}
          {% set sunset = as_timestamp(states.sun.sun.attributes.next_setting) %}
          {{ (sunset - offset) | timestamp_custom('%H:%M:%S') }}

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…

If there is a requirement to maintain the HH:MM:SS format then I would suggest either of the following two conversion techniques.

The first method is nearly identical to yours except split is only called once as opposed to three times.

          {% set offset = '00:30:00'.split(':') %}
          {% set offset = offset[0] | int * 3600 + offset[1] | int * 60 + offset[2] | int %}
          {{ offset }}

The second method simply slices the string:

          {% set offset = '00:30:00' %}
          {% set offset = offset[:2] | int * 3600 + offset[3:5] | int * 60 + offset[6:] | int %}
          {{ offset }}

Anyway, this is just icing on the cake. You identified the root-cause and that was the mistaken conversion of offset into a datetime object.

2 Likes

So many options - thank again both, I appreciate it. :grinning:

2 Likes