Automation half an hour before alarm

I want my automation to start half an hour before my alarm. I have googled and tried different suggestions that were given in the forum, but have not been able to get my automation to trigger. Right now I am using this template trigger:

trigger:

  • platform: template
    value_template: >
    {{ states(‘sensor.time’) == (as_timestamp(states(‘input_datetime.alarm’)) - 1800) | timestamp_custom(‘%H:%M’, False) }}

Any suggestions on how to improve?

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Does your input_datetime contain just a time or a date and time?

Time only:

{{ now() > today_at(states('input_datetime.alarm')) - timedelta(minutes = 30) }}

Time and Date:

{{ now() > state_attr('input_datetime.alarm','timestamp')|as_datetime|as_local - timedelta(minutes = 30) }}

It only has time. I have updated the YAML to this, but it still does not work:

trigger:

  • platform: template
    value_template: >
    {{ now() > today_at(states(‘input_datetime.alarm’)) - timedelta(minutes = 30) }}

Usually it doesn’t require repeating, but (again), please follow the guidelines for posting code.

1 Like

You need to format your code, as we can’t see, if it is correct…

Please take a look at the link Tom already posted, or read up on that here:

We can’t help you, if you don’t do that, sorry! :slight_smile:

Please, take the time and read these posts. The second thing: if it doesn’t work, provide us with more information. In short: you’re making it really hard to help you. :slight_smile:

Thanks for your understanding!

EDIT: @CO_4X4 beat me to it… :laughing: :laughing:

1 Like
trigger:
  - platform: template
    value_template: >
    {{ now() > today_at(states('input_datetime.alarm')) - timedelta(minutes = 30) }}

Do you see any errors in the code?

Try your code in the developer tools, there you get an error message, that will help you further.

In your case, it’s the incorrect format of “states(…”, as this is a string of the state, and not the corresponding (datetime) object. You would compare a string against a datetime object…

There are a few ways, I’d do it like this:

{{ now() > (state_attr('input_datetime.alarm', 'timestamp') | as_datetime - timedelta(minutes=30)) }}

I tried it with your line, still nothing. In the developer tools, it only says:

This template updates at the start of each minute.

This template listens for the following state changed events:

Entity: input_datetime.alarm

This is the code from @paddy0174 when used against one of my existing date/time helpers:

It works.

That’s the same as what I see, but the automation does not trigger.

If your code is PRECISELY as you pasted it, then the indent is wrong (and should have prevented you from saving it I think):

trigger:
  - platform: template
    value_template: >-
      {{ now() > today_at(states('input_datetime.alarm')) - timedelta(minutes = 30) }}

or

trigger:
  - platform: template
    value_template: "{{ now() > today_at(states('input_datetime.alarm')) - timedelta(minutes = 30) }}"

Please post the complete automation and how you set it up. The template is working, so it must be something else. :slight_smile: