Run Automation 5 Minutes Before Alarm

I’m trying to get a light to come on 5 minutes before my alarm. I created a datetime helper. I have an automation that is triggered when my next alarm changes. But I can’t get the action for that automation to set the time I’ll use to trigger the other automation to turn the light on to work.

I’ve been using the UI and I’m very new to YAML. I set the action to Call Service and then input.datetime. Then in that section I have this code:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.alarm_helper
data:
  timestamp: "{{ as_timestamp(states.sensor.skiboard_next_alarm.state) - 60 * 5 }}"

But it doesn’t seem to be setting a time for the helper when I check its state. What am I doing wrong?

Please paste in the YAML above as it appears in the editor, correctly formatted (see here).

Next, copy/paste the following into the Template editor and copy/paste into here what it returns:

{{ states('sensor.skiboard_next_alarm') }}
{{ states('sensor.skiboard_next_alarm')|as_timestamp }}
{{ as_timestamp(states.sensor.skiboard_next_alarm.state) - 60 * 5 }}

Thanks for you help. I corrected the formatting in the first post.

Turns out I thought skiboard was an HA default but I needed to change that to my actual sensor. I did that and the template editor validated the code and gave the outputs I would expect. I changed my automation and it started working.

Here’s the code that worked for me:

service: input_datetime.set_datetime
target:
  entity_id: input_datetime.alarm_helper
data:
  timestamp: "{{ as_timestamp(states.sensor.pixel_7_pro_next_alarm.state) - 60 * 5 }}"


Maybe that will help someone else new figure it out. Thanks!

2 Likes