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 tools ➜ templates:
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
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.