Time templating

I have an automation like this:

> automation:
>     - id: alarm_1_weekdays
>       hide_entity: true
>       alias: "Sveglia (giorni lavorativi)"
>       trigger:
>           - platform: time
>             minutes: '/1'
>             seconds: 0
>       condition:
>           - condition: state
>             entity_id: input_boolean.alarm_1_weekday
>             state: 'on'
>           - condition: time
>             weekday:
>                 - mon
>                 - tue
>                 - wed
>                 - thu
>                 - fri
>           - condition: template
>             value_template: >-
>                 {{ now().strftime("%H:%M") == states.sensor.alarm_1_time.state }}

This row:
{{ now().strftime("%H:%M") == states.sensor.alarm_1_time.state }}
trigger the automation when the current time is equal to the one set in the states.sensor.alarm_1_time.state

How can I tell this automation to trigger with 10 minutes in advance over the states.sensor.alarm_1_time.state?

Thanks

it’s ugly and hacky and would be better served as a sensor (the time minus 10 minutes) to compare but this should work.

{{ now.strftime("%H:%M") == (((as_timestamp(states.sensor.alarm_1_time.state) - ((10 | int) * 60)) | timestamp_custom("%H:%M")) }}

thank you I will try it

it does now work
Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected ‘}’, expected ‘)’) for dictionary value @ data[‘condition’][2][‘value_template’]. Got None
not a valid value for dictionary value @ data[‘condition’][2][‘condition’]. Got None
required key not provided @ data[‘condition’][2][‘entity_id’]. Got None. (See /config/configuration.yaml, line 144). Please check the docs at https://home-assistant.io/components/automation/

sorry, it should have 1 less (

{{ now.strftime("%H:%M") == ((as_timestamp(states.sensor.alarm_1_time.state) - ((10 | int) * 60)) | timestamp_custom("%H:%M")) }}

should be correct

no need to ask sorry since you are helping me
thank you man

in any case I tried it but it does not work :slight_smile:

weird. You could just set a sensor that does the time calculation and compare it to that for your automation.

I do something similar in the mornings for turning lights on before sunrise. Its similar to this in that it takes sunrise time and subtracts a set amount of time from a slider and then the automation fires when it hits that time.

take a look at this thread.