How to use next alarm sensor

I’m trying to incorporate the next alarm sensor into my HA alarm clock but cannot for the life of me figure out a template that works using the HA date time sensor. Any help with this?

5 Likes

This one uses the sensor date time and compares it to the alarm time and triggers on the time.
This uses Hassalarm but the template should be the same.

{{ states('sensor.date_time') == (state_attr('input_datetime.next_alarm', 'timestamp') | int | timestamp_custom('%Y-%m-%d, %H:%M', True)) }}

This is a template that will check if the current time is equal to the time of the next alarm sensor in the app. You see 2 times 0*60 in the template, this makes it possible that you can have a little offset with the time, if you make the 0 to a 1 it will add one min to the time and of course this can be as high as you want. The first 0*60 if you change this you will add something to the current time and if you change the second you will add some min to the next_alarm sensor. (This does not affect the sensor or current time itself)

"{{ (((as_timestamp(now()) | int) + 0*60) | timestamp_custom('%Y-%m-%d %H:%M:00')) == (((state_attr('sensor.Your_phone_name_next_alarm', 'Time in Milliseconds') | int / 1000) + 0*60 ) | timestamp_custom('%Y-%m-%d %H:%M:00')) }}"

If there is something not clear just get back at me and i’ll do my best to explain it

3 Likes

I’ve tried your solution and it does seem to work as far as generating the same numbers for the date and time but I can’t get the sensor to show true. I split the template in 2 pieces and ran them through the template editor in the developer options and notice that the alarm timestamp is spaced differently than the current timestamp.

I was able to get it to work by using this template.

{{now().strftime("%a %h %d %H:%M %Z %Y") == (((state_attr('sensor.joes_galaxy_s10_next_alarm', 'Time in Milliseconds') | int / 1000) + 0*60 ) | timestamp_custom('%a %h %d %H:%M %Z %Y'))}}

and this for a binary sensor. Hopefully this will help someone else in the future

- platform: template
  sensors:
    joes_next_alarm:
      entity_id:
        - sensor.joes_galaxy_s10_next_alarm
        - sensor.time
      friendly_name: "Joes Next Alarm"
      value_template: >-
        {{now().strftime("%a %h %d %H:%M %Z %Y") == (((state_attr('sensor.joes_galaxy_s10_next_alarm', 'Time in Milliseconds') | int / 1000) + 0*60 ) | timestamp_custom('%a %h %d %H:%M %Z %Y'))}}

11 Likes

Thanks - this worked beautifully for me using the MotoG7

:+1: :grinning:

So I’ve noticed that the sensor that I created above only triggers maybe 2 times out of 20. Any ideas on how to get a more accurate sensor made?

A wild guess, but try and remove the seconds on both sides.

1 Like

:man_facepalming: I can’t believe that didn’t cross my mind, thank you very much!

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') }}"
17 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:
      ...

4 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…

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