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.
Hrm, I need to give that a try tonight. I didn’t know (and honestly, I should just have tried) you can completely omit to/from in the state trigger…
Thx!
This forum really is a great help. Using your examples I have defined an automation that stores the next alarm in an input_datetime entity called his_phone_alarm and her_phone_alarm.
Now what I would like to achieve is to trigger an action, when the first of the two alarms goes off, or latest at a set time, to trigger the action, in case no alarm is set for that day (i.e. weekends).
trigger:
- platform: time
at: input_datetime.his_phone_alarm
seems to be acceptable, but
trigger:
- platform: time
at: {{ [ {{ [input_datetime.his_phone_alarm, input_datetime.her_phone_alarm] | min }}, 07:30] | min }}
I guess is not an acceptable term for home assistant. Sorry for my confusion, but I am quite new to home assistant and am still struggeling with the language.
Edit: I am guessing that for these more complex terms I can nolonger use the platform: time, but need to use the a template. However I need to assure not to compare apple with oranges, as the next phone alarm is a date and time. On a weekend, this variable may indicate a time for the upcomming monday, so I somehow need to compare it to a date and time variable, that I could set to the same fixed time on the following day, when the action is triggered.
Now I am stuck in defining this action.