Subtract time

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") }}'

//Emil

Take a look at:

You’ll want to subtract then they’re in timestamp format.

Check this topic and my setup: https://community.home-assistant.io/t/creating-a-alarm-clock/410/188?u=bob_nl

In this topic you’ll also find my complete setup which does exactly what you want (start wakeup alarm 20 mins before alarm time).

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 :slight_smile: … 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):

{{((((as_timestamp(states.sun.sun.attributes.next_dawn)) -  (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int }}

and it returns 116.

Those 116 minutes should then be used in an automation:

- alias: bla
  trigger:
    platform: sun
    event: sunset
  action:
    - service: switch.turn_on
      entity_id: switch.switch1
    - delay:
        # minutes: {{((((as_timestamp(states.sun.sun.attributes.next_dawn)) -  (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int }}
        minutes: 120
    - service: switch.turn_off
      entity_id: switch.swtich1

The hardcoded part is working, but not when using the #'ed one.
Thouhgts?

I don’t believe that delay accepts a template. You probably get an error in your home-assistant.log

Sure … “Check config” from the GUI returns

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:

ERROR (Thread-1) [homeassistant.util.yaml] invalid key: “OrderedDict([(’((((as_timestamp(states.sun.sun.attributes.next_dawn)) - (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int’, None)])”
_ in “/home/homeassistant/.homeassistant/tm_automation.yaml”, line 61, column 0_
ERROR (MainThread) [homeassistant.bootstrap] Error loading /home/homeassistant/.homeassistant/configuration.yaml: invalid key: “OrderedDict([(’((((as_timestamp(states.sun.sun.attributes.next_dawn)) - (as_timestamp(states.sun.sun.attributes.next_dusk))) / 60 ) / 4) | int’, None)])”
_ in “/home/homeassistant/.homeassistant/tm_automation.yaml”, line 61, column 0_

Right - you can’t use a template for the value of “delay”. It expects an integer.

Open a feature request if you’re inclined to.

Seems like this would be easier (and more robust) if you used two triggers - one for on, one for off.

I say “robust” because I don’t believe a delayed action will survive a reboot.

if you used two triggers - one for on, one for off.

I am still trying to find out a way how to use a “variable” for that time then.

It looks like you’re triggering based on sunrise/sunset conditions. You can use an offset in conjunction with that. In a trigger.

Hour and a half before sunset

  - platform: sun
      event: sunset
      offset: "-01:30:00"

Two hours after sunrise

  - platform: sun
      event: sunrise
      offset: "2:00:00"

etc

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.