Bvdmgg
(Bvdmgg)
June 6, 2024, 10:30am
1
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?
tom_l
June 6, 2024, 10:48am
2
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) }}
Bvdmgg
(Bvdmgg)
June 6, 2024, 12:27pm
3
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) }}
CO_4X4
(Colorado Four Wheeler)
June 6, 2024, 12:35pm
4
Usually it doesn’t require repeating, but (again), please follow the guidelines for posting code.
Before we begin…
This forum is not a helpdesk
The people here don’t work for Home Assistant, that’s an open source project. We are volunteering our free time to help others. Not all topics may get an answer, never mind one that helps you solve your problem.
[image]
This also isn’t a general home automation forum, this is a forum for Home Assistant and things related to it. Any question about Home Assistant, and about using things with Home Assistant, is welcome here. We can’t help you with eve…
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:
Spaces at the beginning of the line are critical in yaml, and people trying to help you may want to copy your code so that they can try it out themselves, so please format it correctly when you are posting.
Use the preformatted text tool (</> in the edit post toolbar). If it is not immediately visible, it will be in the cogwheel menu.
[Code_format]
Alternatively you can use three backticks (the key is on the far left, 2nd row on en keyboards) on their own line above and below the code. That i…
We can’t help you, if you don’t do that, sorry!
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.
Thanks for your understanding!
EDIT: @CO_4X4
beat me to it…
1 Like
Bvdmgg
(Bvdmgg)
June 6, 2024, 1:04pm
6
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)) }}
Bvdmgg
(Bvdmgg)
June 6, 2024, 2:55pm
8
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
CO_4X4
(Colorado Four Wheeler)
June 6, 2024, 3:04pm
9
This is the code from @paddy0174 when used against one of my existing date/time helpers:
It works.
Bvdmgg
(Bvdmgg)
June 6, 2024, 3:13pm
11
That’s the same as what I see, but the automation does not trigger.
CO_4X4
(Colorado Four Wheeler)
June 6, 2024, 3:17pm
12
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.