Automation trigger "15 min before specified time"

I hope I do this

{{ state_attr('input_datetime.alarm_time', 'timestamp') - 900 }}

but how to create trigger for this value?

Both my constructions for comparing not worked

current is: {{ as_timestamp(states('sensor.time')) }}

test: {{ as_timestamp(now().time()) }}

Here is what I use to start heater Xmn before alarm set. (so it’s warm when we have to get up).
There is one input_datetime and one input_number used, the alarm_clock, and the anticipation time, defined like this.
Input datetime :

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

Input number :

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

The automation is a template trigger :

  trigger:
    - platform: template
      value_template: "{{ (((states.input_datetime.alarm_clock.attributes['timestamp']) - (states.input_number.heat_before.state | int * 60)) | timestamp_custom('%H:%M', False) ) == ( states.sensor.time.state ) }} " 

1 Like

can you please help me with this one, i feel there is a tiny mistake here or there

basically i want the action to be triggered 10 mins before the sensor
now trying with one sensor once it works i will apply it to others


alias: Prayer Times
trigger:
  - platform: time
    at:
      - sensor.fajr_prayer
      - sensor.dhuhr_prayer
      - sensor.asr_prayer
      - sensor.maghrib_prayer
      - sensor.isha_prayer


  - platform: template
      - value_template: "{{ (((states.sensor.fajr_prayer.attributes['timestamp']) - (states.sensor.fajr_prayer.state | int * 10)) | timestamp_custom('%H:%M', False) ) == ( states.sensor.time.state ) }} " 
action:
  - service: media_player.volume_mute
    data:
      is_volume_muted: true
    target:
      entity_id: media_player.group_snapcast_client
  - delay:
      hours: 0
      minutes: 10
      seconds: 0
      milliseconds: 0
  - service: media_player.volume_mute
    data:
      is_volume_muted: false
    target:
      entity_id: media_player.group_snapcast_client
trigger:
  - platform: template
    value_template: "{{ (states.sensor.fajr_prayer.attributes.timestamp - 600) | timestamp_custom('%H:%M', False) ) == states.sensor.time.state }} " 

Hi guys I also have a stupid question. I have a robotic lawnmower which mows on certain days. I set the whole thing in the app and via the Landroid Cloud I get the day and time written as a value in a data point.

Now I want to pick up this time stamp but I want to perform another action 30 seconds beforehand and that is to raise the garage doors of my robotic lawnmower garage.

My idea was to create a helper where I set the seconds, how much earlier he should raise the shutters and then another helper then updates the new time stamp. But maybe I’m thinking too complicated and it’s much easier. Ultimately, I just need a trigger (i.e. an automation) which triggers, for example, 30 seconds beforehand and then raises the roller shutters.

What is the best way to do this? If possible, please explain in a beginner-friendly way. Thank you very much :slight_smile:

The Landroid entity for the next start looks like this:

And the second problem is that the time is displayed 2 hours back, even though I have set the correct time zone for Germany. So the planned start is 11:30 but the time displayed is 9:30 as shown in the picture.

I would not write automation that does one thing then another that figures out how to do something before that… I’d write the automation to trigger the very first action. Then trigger the next… Based upon completion of the first if possible, but doing a delay for a set time if necessary. Easier usually to think progressively in time rather than backwards

Btw, if you have a new question, it’s generally best to start a new thread, even if it is somewhat related to a 2 year old thread…

1 Like

Yes, I agree with you. Only in my case, I set the mowing time for today at 18:00. As soon as this value is entered in the Worx Landroid app, the value also appears in HA. At that moment, it can already write the value to a new data point, which then has the time stamp 1 minute before, for example.

As soon as it is then 5:59 pm, another automation can start which then opens the garage doors, but unfortunately I don’t know how to do these two things in the smartest way.

Should I open a new topic now?

ah… if you do have an entity that has a time on it. then you can subtract from it…

the time would be something like
states('sensor.whatever_has_the_time') - timedelta(seconds=30)

a trigger template could be

{{ now() > states('sensor.whatever_has_the_time') - timedelta(seconds=30) }}

or something like that… i don’t know what entity of yours has that time, or the format, so i’m guessing a little.

don’t worry about opening a new topic now. for next time, please do so.

{{ now() > states(‘sensor.m700_next_scheduled_start’) - timedelta(seconds=30) }}

TypeError: unsupported operand type(s) for -: ‘str’ and ‘datetime.timedelta’

And unfortunately, the sensor shows me the time 2 hours back. This means it is supposed to mow at 9:30 a.m. the next time, but something seems to be wrong with the time conversion.

This works, but I would have to add 2 hours on top:

{{ states(“sensor.m700_next_scheduled_start”) | as_datetime - timedelta(minutes=states(“input_number.worx_garagentore_vorher_offnen”) | int) }}’

@armedad

I just tried it again as an automation as a template trigger but unfortunately it does not trigger. What am I doing wrong :frowning:

@armedad

What I don’t understand is that it shows the correct time here, but when I display it under templates, it shows a completely different time. is that normal?


