Timestamp_local giving wrong time (after adding input_number to input_datetime)

Hi,

I currently set my hot water via 4 sets of on/off input_datetimes - works perfectly well, but after using I realise that it is ‘easer’ for me to set a start time and a duration (ilo of stop time).

I have therefore created 4 matching input_numbers and am trying to get the off time from them.

This is what I I have in developer toolstemplates:

datetime:              {{ states('input_datetime.hive_water_1_on') }}
input_numer:           {{ states('input_number.hive_water_1_time') }}

datetime as timestamp: {{ state_attr('input_datetime.hive_water_1_on','timestamp') }}
input_number as secs:  {{ (states('input_number.hive_water_1_time')|float(30) *3600) }}

adding - local=True:
{{
  ( state_attr('input_datetime.hive_water_1_on','timestamp')
  + states('input_number.hive_water_1_time')|float(30) *3600
  ) | timestamp_custom("%H:%M:%S", local=True)
}}
adding:
{{
  ( state_attr('input_datetime.hive_water_1_on','timestamp')
  + states('input_number.hive_water_1_time')|float(30) *3600
  ) | timestamp_custom("%H:%M:%S")
}}
adding - UTC:
{{
  ( state_attr('input_datetime.hive_water_1_on','timestamp')
  + states('input_number.hive_water_1_time')|float(30) *3600
  ) | timestamp_utc()
}}

…and it gives me this:

datetime:              07:30:00
input_numer:           0.5

datetime as timestamp: 27000
input_number as secs:  1800.0

adding - local=True:
09:00:00
adding:
09:00:00
adding - UTC:
1970-01-01T08:00:00+00:00

now, 07:30 + 00:30 should be 08:00 (which is what I am looking for).

My current timezone is UTC, but timestamp_local is giving me UTC+1. HA’s time is correct. Can someone please help me understand the error?

Thanks!

You’re using the old “unfriendly” way to deal with time in HA. Do yourself a favor and use datetime objects and timedelta objects via today_at, as_datetime, as_local, timedelta. Lastly use .strftime on the result to get the desired ending time. However I don’t necessarily recommend doing that because you can turn this into a timestamp sensor and then use that in automations.

datetime:              {{ states('input_datetime.hive_water_1_on') }}
input_numer:           {{ states('input_number.hive_water_1_time') }}

{% set time = states('input_datetime.hive_water_1_on') | today_at %}
{% set minutes = states('input_number.hive_water_1_time') | float %}
{{ (time + timedelta(minutes=minutes)).strftime('%H:%M:%S') }}

as a timestamp sensor

template:
- sensor:
  - name: Hive Water 1 Off
    unique_id: Hive Water 1 Off
    device_class: timestamp
    state: >
      {% set time = states('input_datetime.hive_water_1_on') | today_at %}
      {% set minutes = states('input_number.hive_water_1_time') | float %}
      {{ time + timedelta(minutes=minutes) }}

using the timestamp sensor as a trigger…

alias: Hive Water 1
trigger:
- id: 'on'
  platform: time
  at: input_datetime.hive_water_1_on
- id: 'off'
  platform: time
  at: sensor.hive_water_1_off
action:
  # Replace this service w/ whatever on/off service you're using.
- service: switch.turn_{{ trigger.id }}
  target:
    # Replace this with the correct entity
    entity_id: switch.hive_water_1

thanks, @petro - amazingly helpful as usual!

To be clear, which parts are the ‘old unfriendly’ way and how do I know it’s old from the docs? …I have been taking my logic from here:

thanks!

Well they aren’t old, they are just the original things in HA to handle time. That’s as_timestamp, timestamp_custom, and timestamp_local. They are all valid options and still work and they aren’t being removed. However, if you don’t fully understand utc and local timestamps, you will get tripped up when using them. This is because you need to handle the UTC/Local offset or understand how to overcome that by using the provided arguments.

If you use all the newer datetime oriented functions, then you don’t have to worry about that at all.

1 Like