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.