Do something BEFORE a specified time

Hello,
is there a possibility to configure an atuomation to do something before a specified time?
I have simple timer, It works at specified time but I want to power on heater 15 minutes before that specified time.
I suppose it need to do something with sensor alarm_time and substract 15 from minutes. But how to do that if minutes = for example 5?
I will appreciate any help.
Regards

Here is my code:

  - platform: template
    sensors:
      alarm_time:
        friendly_name: 'Czas'
        value_template: '{{ "%0.02d:%0.02d" | format(states("input_number.grzejniki_turbo_hour") | int, states("input_number.grzejniki_turbo_minute") | int) }}'

and

input_number: 
  grzejniki_turbo_hour:
    name: Godzina
    icon: mdi:timer
    min: 0
    max: 23
    step: 1
  grzejniki_turbo_minute:
    name: Minuta
    icon: mdi:timer
    min: 0
    max: 59
    step: 1

and

- alias: "Heater Scheduled Task"
  trigger:
    - platform: time
      minutes: '/1'
      seconds: 0
  condition:
    - condition: state
      entity_id: input_boolean.grzejnik_wlacznik
      state: 'on'
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: template
      value_template: '{{ now().strftime("%H:%M") == states.sensor.alarm_time.state }}'
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.grzejnik_salon_turbo

I do this using the following value_template:

value_template: '{{ ((now().strftime("%s") | int + 900) | timestamp_custom("%H:%M:%S")) == states.input_datetime.alarm.state }}'

You need to convert the current time to seconds, add in your number of minutes as seconds and then check to see if it is equal to your alarm.

So yours should be something like:

{{ ((now().strftime("%s") | int + 900) | timestamp_custom("%H:%M")) == states.sensor.alarm_time.state}}

Thanks for reply. The concept is right, but when I pasted code I have this error:

Error doing job: Task exception was never retrieved
Traceback (most recent call last):
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\components\automation_init_.py”, line 330, in async_trigger
if skip_condition or self.cond_func(variables):
File "C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\components\automation_init
.py", line 436, in if_action
return all(check(hass, variables) for check in checks)
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\components\automation_init_.py”, line 436, in
return all(check(hass, variables) for check in checks)
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\helpers\condition.py”, line 316, in template_if
return async_template(hass, value_template, variables)
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\helpers\condition.py”, line 298, in async_template
value = value_template.async_render(variables)
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\helpers\template.py”, line 132, in async_render
return self._compiled.render(kwargs).strip()
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\asyncsupport.py”, line 76, in render
return original_render(self, *args, **kwargs)
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py”, line 1008, in render
return self.environment.handle_exception(exc_info, True)
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\environment.py”, line 780, in handle_exception
reraise(exc_type, exc_value, tb)
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2_compat.py”, line 37, in reraise
raise value.with_traceback(tb)
File “”, line 1, in top-level template code
File “C:\Users\Latitude\AppData\Local\Programs\Python\Python36\lib\site-packages\jinja2\sandbox.py”, line 427, in call
return __context.call(__obj, *args, **kwargs)
ValueError: Invalid format string

try this:

- alias: "Heater Scheduled Task"
  trigger:
    - platform: time
      minutes: '/1'
      seconds: 0
  condition:
    - condition: state
      entity_id: input_boolean.grzejnik_wlacznik
      state: 'on'
    - condition: time
      weekday:
        - mon
        - tue
        - wed
        - thu
        - fri
    - condition: template
      value_template: >
        {% set current_time = now().hour * 60 + now().minute %}
        {% set alarm_hour, alarm_minute = states.sensor.alarm_time.state.split(':') %}
        {% set alarm_time = alarm_hour | int * 60 + alarm_minute | int %}
        {{ current_time == alarm_time - 15 }}
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.grzejnik_salon_turbo



Do a trigger template !

Example, I use this to turn on the bathroom heater X minutes before the wake up time.

First you’ll need a sensor to update the current hour (calling now doesn’t work as a trigger template):

  - platform: time_date
    display_options:
      - 'time'

Then I have one input_datetime in which the wake up time is stored (you can set this one as you like, interface input or some rules to find it out) :

  alarm_clock:
    name: Heure de réveil
    has_date: false
    has_time: true

And an input_number that let me set how many minutes before wake up hour I want the bathroom heater to start :

anticipation_chauffage_sdb:
  min: 5
  max: 45
  step: 1
  name: "Anticipation Chauffage SdB"
  mode: slider
  unit_of_measurement: "mn"
  icon: mdi:alarm-check

Then the automation that triggers X mn before the wake up hour. Conditions added to turn this on only if there are people home and it’s cold outside :

- alias: 'A_ Radiateur SdB'
  trigger:
    - platform: template
      value_template: "{{ (((states.input_datetime.alarm_clock.attributes['timestamp']) - (states.input_number.anticipation_chauffage_sdb.state | int * 60)) | timestamp_custom('%H:%M', False) ) == ( states.sensor.time.state ) }} " 
  condition:
    - condition: numeric_state
      entity_id: sensor.m_temperature
      below: '18'
    - condition : state
      entity_id: binary_sensor.somebody_home
      state: 'on'
  action:
    - service: switch.turn_on
      entity_id: switch.chauffage_sdb

I also have some to turn on the boiler for global heating before wake up hour, to progressively turn on lamps on wake up too…

1 Like

Thank you very much! It works :slight_smile: