How to use next alarm sensor

I tried this, seems lika a good clean approach. There are some quirks that mess it up for testing, but in real life it might work OK.

But, i seem to run into that the alarm is moved from the app before HA gets around to triggering it. I ususally use flight mode so that should again work for real life, but still a bit fudged…

Hi @myle

Thanks for your template.

I am trying to add 5 mins to the next alarm in minutes but I am not getting the right value with the 5 minutes added. Here is my code:

{% set dummy = states("sensor.time") %}
        {{((states('sensor.sm_g986b_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int + now()|as_timestamp|int +5|int}}
      availability_template: "{{ not is_state('sensor.sensor.sm_g986b_next_alarm','unavailable') }}"

Can you please help?

Thanks.

Off top of head not in fron of computer as does not know what 5 is so

Try now.timedelta(0,5)

1 Like

I’ve the sensor working. But I want it to turn on the thermostat. Automation doesn’t get triggered. I didn’t use a condition but that’s not really needed for now. It should work, but it doesn’t.

configuration.yaml

sensor:
  - platform: fitbit
    clock_format: 24H
    monitored_resources:
      - "body/weight"
      - "activities/steps"
      - "activities/heart"
      - "sleep/minutesAsleep"

  - platform: template
    sensors:
      minutes_next_alarm_stefan:
        friendly_name: "Minutes until Next Alarm Stefan"
        unit_of_measurement: "m"
        value_template: >-
          {% set dummy = states("sensor.time") %}
          {{((states('sensor.pixel5_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
        availability_template: "{{ not is_state('sensor.pixel5_next_alarm','unavailable') }}"
        attribute_templates:
          time: "{{ state_attr('sensor.pixel5_next_alarm','Local Time') }}"
        device_class: timestamp

By the way, take a look at device_class I added there. Without it, no way the sensor would become available in Ha; One of the few things I hate about HA, nothing about that in the official documentation or on the forum, somebody on the Discord told it to me. After looking around for ages why it wouldn’t work. By the way, the official code also doesn’t work as it’s timestamp it requires UTC input and I’m on GMT +1. I’ve a very hard time understanding why that code exists officially and it simply doesn’t work. It should work and be as easy as that by the way, but it isn’t. Oh boy no, why do something in 5 minutes as 5 hours is better …

this code doesn’t work

automation:
  - trigger:
      platform: time
      at: sensor.phone_next_alarm
    action:
      service: light.turn_on
      entity_id: light.bedroom

on with the automation that won’t trigger, shall we?

- id: '1609284651176'
  alias: Turn on heat 1 hour before wake-up
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.minutes_next_alarm_stefan
    below: '60'
  condition: []
  action:
  - device_id: 91d0ff446acba47608bbe4119dfe616e
    domain: climate
    entity_id: climate.thermostat
    type: set_preset_mode
    preset_mode: home
  mode: single

In the log file, I don’t see the automation being triggered.
It only says sensor.minutes_next_alarm_stefan became unavailable (which goes rather fast, I suspect HA doesn’t have the time to trigger the automation, or have I done something stupid?

Any help would be greatly appreciated. As someone who works at unregular hours, this automation is a life and euro saver and one of the few that are really the reason why I started with HA, otherwise I could just have stayed with Google Routines.

What official code?
If you’re talking about the template sensor you have there then that is a copy of another members code. That is not official (perhaps say thank you?).
If you need it in a different timezone then just add the time you need

What?

No… because your phone is called pixel5, not phone. So sensor.pixel5_next_alarm I assume.

You said before that this entity does not work (sensor.minutes_next_alarm_stefan). Does it work now? Or are you trying to code an automation to a entity that does not work.

This thread links to automation triggers documentation on the website of HA. https://www.home-assistant.io/docs/automation/trigger/#sensors-of-datetime-device-class

It’s supposed to be the more easy way to get this very handy automation done.

And there is this “official” code about this automation trigger.

automation:
  - trigger:
      platform: time
      at: sensor.phone_next_alarm
    action:
      service: light.turn_on
      entity_id: light.bedroom

I was only pointing out that doesn’t work dude. And it’s not my first time with HA, that I try to get something working, only to notice that it’s own documentation is irrelevant or incomplete.

So the sensor code in this thread works. But my automation still wouldn’t kick in. I tried multiple times last night to set an alarm on my phone and to trigger the automation. No luck.

I went to sleep disappointed and tried one final time and I set an alarm for 1 pm

And look what I’ve found in the logfile

[Turn on heat 1 hour before wake-up] has been triggered by numeric state of sensor.minutes_next_alarm_stefan

12:01:00 PM - 4 hours ago

It worked. Which makes sense as there were no errors anymore in the code. And it was warm in my house when I woke up today.

Why? I’ll never know …I did a full restart just before setting the alarm. Maybe reload automations alone wasn’t enough. I added a condition that I needed to be home. Maybe that did the trick. Who knows?

Now let’s monitor it and see if it keeps working.

I’m always grateful for code examples. There’re way to little over here (or they’re very hard to find in threads multiple pages long with a lot of bad code where you’ve to weed through in order to find the good code and hardly searchable).

I do get pissed though if I’ve to spent multiple hours only to find out that it wasn’t my stupidity but somebody’s else (and even that I can live with in all honesty, nobody is perfect, myself included), but I can’t live with the fact that somebody knows on the discord how it works (thankfully) but that was only by chance that I found that answer. Why isn’t it than in the official documentation if people know that it’s wrong?

1 Like

So for reference. I was the one who helped you out on discord. Can you do me a favor and double check that HA has the correct time zone for your installation under configuration? I would also check the host OS too and ensure its all reporting the time/date correctly.

Just now I used the exact same example as the documentation provided.

- alias: Phone alarm
  trigger:
    platform: time
    at: sensor.dannys_pixel_4_xl_next_alarm
  action:
  - service: notify.mobile_app_dannys_pixel_4_xl
    data_template:
      message: "alarm going off"

I set my alarm for 1 minute in the future and sure enough as my phone was going off the notification arrived as expected.

I suspect your issue may have to do with the date/time of the server rather than an issue with the code itself.

Also the docs you linked to mention specifically what type of sensor is accepted here

The Entity ID of a sensor with the “timestamp” device class.

in addition to that the dev docs outline the data the sensor is required to send back, but given the example worked you didnt need to be concerned with this part

what is your sensor.phone_next_alarm what does it say in the Developer tools area

I dont have that sensor i have

sensor.stephan_phone_next_alarm which came from my phone

my understanding is it wont get trigger as there is no trigger its telling you the state of the next alarm
and the state only changes when the next alarm is reset to the next one which wont nevery be now

but what we can do is create a sensor that counts down to the next alarm

    minutes_next_alarm_stephan:
      friendly_name: "Minutes until Next Alarm Stephan"
      unit_of_measurement: "m"
      value_template: >-
        {% set dummy = states("sensor.time") %}
        {{((states('sensor.stephan_phone_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
      availability_template: "{{ not is_state('sensor.stephan_phone_next_alarm','unavailable') }}"
      attribute_templates:
        time: "{{ state_attr('sensor.stephan_phone_next_alarm','Local Time') }}"

the maths of the sensor is

{{((states('sensor.stephan_phone_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}

which take the sensor.stephan_phone_next_alarm minis now() from it which give us a minutes left

now we right a automation and watch the tigger

- id: '85cf493e-b8eb-4a8b-8645-b384b752d0fd'
  alias: My Phone Alarm about to go off
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.minutes_next_alarm_stephan
    below: '2'
  condition: 
  - condition: state
    entity_id: light.kitchen
    state: 'off'
  action:
 bla bla bla

and as for say

they do work on his system but we dont know what the

sensor.phone_next_alarm is

those example are pointer so you see how to do it

here is a other one

  - trigger:
      platform: state
      entity_id: binary_sensor.motion
      to: 'on'

so it always trigger
but

  - trigger:
      platform: state
      entity_id: binary_sensor.motion
      form: 'off'
      to: 'on'

will only trigger when change form off to on

and there a big differance

and

  trigger:
  - entity_id: 
    - binary_sensor.garage_side_door
    - binary_sensor.garage_door
    - binary_sensor.front_door
    - binary_sensor.cupboard_door
    - binary_sensor.linen_door
    - binary_sensor.washing_door
    - binary_sensor.dryer_door
    platform: state

so when any of these state CHANGE the automation is triggered

2 Likes

Yeah, but that doesn’t work because those timestamp things come in UTC format and I’m on GMT+1; There’s always an hour difference in the sensor that way and it will never fire.

That’s what they said to me on the Discord and that I should use a template sensor and use local time. Which i did and which eventually worked.

So maybe I did something stupid but that code doesn’t work. My nas is on tz europe/brussels, docker also, ha container also, phone also.

thx, but that’s what I did eventually to get it working, it didn’t fire last night (and I tried multiple times), but magically it worked over night, so let’s see it will work the coming night also

I am in PST timezone and the automation triggers fine so I take back what I said about that in discord. check your timezone and system date/time make sure they match etc…

Something is off in your setup if its working for me using the same example you expected to work. Keep in mind im in a different time zone and teh sensors actual state is UTC so HA is able to make the time translation for you in the automation.

I upgraded to latest version that became available today and it works now!

Alarm sensors from multiple phones:

Maybe this is useable for some else as well: I wanted to be able to set an alarm on any of our phones and turn on the light 30min before the alarm goes off. The sensor always selects the alarm which will go off first (no matter on which phone).

The sensor also contains an attribute with a more human-readable format (in localtime) of the next alarm to be used in a lovelace card:

image

    next_alarm:
       value_template: "{{ [states('sensor.col_l29_next_alarm'), states('sensor.ane_lx1_next_alarm')] | min }}"
       attribute_templates:
         next_alarm_localtime: >-
           {{ as_timestamp(states('sensor.next_alarm')) | timestamp_custom('%A %H:%M') }}

The supporting binary_sensor:

binary_sensor:
  - platform: template
    sensors:
      alarm_light_on:
        value_template: >-
          {% set alarm_time = as_timestamp(states('sensor.next_alarm')) %}
          {% set time_now = as_timestamp(now()) %}
          {% if alarm_time != None %}
            {{ time_now >= alarm_time - 1800 }}
          {% else %}
            {{ unavailable }}
          {% endif %}

The automation for it looks as follows:

alias: Turn on led_underbed 30min before Alarm goes off
description: ''
trigger:
  - platform: state
    entity_id: binary_sensor.alarm_light_on
    to: 'on'
condition:
  - condition: time
    after: '05:00'
    before: '09:00'
action:
  - service: light.turn_on
    data:
      color_name: royalblue
    entity_id: light.underbed
mode: single

Lovelace card:

      card:
        type: entity
        name: Next Alarm
        icon: 'mdi:alarm'
        entity: sensor.next_alarm
        attribute: next_alarm_localtime
7 Likes

This one is really great! I’ve added some attributes to sensor.next alarm

A, Time left in minutes
B, For text in Lovelace, show how many hours and minutes left (ex 1 hour and 40 minutes but in Swedish ^^,)
C, Tell me who’s alarm is primary

  sensors:
    next_alarm:
      value_template: "{{ [states('sensor.hannahs_mobil_next_alarm'), states('sensor.oneplus_a6013_next_alarm')] | min }}"
      attribute_templates:
        alarm_time: >-
          {{ as_timestamp(states('sensor.next_alarm')) | timestamp_custom('%H:%M') }}
        minutes_left: >-
          {% set dummy = states("sensor.time") %}
          {{((states('sensor.next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
        text_time_left: >-
          {% set time = state_attr('sensor.next_alarm','minutes_left') %}
          {% set hours = ((time | int /  60) | string).split('.')[0] %}
          {% set minutes = time | int % 60 %}
          {{hours}} timmar och {{minutes}} minuter 
        text_whos_alarm: >-
          {% if states('sensor.oneplus_a6013_next_alarm') == states('sensor.hannahs_mobil_next_alarm') %} Inget alarm
          {% elif ((states('sensor.next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int ==  ((states('sensor.hannahs_mobil_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int %}
          Hannah
          {% elif ((states('sensor.next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int ==  ((states('sensor.oneplus_a6013_next_alarm')|as_timestamp|int - now()|as_timestamp|int)/60)|int %}
          Filip
          {% else %}
          ERROR
          {% endif %}
4 Likes

When I set my alarm on my galaxy s20 it shows my next alarm on HA 5 mins early. I.e. if the alarm is set for 06.30 it would show 06.25 as sensor.phone_next_alarm

Is that something normal? Why does it show 5 mins early on HA?

for samsung yes, they do not use the API appropriately and set random times. All we do is send back the data and the package that set the alarm.

Is this still working for everyone? I don’t know what’s happened but for some reason it’s stopped triggering for me. Even a simple automation like this doesn’t work :frowning_face:

- alias: zzztest
  trigger:
  - platform: time
    at: sensor.lm_g810_next_alarm
  action:
  - service: light.turn_on
    entity_id: light.test_lights

I checked the time in HA is correct, the log doesn’t show anything and the automation is on and runs fine if I trigger it manually. WTH :confused:

I think the reason your alarm didn’t work is the same as mine. The default LG clock app resets the alarm date/time to the new data before HA triggers. This is probably a difference in behaviour to the Google Clock which doesn’t immediately change the next alarm date/time. More info in the github issue I opened.

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.