How to use next alarm sensor

This is not true, apps that set an alarm with android immediately send out their intent which our app reads. More than likely another app on your device is setting an alarm with the API before the google clock app which prevents an update from showing up because it technically is not the next scheduled alarm.

https://developer.android.com/reference/android/app/AlarmManager#getNextAlarmClock()

https://developer.android.com/reference/android/app/AlarmManager#ACTION_NEXT_ALARM_CLOCK_CHANGED

We also detail this in our docs too: https://companion.home-assistant.io/docs/core/sensors#next-alarm-sensor

As I said on GitHub, I had only the Google clock app set in the allow list, so how is another app interfering? Let’s carry on the discussion there, I only posted here to point anyone else interested there.

Because there is a point in the database which is supposed to be used for alarms but another app uses that spot for their needs.

Just like tasker does.
If you use tasker time events then you can see the alarm time.
So what I did was create the automation in Home Assistant that sends an notification to my phone.
Tasker reads the notification and carries out the action.
And I don’t mean notification with intent, just “sound off” and the sound is shut off.

If anyone has any issues with the sensor.phone_next_alarm not triggering, it’s most likely to be due to a time difference between HA and your phone. My phone was +5s ahead for some reason which meant the next_alarm sensor was being updated to the next alarm before it was triggered in HA. I’m going to use the minutes before sensor to trigger, as triggering a minute before makes no difference but not triggering at all makes a massive difference to my automations. Unfortunately the devs are not willing to look at building any fail-safe into this as they expect everything to work to the letter, which I understand to a degree. Unfortunately in the land of Android things aren’t always such an exact science. So users will have to take a different approach to that which is documented and apply a fail-safe approach if they want to rely on this sensor to start their day.

" + 0*60 " for what ?

Most likely the zero is if you want it to trigger before or after the alarm

Hi,

I have been fiddeling with next_alarm to have a working alarm clock for getting up in the morning. This works great but I’m an avid snoozer and I can’t really wrap my head around on how to get that to work.

This is the code for the automations and the input_boolean to stop the alarm from starting again.

input_boolean:
  alarm_triggered_today:
    name: "Alarm Triggered"
    initial: off
    icon: mdi:alarm

automation:
  - alias: "Alarmclock: Waking Male"
    trigger:
      - platform: template
        value_template: '{{ states(''sensor.date_time'') == (as_timestamp(states.sensor.mobil_male_next_alarm.state)|int -60)| timestamp_custom(''%Y-%m-%d, %H:%M'', True) }}'
    condition:
      - condition: time
        after: '05:00:00'
        before: '10:00:00'
      - condition: state
        entity_id: binary_sensor.mode_home
        state: 'on'
      - condition: state
        entity_id: input_boolean.alarm_triggered_today
        state: 'off'
    action:
      - service: script.turn_on
        entity_id: script.alarm_clock_on
      - service: input_boolean.turn_on
        data:
          entity_id: input_boolean.alarm_triggered_today

  - alias: "Alarmclock: Reset Triggered Today"
    trigger:
      platform: time
      at: '01:00:00'
    action:
      - service: input_boolean.turn_off
        data:
          entity_id: input_boolean.alarm_triggered_today

This is the script for starting the wake-up process.

script:
  alarm_clock_on:
    alias: "Alarmclock: Waking up Male starts"
    sequence:
      - service: media_player.select_source
        data:
          entity_id: media_player.sonos_symfonisk
          source: 'Morgenkaffe'
      - delay: '00:00:20'
      - service: light.turn_on
        data:
          entity_id: light.simon_dimmer_bedroom_level
          brightness_pct: 10  
          transition: 30
      - delay: '00:00:20'
      - service: light.turn_on
        data:
          entity_id: light.simon_dimmer_bedroom_level
          brightness_pct: 20  
          transition: 30
      - delay: '00:00:30'
      - service: light.turn_on
        entity_id: light.nedis_wifi_smartbulb_bedroom_left
        data:
          brightness: 255
          rgb_color: [239,235,216]
      - service: light.turn_on
        entity_id: light.nedis_wifi_smartbulb_bedroom_right
        data:
          brightness: 255
          rgb_color: [239,235,216]

