I make use of the alarm data in multiple automations and templates, so for me it made sense to create a template sensor that gives the minutes until the next alarm.
- platform: template
sensors:
minutes_next_alarm:
friendly_name: "Minutes until Next Alarm"
unit_of_measurement: 'm'
entity_id: sensor.time
value_template: "{{((states('sensor.moto_g_5_plus_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}"
availability_template: "{{ not is_state('sensor.moto_g_5_plus_next_alarm','unavailable') }}"
attribute_templates:
time: "{{ state_attr('sensor.moto_g_5_plus_next_alarm','Local Time') }}"
I have discovered that using the next_alarm sensor data directly may not be 100% reliable.
With my phone as soon as the android alarm app fires the alarm it immediately changes value of the next_alarm to a different value. It will immediately will relay the change the home assistant and home assistant will change the value of the sensor. The amount of time it takes is about 1 second dependent entirely on the amount of latency it takes for the state change to occur leaving a narrow window of time for the automation to trigger. Everything works when hardware clocks of the android phone and the machine home assistant is running on are in sync and home assistant can evaluate the trigger as true before next_alarm changes causing the automation to trigger.
However if the android clock is running a little fast, or the HA clock a little slow the android app can fire alarm and clear the next_alarm sensor before the clock on the HA machine reaches minute and 0 seconds causing the automation using the next_alarm value to never trigger.
The solution I have come up with is following: Use an automation to copy valid alarm data (seconds are truncated) into an input_datetime and hold the value for 30 seconds after a state change to prevent the data from being lost.
The input_datetime entity can be used directly with platform: time and condition: time starting with version 0.115.
This is going to be a rather generic answer because I only have my phone at hand.
But I believe if you add the timestamp attributes to an array and get the min value then that should be it.
So something like:
{{ [sensor.alarm.attributes.timestamp, sensor.alarm2.attributes.timestamp] | min }}
That should give you the first alarm.
Then I guess you can add a format of the timestamp after.
I can do it on the computer tomorrow if you need help.
I was using this as well, however since HA core version 0.115 it is not working anymore. As entity_id’s are not needed anymore, the sensor is not triggered every minute anymore. I added a dummy read in the value_template of sensor.time and now the sensor gets update every minute again.
sensors:
minutes_next_alarm_paul:
friendly_name: "Minutes until Next Alarm XXXX"
unit_of_measurement: 'm'
#entity_id: sensor.time
value_template: >-
{% set dummy = states("sensor.time") %}
{{((states('sensor.XXXXX_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
availability_template: "{{ not is_state('sensor.XXXXX_next_alarm','unavailable') }}"
attribute_templates:
time: "{{ state_attr('sensor.XXXXX_next_alarm','Local Time') }}"
Where XXXXX is the name of the phone. The line with set dummy = states(“sensor.time”) makes sure that this sensor gets updated every minute.
The automation:
- alias: NextAlarm Wakeup light
description: switch light based on next alarm
trigger:
- below: '2'
entity_id: sensor.minutes_next_alarm_paul
platform: numeric_state
condition:
- after: '6:30'
before: '10:00'
condition: time
action:
- service: script.light_on
- delay: '2700'
- service: script.light_off
mode: single
This script will run 1 minute before the alarm goes off (below 2 means at 1) and calls a script. I this script I have a transition time of 60 seconds, so when the alarm goes off the lamp is also at full power. Further I will only allow the script to run between 6:30 and 10 AM, so that when I need to get up very early my partner does not notice. And not after 10 AM for any other alarm I am using during the day, the light does not need to be turned on.
You can at more triggers like shown below if your partner has a different alarm set:
thx for this automation! It’s sad we cannot just use the next_alarm sensor directly as trigger in HA (yet), like we can now use any input_datetime.
I think your automation is the simplest to achieve a trigger based on the next_alarm.
But it took me a bit to find out how it actually gets triggered: I removed the “sensor.date_time”, as I thought that’s something custom from you, until I found out: That’s a sensor platform Time & Date - Home Assistant
And this makes the automation actually get triggered. So I just wanted to add here if somebody else also wonders:
To make use of it, this needs to be added to the configuration.yaml:
There are more sensors available, noted in the link above.
Also, for those people (like me) who love to hit the “Snooze” button, I’ve added a little binary input to not have the automation trigger multiple times in the morning:
- id: wake_up_andy
alias: '[Bedroom] Wake up Andy!'
trigger:
- platform: template
value_template: "{{ states('sensor.date_time') == as_timestamp(states.sensor.vog_l29_next_alarm.state)| timestamp_custom('%Y-%m-%d, %H:%M') }}"
condition:
- condition: state
entity_id: binary_sensor.workday_sensor
state: 'on'
- condition: state
entity_id: input_boolean.andys_alarm_triggered_today
state: 'off'
action:
- scene: scene.bedroom_wake_up_andy
Note: The binary_sensor.workday_sensor is also a special sensor: Workday - Home Assistant
And this automation will reset the helper boolean at night again:
Now that I have been thinking about it, I believe you can.
If you have a input_datetime that is set by an automation that is triggered by the sensor change.
I had the same idea, but I wasn’t able to write the trigger: Afaik, you can only write state changed trigger with either “from” or “to” (or both). But you can’t write a trigger for just “if the sensor changed from anything to anything”. Or can you?
I’ve tried something like this, and it seems to work fine for me (you just need to create the input_datetime through Helpers):
- id: set_next_alarm_time
alias: Set Next Alarm Time
trigger:
- platform: state
entity_id: sensor.my_phone_next_alarm
condition:
- condition: template
value_template: '{{ trigger.to_state.state not in ["unknown","unavailable"] }}'
action:
- service: input_datetime.set_datetime
data:
timestamp: '{{ as_timestamp(trigger.to_state.state) }}'
entity_id: input_datetime.next_alarm_time
mode: single
- id: alarm_triggered
alias: Alarm Triggered
trigger:
- platform: time
at: input_datetime.next_alarm_time
action:
... actions to be triggered by alarm ...
Technically, you should be able to use the “Time in Milliseconds” attribute of the alarm sensor instead of using as_timestamp but somehow (for me at least) this seems to end up being a few seconds out.