I have a working tasker-to-hass alarm clock working. I send “alarm_hour” and “alarm_minute” och have a template sensor set up. When the alarm clock in my phone goes off a group of lights is turned on.
Now I’m trying to make a wakeuplight that starts a transition before my android alarm clock goes off. I’m trying to make a template sensor that subtracts a “offset time”. But I’m having some trouble with subtracting time. I have searched the forum but havent figured out how to make this work.
- platform: template
sensors:
# System time
sys_time:
friendly_name: 'System Time'
value_template: '{{ (now().strftime("%s") | int | timestamp_custom("%H:%M")) }}'
unit_of_measurement: 'time'
# Alarm time from my android phone, sent from tasker
alarm_time:
friendly_name: 'Alarm Time'
value_template: '{{ "%0.02d:%0.02d" | format(states("sensor.alarmhour") | int, states("sensor.alarmminute") | int ) }}'
# Time to offset wakeup light
wakeuplight_offset_time:
friendly_name: 'Offset time'
value_template: '{{ "%0.02d:%0.02d" | format(0 | int, 20 | int) }}'
# Calculate when to start wakeuplight. I use this time in an automation to trigger a wakeup script
wakeuplight_start_time:
friendly_name: 'Wakeup script start time'
value_template: '{{ strptime(states.sensor.alarm_time.state, "%H:%M") - strptime(states.sensor.wakeuplight_offset_time.state, "%H:%M") }}'
I want to solve a similar problem, so my question does also fit in here as it also has to do with maths and time … I want to turn on lights for a certain amount of time which should be calculated: (dawn - dusk):4.
The following example is working in the HASS GUI (/dev-template):
ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: expected int for dictionary value @ data[‘action’][1][‘delay’][‘minutes’]. Got None.
when using single or double quotes around the statement.
Using no quotes results in a valid config, but the log says:
Thanks. I was already aware of that, but I want to somehow calculate the time when the switch should be turned off.
See the formula above … is there a way to use a template for ‘offset’?
Ah. I see. I guess the only way I can think of off the top of my head would be to calculate the “off” time in the same automation that turns on the light. Then save that value in a slider and use a template trigger that uses the slider value as reference.
The Alarm Clock thread has some examples of extracting trigger times from sliders. Wouldn’t match exactly what you’re doing, but should be enough inspiration.