This is what happens when I hit the snooze button.
When hitting the snooze button (WallMote) all lights and audio stops immediately. I assume that is because of script.turn_off killing script.alarm_clock_on.
After a 5 minute delay the alarm goes of again but when hitting the snooze button nothing happens until the sequence is complete. What I want is to have all lights and audio stop immediately.
If I wait til the script has finished the snooze button works and I can sleep for 5 more minutes.

  alarm_clock_snooze:
    alias: "Alarmclock: Snoozing"
    sequence:
      - service: script.turn_off
        data:
          entity_id: script.alarm_clock_on
      - service: light.turn_off
        entity_id: light.simon_dimmer_bedroom_level
      - service: light.turn_off
        entity_id: light.nedis_wifi_smartbulb_bedroom_left
      - service: light.turn_off
        entity_id: light.nedis_wifi_smartbulb_bedroom_right
      - service: media_player.volume_mute
        data:
          entity_id: media_player.sonos_symfonisk
          is_volume_muted: true
      - delay: '00:05:00'
      - service: media_player.volume_mute
        data:
          entity_id: media_player.sonos_symfonisk
          is_volume_muted: false
      - delay: '00:00:20'
      - service: light.turn_on
        data:
          entity_id: light.simon_dimmer_bedroom_level
          brightness_pct: 10  
          transition: 30
      - delay: '00:00:20'
      - service: light.turn_on
        data:
          entity_id: light.simon_dimmer_bedroom_level
          brightness_pct: 20  
          transition: 30
      - delay: '00:00:30'
      - service: light.turn_on
        entity_id: light.nedis_wifi_smartbulb_bedroom_left
        data:
          brightness: 255
          rgb_color: [239,235,216]
      - service: light.turn_on
        entity_id: light.nedis_wifi_smartbulb_bedroom_right
        data:
          brightness: 255
          rgb_color: [239,235,216]

This is stops the alarm clock.

  alarm_clock_stop:
    alias: "Alarmklokke: Slå av alarm"
    sequence:
      - service: script.turn_off
        data:
          entity_id: script.alarm_clock_on
      - service: light.turn_off
        entity_id: light.simon_dimmer_bedroom_level
      - service: light.turn_off
        entity_id: light.nedis_wifi_smartbulb_bedroom_left
      - service: light.turn_off
        entity_id: light.nedis_wifi_smartbulb_bedroom_right
      - service: media_player.media_pause
        data:
          entity_id: media_player.sonos_symfonisk

Any help would be appreciated.

Best regards

You probably want to set the mode for your automation/script

Hi Andy,

Blockquote 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:
andys_alarm_triggered_today:
name: “[zzz-Helpers] Andys alarm triggered today”
initial: off
icon: mdi:alarm

Where and how did you enter that ?

Under binary_sensor: in the configuration.yaml

I did it with ‘helpers’ through UI, seems to be working.
But I keep getting

Template variable error: ‘sensor’ is undefined when rendering '{{ states(‘sensor.date_time’) == as_timestamp…

Any idea why?

Do you have a sensor.date_time?
It’s a sensor you need to configure in configuration.yaml

yes. i have
sensor:

  • platform: time_date
    display_options:
    • ‘date_time’
      In configuration.yaml

Since you only gave use a partial error message then it’s hard to help you more. But most likely it’s what comes after as_timestamp

My automation:

alias: '[Bedroom] Morning'
description: ''
trigger:
  - platform: template
    value_template: >-
      "{{states('sensor.date_time') ==
      as_timestamp(states.sensor.motorola_razr_5g_next_alarm)|
      timestamp_custom('%Y-%m-%d, %H:%M') }}"
condition: []
action:
  - service: mqtt.publish
    data:
      topic: blinds/side/percentage
      payload: '0'
mode: single

Error message:

  • Template variable error: ‘sensor’ is undefined when rendering ‘{{ states(‘sensor.date_time’) == as_timestamp(sensor.motorola_razr_5g_next_alarm)| timestamp_custom(’%Y-%m-%d, %H:%M’) }}’

I got rid of the previous error, quotation marks were missing.
But the automation does not trigger. I see no errors in the log.
Could that be because of the mismatch of timestamps?

I have the following automation:

trigger:
- platform: template
  value_template: >-
    "{{states('sensor.date_time') ==
      as_timestamp(states.sensor.pixel_3_next_alarm)|
      timestamp_custom('%Y-%m-%d, %H:%M') }}"

condition:
- condition: and
  conditions:
  - condition: state
    entity_id: group.persons
    state: home
  - condition: time
    after: 06:00
    before: '12:00'

action:
- service: script.play_radio_sb
mode: single

But when the alarm on the phone goes, it does not trigger this automation. What do I need to change to get the trigger working?

should be:

as_timestamp(states.sensor.pixel_3_next_alarm.state)

or
as_timestamp(states('sensor.pixel_3_next_alarm'))

You can always try your templates in the templates tool and see what they output before using them in automations.

You can use the sensor directly as you are trying to trigger the moment the alarm sounds off.

5 Likes

Thx, real easy that way