How to use next alarm sensor

Cool. Thanks.

I did get my alarm to trigger automations, but i like to set multible alarms with about 5 to 15 minutes in between.
I want the lights to turn on when the last alarm goes off.
So is there a way to ad a condition that if there is second alarm in the next 15 minutes it stops?

No the app is unaware of any additional alarms set. It only knows about the next alarm. One at a time.

The app may be unaware of it, but Home Assistant can be made to be…

If you throw a short delay in at the beginning of the actions, you can then poll the entity again to see if there is another alarm within 15 minutes…

alias: Mobile Last Alarm sequence
description: ""
trigger:
  - platform: time
    at: sensor.YOUR_PHONE_next_alarm
condition: []
action:
  - delay: 5
  - variables:
      no_next: "{{ (today_at() + timedelta(days=1)) | string }}"
      alarm: >
        {% set ent = 'sensor.YOUR_PHONE_next_alarm'%}
        {{ no_next if (states(ent) == 'unavailable') else states(ent) }}
  - choose:
      conditions:
        - alias: Check if this is the last alarm for this morning
          condition: template
          value_template: |
            {{ alarm | as_datetime > now() + timedelta(minutes=15) }}
      sequence:
        - service: light.turn_on
          data: {}
          target:
            entity_id: light.bedroom
    default:
      - service: YOUR NORMAL MORNING SERVICES
mode: single

If you need data from the originating alarm you can use one or more or the trigger variable types.

This does not account for an alarm being snoozed though, it only helps you see what the next state of the sensor is.

And this part can be automated also.
My spouse likes to do this also, so if she sets an alarm the next morning then an automation kicks in and sets 9 more alarms with a few minutes between them

I would say Drew’s method is a valid answer.
Sure a snooze will knock things out of balance, but then again, the snooze will be the last alarm therefor it’s still correct as I see it.
But I guess Tw needs to answer this.

In reality snoozing or another alarm immediately after the current one is dismissed will still have the same results. The app and HA are still unaware of any additional alarms set until the current alarm has gone off (thats what the delay is for, waiting for the sensor to update). Whether you snooze an alarm for 5 minutes or have another alarm set for 3 minutes. The app will just report the literal next alarm on schedule. I always tell users its best to think of things like a literal timeline where only one comes after another. The app only knows what is next in line.

Now if you are looking to actually determine if an alarm was snoozed or dismissed, Sleep as Android has custom events for that to let things get a little more granular.

Thanks for the reply’s.
I have something that works for me.

alias: Wekker
description: ""
trigger:
  - platform: time
    at: sensor.sm_s908b_next_alarm
condition:
  - condition: zone
    entity_id: device_tracker.sm_s908b
    zone: zone.home
action:
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 500
  - if:
      - condition: time
        after: sensor.sm_s908b_next_alarm
        before: "23:59:59"
    then:
      - type: turn_on
        device_id: 8f969db4737ed9aaa463b8d68922edb9
        entity_id: light.filament
        domain: light
        brightness_pct: 40
mode: single

It now checks if there are no other alarms before midnight and does not trigger if there is one.

I only use alarms to wake up, not as (next day) reminders, so this works for me.

Does it still work for you? Since upgrading to 2023.6 this method of triggering automations stopped working for me and it worked for a long time without any problems.
I’ve opened an issue for it: Android next alarm sensor no longer triggers automation · Issue #94526 · home-assistant/core · GitHub

Edit:
OK, got it. It turned out that Android clock was a few seconds ahead of actual time and by the time it get to the set hour in HA the sensor got updated to the next alarm set in the phone. Syncing clock on the phone solved the issue.

1 Like

This turn lights on after alarms but how to turn on light before alarm?

This is how I made my automation to get the light to fade in before the phone alarm.

For this I have created:

Two input boolean for turning the automation or auto light on and off

input_boolean.alarm_clock_phone
input_boolean.alarm_clock_light

Two date times, one is for the phone alarm time, and the other is when the lights will turn on.

input_datetime.alarm_clock_phone
input_datetime.alarm_clock_light_start

Then an input number for how long before the phone alarm I want the lights to turn on. This will also make the light fade in to 100% (or profile Energize) when the alarm goes off. You will have to change this to suit your conditions.

input_number.alarm_clock_light_fade_in

Then my automation

