How to automatically turn-on the room's light some minutes before the phone's alarm clock?

Hi, I’m totally ignorant about automation on hassio.

I have the android app of Home Assistant, which natively send to HA the timestamp of the next scheduled alarm clock (entity is called sensor.smartphone_moto__next_alarm).
I’d like to automatically turn on the light in the room ~5 minutes before the alarm clock will ring (I already have that switch configured/working).

Now I’m not sure about what I have to insert inside automation.yaml . Seems like I have to compile the trigger section with some platfrom able to send events. But in fact, it seems that the time of next alarm is sent as state, see below [1].
At the same time, the hassio UI is “correctly” interpreting that state, because it shows a countdown to the next alarm clock. Any idea about how to configure such automation?

[1] : state visible from “Developer tools” in hassio UI

Local Time: Sun Jul 11 07:28:00 GMT+02:00 2021
Package: com.google.android.deskclock
Time in Milliseconds: 1625981280000
friendly_name: smartphone_moto Next Alarm
icon: mdi:alarm
device_class: timestamp

Setup an input_datetime and two automations.

The first automation is setting your “light_on_time” in this input_datetime with a value sensor.smartphone... minus five minutes. This automation is triggered by the state change of the sensor.

The second automation is turning your light on, when the time in the input_datetime is triggered.

If you want to go crazy, you can even set the time frame (the five minutes) via an input_number that represents that value.

1 Like

you can set this as your trigger template:

{{ as_timestamp(states('sensor.smartphone_moto__next_alarm')) - 600 < as_timestamp(now()) }}
7 Likes

thank you both :smiley: I’ve successfully configured the solution proposed by @finity . But I see the higher level of customization of @paddy0174 's solution.

are there other practical differences, between the two solution? I suppose that hassio perform a constant evaluation (of expression) for @finity solution, while @paddy0174 solution sounds more like event driven . Am I right?

just another way-- google has a ‘clock’ which has routines, and conveniently has all the exposed data from home assistant.

Its so easy, its not even funny.

Not really.

Both are driven by entity state changes.

In paddy’s solution you have two automations and an extra input_datetime entity.

mine only uses the existing entities already exposed in HA.

You could still use my solution even if you wanted to have a variable “minutes before alarm” setpoint.

just create an input_number that holds the “minutes before the alarm” value you want it to trigger.

then use that value (converted into seconds) in the template above instead of the fixed “600” value.

@fiftys Intersting, I’ll dig more for future references.

@finity Thank you for your patience

Now my small routine is happily working. I’m thinking about the next possible/useful automatization

1 Like

Thank you very much.

This worked flawlessly for a long time but no longer works for me anymore, it just doesn’t trigger. Anyone else having the same issues?

Beware time synchronization issues: Android next alarm sensor not triggering automations or Node Red time nodes at alarm time · Issue #2616 · home-assistant/android · GitHub