Remaining time until next leave to work

Hi,
I am having some issues with calculating in my template sensor time from now until next leave to work time.
I would like my template to check if now already past today’s leaving time and if so, add one day. However if after adding one day we get to Saturday or Sunday then respectively I need to add 172800 and 86400 seconds.
Secondly, during my first attempt I did have some issues with a timezone offset. I couldn’t fix it so after a while I decided to start from 0 again. Because currently my template doesn’t generate any output I also wanted to ask if my logic is correct and timezone will not be an issue on this occasion?

  - platform: template
    sensors:
      next_leave_to_work:
        friendly_name: "Next leave to work"
        value_template: >
          {%- set time = '06:00' -%}
          {%- set next_leave = as_timestamp(states.sensor.date.state + ' ' + time) -%}
          {%- set now = as_timestamp(now()) -%}
          {%- if now | int >= next_leave | int -%}
              {%- set next_leave = next_leave | int + 86400 -%}
          {%- else -%}
              {%- set next_leave = next_leave | int -%}
          {%- endif -%}
          {%- set weekday = next_leave | timestamp_custom('%w') | int -%} 
          {%- if weekday == 6 -%}
            {%- set next_leave = next_leave | int + 172800 -%}
          {%- endif -%}
          {%- if weekday == 7 -%}
            {%- set next_leave = next_leave | int + 86400 -%}
          {%- endif -%}
          {{ next_leave | timestamp_custom('%Y-%m-%d %H:%M') }}

Thanks in advance.

1 Like

For the most part it looks ok, although it could be simplified a bit. The main issue, though, is this will only update when sensor.date updates, which is once a day at midnight. You need to enable sensor.time and then specify it as the value of the entity_id parameter.

  - platform: template
    sensors:
      next_leave_to_work:
        friendly_name: "Next leave to work"
        entity_id: sensor.time
        value_template: >
          {% set next_leave = as_timestamp(states.sensor.date.state ~ ' 06:00')|int %}
          {% set now = as_timestamp(now())|int %}
          {% if now >= next_leave %}
              {% set next_leave = next_leave + 86400 %}
          {% endif %}
          {% set weekday = next_leave | timestamp_custom('%w') | int %} 
          {% if weekday == 6 %}
            {% set next_leave = next_leave + 172800 %}
          {% elif weekday == 7 %}
            {% set next_leave = next_leave + 86400 %}
          {% endif %}
          {{ next_leave | timestamp_custom('%Y-%m-%d %H:%M') }}
2 Likes

This thread is great!

I’ve been struggling trying to figure out how to manipulate time values and both posts are amazing examples to help me work through how the conversions work!

It’s surprisingly hard to try to find this stuff out by Googling. I’ve picked up bits and pieces along the way but this really helped bring a lot of it all together.

I’ve now written up a pretty long cheat sheet for myself explaining to myself how it works. I’m sure it’s not complete but it’s a huge step from where I was before this thread.

Thanks to both of you.

:+1:

Thanks a lot @pnbruckner for the solution, I also got some assistance on the discord channel but as @finity stated, there isn’t a lot of materials in regards to the time manipulation in HA hence I am finding it very helpful as it follows my thinking.

Many Thanks again!