alias: Alarm clock Phone
description: ""
trigger:
  - platform: time
    at: input_datetime.alarm_clock_light_start
    id: Light On
  - platform: state
    entity_id:
      - sensor.phone_next_alarm
    for:
      hours: 0
      minutes: 1
      seconds: 5
    id: Phone sensor
  - platform: state
    entity_id: input_number.alarm_clock_light_fade_in
    entity_id: input_boolean.alarm_clock_light
    to: "on"
    id: boolean light on
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: Light On
        sequence:
          - condition: state
            entity_id: input_boolean.alarm_clock_light
            state: "on"
          - service: light.turn_on
            target:
              entity_id: light.bedroom
            data:
              profile: Energize
              transition: >-
                {{states('input_number.alarm_clock_light_fade_in') |
                int(default=1) * 60 }}

    default:
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.alarm_clock_phone
                state: "on"
            sequence:
              - service: input_datetime.set_datetime
                target:
                  entity_id: input_datetime.alarm_clock_phone
                data:
                  datetime: >-
                    {{ as_timestamp (states("sensor.phone_next_alarm"), 0)|
                    timestamp_local }}
        default: []
      - choose:
          - conditions:
              - condition: state
                entity_id: input_boolean.alarm_clock_light
                state: "on"
            sequence:
              - service: input_datetime.set_datetime
                target:
                  entity_id: input_datetime.alarm_clock_light_start
                data:
                  datetime: >-
                    {{ (states("input_datetime.alarm_clock_phone")| as_timestamp
                    - states('input_number.alarm_clock_light_fade_in') |
                    int(default=1) * 60 ) | timestamp_local }}
        default: []

mode: parallel
max: 2

It might not be the best of automations, but it does work for me :slight_smile:

2 Likes

Hello, in principle the automation works, but the lamp also turns on if I change the alarm on my cell phone or deactivate it. Is this normal? Can I change this? As I understand it, it only checks whether the alarm changes, and if so, the action is carried out.

Thank you for the great solution, it really helped! I took another approach, but I think I got it so far, but unfortunately it does not trigger the alarm.
I used these two lines:
(the upper one in a template,
the bottom one only as reference)

{{ (as_timestamp(states("sensor.phone_next_alarm"),0) - 30 * 60 ) | timestamp_local }}
{{now()}}
2024-01-09T05:30:00+01:00
2024-01-08 20:59:00.499228+01:00

So it is quite different, but I have no idea why this does not activate the trigger.
Do you have any idea?

Hello, in principle the automation works, but the lamp also turns on if I change the alarm on my cell phone or deactivate it. Is this normal?

No! :slight_smile:
My automation is based on two automations, that I’ve merged into one, as I like having it in the same automation.

But I started out with two:

Automation 1 is responsible for updating the “input_datetime.alarm_clock_light_start”. And this will trigger everytime the time changes, and update accordingly.

Automation 2 triggers by the time “input_datetime.alarm_clock_light_start” and start turning on the lights.

I use the “choose” funtion to separate this in the merged automation.
If the automation is triggered by the “alarm_clock_light_start” (see that I’ve given it an id name “Light On”:

trigger:
  - platform: time
    at: input_datetime.alarm_clock_light_start
    id: Light On

It will only trigger the action:

action:
  - choose:
      - conditions:
          - condition: trigger
            id: Light On

If any other trigger starts the automation, it will start at the “default” section.

action:
  - choose:
     ...
        Ignore this
     ...
    default:
        Starts here

If it’s confusing, I would try to do it in two automations. Then it’s probably easier to understand and trouble shoot.

So it is quite different, but I have no idea why this does not activate the trigger.

You don’t really give us much info to work on here.

Is this your automation trigger?:

{{ (as_timestamp(states(“sensor.phone_next_alarm”),0) - 30 * 60 ) | timestamp_local }}

If so, I would verify that you can use a time template as a trigger in Home Assistant.
I have never done that, so I don’t know. And I feel it’s somewhat strange way of doing it…

Thank you for the reply!
So, my idea was to use no helper at all. If you use a calendar event as a trigger you can set an offset and that’s it. But for next_alarm there is no way doing so.
So I thought I use a helper which acts as a trigger but this also doesn’t work, as mentioned above.
It works now as described in this post but this feels like too many automations just for an alarm clock.

But hey, it works, so I focus on other things now and might come back to this later.

Thanks a million for this. Drove me up the wall to find out why automation didn’t work, turned out phone’s clock was 2 seconds ahead.
But this makes the usage of next_alarm unreliable, since even a tiny bit out of sync results in automation not triggering.
Is there a way to make the next_alarm update every minute on 30th second for example? That way even if clocks are a bit out of sync, the automations would still work.

1 Like

The next alarm sensor will always update immediately upon receiving a state change. That’s why the time being kept in sync is important.

Yup, that’s exactly why I suggest that it should be updated not immediately but with a small delay. Corner case here is a person has two alarms set 1 minute apart and their phone is over 30 seconds out of sync with the HA. But how many people really set alarms 1 minute apart while I imagine 1-5 seconds out of sync should not be that uncommon.

You can just create a template sensor that holds your next alarm - 10 seconds and use this as your automation trigger.