How to use next alarm sensor

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') }}"
18 Likes

Thanks Eric
This worked for me and seems like the simplest solution when it comes to triggering automation’s.

just wanted to chime in and say thank you ! works like a charm :slight_smile:

1 Like

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.

input_datetime:
  android_alarm:
    name: Android Alarm Time
    has_time: true
    has_date: true
    icon: mdi:android

automation:
  - alias: Android Set Datetime
    trigger:
      platform: state
      entity_id: sensor.moto_g_5_plus_next_alarm
    mode: restart
    condition:
      - condition: not
        conditions:
          - condition: state
            entity_id: sensor.moto_g_5_plus_next_alarm
            state:
              - unavailable
              - unknown
    action:
      - delay: 00:00:30
      - service: input_datetime.set_datetime
        entity_id: input_datetime.android_alarm
        data_template:
          datetime: "{{ as_timestamp(states('sensor.moto_g_5_plus_next_alarm'))|timestamp_custom('%Y-%m-%d %H:%M:00') }} "

automation 2:
  - alias: Demo
    trigger:
      platform: time
      at: input_datetime.android_alarm
    action:
      ...

5 Likes

I just removed 60 seconds from the alarm time in the automation template.
One minute doesn’t matter

This is my trigger template:


{{ states('sensor.date_time') == (as_timestamp(states.sensor.andreas_next_alarm.state) |int -60)| timestamp_custom('%Y-%m-%d, %H:%M', True) }}

I know I could use the attribute with the timestamp but since it’s in milliseconds it needs to be divided by 1000, so it’s no difference…

3 Likes

Hellis81, do you mind posting your full automation?
Thanks!

This is to turn on the coffee machine one minute before the alarm goes off.

- id: '1594491723756'
  alias: Kaffe Andreas
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.date_time'') == (as_timestamp(states.sensor.andreas_next_alarm.state)
      |int -60)| timestamp_custom(''%Y-%m-%d, %H:%M'', True) }} '
  condition:
  - condition: template
    value_template: '{{ states.input_boolean.kaffe_andreas.state == "on" }}'
  action:
  - data: {}
    entity_id: switch.sonoff_1000b711c4
    service: switch.turn_on
  - data: {}
    entity_id: input_boolean.kaffe_andreas
    service: input_boolean.turn_off
  mode: single
8 Likes

A little off toipic, but i like to get a sensor with the first alarm from 2 phones. Any suggestions ?

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.

1 Like

I got the idea. Thank you :slight_smile:

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