How to use next alarm sensor

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.

Can you put here your automation, and sensor template, please? I’m facing same problem with 115.3

sure here is my sensor:

  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:

  - below: '2'
    entity_id: sensor.minutes_next_alarm_someoneelse
    platform: numeric_state
5 Likes

Hey @Hellis81 ,

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 :wink: 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:

sensor:
  - platform: time_date
    display_options:
      - 'date_time'

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:

binary_sensor:
  andys_alarm_triggered_today:
    name: "[zzz-Helpers] Andys alarm triggered today"
    initial: off
    icon: mdi:alarm

And that’s the automation itself:

- 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:

- id: reset_helper_booleans
  alias: '[zzz-Helpers] Reset helper booleans at night'
  trigger:
  - at: 00:30
    platform: time
  condition: []
  action:
  - service: input_boolean.turn_off
    data:
      entity_id: andys_alarm_triggered_today

Hope this also helps somebody :slight_smile:

4 Likes

Good addition!

I don’t use the snooze :slight_smile:

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?

Perhaps not in HA but I’m quite sure node red can do it.
It can trigger on state change.

But then you make it easier on one end and requires more work in the back end.

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.

2 Likes

Yeah, I’m not using Node-RED, so with vanilla HA you can’t do it right now…

@Steven_Rollason managed to do it in HA.
I don’t have the same ninja skill he does :slight_smile:

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!

I believe the key is this part here:
{{ trigger.to_state.state

So it uses the trigger which is the phone alarm, but it’s “to state”, meaning it has to change to trigger.

Don’t think that does the trick, actually.

  • Because the condition is just preventing the automation to do something if the alarm is unset or because of any issue it turns to unavailable
  • And in the action he then just uses the value to set the input_datetime

But actually making the automation trigger is the interesting thing, as the trigger has no to/from

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.

What exatly do you mean by Helpers? For now I have added the following to my configuration.yaml to define the input_datetime.his_phone_alarm

input_datetime:
  his_phone_alarm:
    name: His Alarm Time
    has_time: true
    has_date: true
    icon: mdi:android

Is there a another way to define these not cluttering the configuration.yaml?

If you go to Configuration > Helpers (in the same section as Automations, Scripts, etc.) you can create the various input entities via the UI instead of needing to use YAML. I believe they are stored under the .storage folder if you create them through the UI.

Thanks. That was quick and easy :smiley:

You are trying to get the min value from three values?
The first two values are proper date time values which can be converted to UNIX (which I assume is done).
So as example right now is 1602418134 UNIX time.
The last value ‘07:30’ does not have a date to it and can not be converted to a UNIX time.

What you need to do is make sure they are all equal before you determine which is “min”.

So either add the date to “07:30” or remove the date from the sensors. Whichever you find the easiest and gives a correct result.

Yes, it seems that I need to define a date to it.
Following activation of the trigger, I need to move the 07:30 variable to the next day in order to check the trigger correctly on the following day. However it seems that the date_time helper variable does not automatically set everything consistently if set manually (i.e. timestamp, date, time and state).

Additionally I just noticed, that input_datetime.his_phone_alarm may also be in the past, incase no new alarm was set. In this case “min” will not do the job.