Unfortunately, the trigger in the automation as a template does not work either :frowning:

trigger:
- platform: template
value_template: “{{ states(“sensor.m700_next_scheduled_start”) | as_datetime -
timedelta(minutes=states(“input_number.worx_garage_gates_pre_open”) | int)
}}'”

i think if you look at the original string, it’s actually the right time (but look at the timezone… it may be in utc/gmt time). you are 2 hrs from gmt?

if i’m right. that’s why it looks fine in the graph and why it shows “differently”… if you want it in local time just do this:

{{ (states(“sensor.m700_next_scheduled_start”) | as_datetime - timedelta(minutes=states(“input_number.worx_garagentore_vorher_offnen”) | int))| as_local }}

that gives the proper time.

for the trigger, you have to express the trigger as something that becomes true… Like when ‘now()’ passes that time… for your trigger template. warning, doing these all in mobile… so giving you the concept but not testing…

“{{ 
now() >=
(states(“sensor.m700_next_scheduled_start”) | as_datetime -
timedelta(minutes=states(“input_number.worx_garage_gates_pre_open”) | int))
}}"
1 Like

Thank you. But with the now in the fuction something is not working right. It says:

platform: template
value_template: |-
  trigger:
      - platform: template
        value_template: "{{ now() > = (states("sensor.m700_next_scheduled_start") | as_datetime - timedelta(minutes=states("input_number.worx_garagentore_vorher_offnen") | int))| as_local }}'"

Message malformed: invalid template (TemplateSyntaxError: unexpected ‘=’) for dictionary value @ data[‘value_template’]

But the local thing has worked wonderfully before

you have a space between the > and =
see mine where there isnt

Ok, I did that, but the trigger does not fire in the automation :frowning:

I have selected the building block template as the trigger, which is correct, isn’t it?

trigger:
    - platform: template
      value_template: "{{ now() >= (states("sensor.m700_next_scheduled_start") | as_datetime - timedelta(minutes=states("input_number.worx_garagentore_vorher_offnen") | int))| as_local }}'"

The funny thing… under developer tools → template it correctly changes from false to true :-/

the trigger (all triggers) fire when it turns from false to true. did that happen? triggers do not fire when they are simply true.

if you validated that (have the automation set and saved… Have the trigger false then make the trigger true) if that didn’t trigger then… Are there other conditions in the automation? post the whole automation you currently have.

No, that’s exactly what he doesn’t do. You can usually see that in automation. There is always a short popup above the trigger that says “triggered”

alias: Template Worx Test Lampe Wohnzimmer
description: ""
trigger:
  - platform: template
    value_template: |-
      trigger:
          - platform: template
            value_template: "{{ now() >= (states("sensor.m700_next_scheduled_start") | as_datetime - timedelta(minutes=states("input_number.worx_garagentore_vorher_offnen") | int))| as_local }}'"
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
  - type: turn_on
    device_id: f96f970a688adbae24cd7047c830d78d
    entity_id: 902ccdb96ef90154b43adb790d1bf746
    domain: light
mode: single

My mistake. I had it in there twice by mistake. Now it works.
How can I change the whole thing to seconds and not minutes?

So it works:

alias: Template Worx Test Lampe Wohnzimmer
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ now() >= (states("sensor.m700_next_scheduled_start") | as_datetime -
      timedelta(minutes=states("input_number.worx_garagentore_vorher_offnen") |
      int))| as_local }}
    for:
      hours: 0
      minutes: 0
      seconds: 1
condition: []
action:
  - type: turn_on
    device_id: f96f970a688adbae24cd7047c830d78d
    entity_id: 902ccdb96ef90154b43adb790d1bf746
    domain: light
mode: single

good to see the whole automation.

  1. you should not be using device id guids. in the ui, choose “entity” (green on the right) and pick the right entity.
    here’s more info why:
    Why and how to avoid device_ids in automations and scripts

  2. when you say “change the whole thing to seconds” what do you mean? do you mean 'input_number.worx_garagentore_vorher_offnen" is seconds? or do you mean something else? if you mean that’s in seconds, just change the minutes= to seconds=

alias: Template Worx Test Lampe Wohnzimmer
description: ""
trigger:
  - platform: template
    value_template: >-
      {{ now() >= (states("sensor.m700_next_scheduled_start") | as_datetime -
      timedelta(seconds=states("input_number.worx_garagentore_vorher_offnen") |
      int))| as_local }}
condition: []
action:
  - type: turn_on
    device_id:  nuke this..
    entity_id:  use the entity_id friendly name

finally… why the “for:” 1 second? that seems odd.

btw… the “as_local” is unnecessary in the trigger because both “now()” and your full expression are fully qualified… ie, both include the timezone info that they are set in. so the comparisons will do the right thing even if the two are in different timezones.

1 Like

Thank you very much for your explanations. From now on I will only use the entities. Good to know :slight_smile:

Yes, I meant the value of the helper. Just like you wrote.

And I only added the 1 second because it didn’t work before. so it was just a test :smiley: