How to use next alarm sensor

Wow , this is exactly what I’m looking for , I added it in my HA.
I removed the 30 minutes -1800 though , want the lights on @ alarmtime

I only have 2 issues , my wifes alarm time when she dismisses an alarm shows negative values , like - 2 hours , 25 minutes untill next alarm. while I have an alarm set at a later timepoint.

and I sometimes have an alarm set an hour apart from my wife and it doesnt switch to my alarm when wife dismisses hers ?

is the negative value an alarm app feature or a template thing ?
and is there a certain HA app update interval for it not to take my alarm time ?

After trying various solutions from this thread, a standard automation is actually already also working when you use the attribute “Local Time”:

alias: 'Alarm: Turn lights ON'
description: ''
trigger:
  - platform: state
    entity_id: sensor.samsung_s10_next_alarm
    attribute: Local Time
condition: []
action:
  - service: light.turn_on
    target:
      area_id: bedroom
mode: single

2 Likes

did you not try the example up above it can be even simpler :slight_smile:

I tried my solution before reaching the end of this topic :grinning:. Just wanted to mention it then as another possibility.

But in the GUI mode, HA give me no entities back, when I use the Time trigger :thinking: in YAML mode it is fine.

really? It should be picking that up there. Might be worth opening a frontend issue because it should pick up any sensor with device_class: timestamp as its attribute

Hmm the entity has that attribute. I will check this more closely tomorrow.

I have now created an issue :slightly_smiling_face: No entities with "timestamp" attribute listed when using "Time" trigger in automations · Issue #10356 · home-assistant/frontend · GitHub

I haven’t tried but from previous experiments I can’t see this working properly because the next alarm includes calendar events. So if I have eg. A birthday calendar event before the set alarm it is getting the date time from there. I’m playing about with a template sensors to try define the two using

{% if 'calender' in (state_attr('sensor.s10_next_alarm', 'Package' )) %} {{ states('sensor. S10_next_alarm' }} {% endif %}

This is grabbing the alarm clock value only, now I just need to get it into a date time helper and hopefully I can simply use that as the trigger.

Here is a solution I found using one input datetime helper and one automation:

alias: Set Alarm Input Helper
description: ''
trigger:
- platform: state
entity_id: sensor.s10_next_alarm
condition: []
action:
- service: input_datetime.set_datetime
data_template:
entity_id: input_datetime.alarm_clock
datetime: >-
{% if 'clock' in (state_attr('sensor.s10_next_alarm', 'Package' )) %} {{
states('sensor.s10_next_alarm') |as_datetime |as_local }} {% endif %}
mode: single

Now I just need to create an automation that is triggered by time and the helper is available to me in the UI - so I tell lies I need 2 automations :laughing:
Thanks for all the info, this thread helped me to see some different options :slight_smile:
Happy 2022

And this automation worked to flash my light when the alarm triggers then turn it off when I stop the alarm. Hopefully this holds up -

alias: S10 Alarm
description: ''
trigger:
  - platform: time
    at: input_datetime.alarm_clock
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.lounge_light_0
    data:
      effect: Slow Pulse
  - wait_for_trigger:
      - platform: state
        entity_id: sensor.s10_next_alarm
  - type: turn_off
    device_id: 2d504a953e3e2f1b9f8c0922848f513e
    entity_id: light.lounge_light_0
    domain: light
mode: single

Edit/ update: I encountered another problem here, if the snooze is set then the snooze time is added to the input helper even tho I push dismiss alarm on the phone. With the snooze turned off in the android alarm settings it works but I need to turn the flashing light off by another means. I could probably use a condition or boolean but I’m reasonably happy to turn my light automation off with a timer for now.

I’m facing same problems with my Xiaomi Phone.
I finally solved all of them removing com.xiaomi.calendar with this guide: Xiaomi Bloatware List | Remove Bloatware on MIUI 12/11/10
Now my next_alarm sensor works as expected.

1 Like

Hi all!
I’ve read several posts and topics without finding a solution for my issues.
I don’t have a regular wake-up time. Some days I wake up at 2:30am, others 4:30am, others 8am, so on…
I would like to set my alarm on mobile phone and based on “sensor.s8_bt_next_alarm” I would trigger automations BEFORE the alarm sounds ON.

For example:
I set my alarm for 2:30am.
At 2:00am (sensor.s8_bt_next_alarm MINUS 30min) turn on toilet heater.
At 2:20am (sensor.s8_bt_next_alarm MINUS 10min) turn on air conditioner on dressing room.
At 3:00am (sensor.s8_bt_next_alarm PLUS 30min) turn on air conditioner on kitchen.
At 3:10am (sensor.s8_bt_next_alarm PLUS 40min) turn off toilet heater.
At 3:15am (sensor.s8_bt_next_alarm PLUS 45min) turn off air conditioner on dressing room.
At 3:30am (sensor.s8_bt_next_alarm PLUS 60min) turn off air conditioner on kitchen.

Mainly I understand that I need to convert my “sensor.s8_bt_next_alarm” to “minutes to next alarm” and the trigger automations based on those “minutes to next alarm”.
For that I added this on configuration.yaml, check after line 107:


But it returns “Configuration invalid”:

Maybe I’m not identing it well…
Any help would be appreciated.
Tks in advance

Hi,

This is almost what I am looking for. It works as expected however one minor problem. When I disable alarm, helper entity value remains so even if alarm is disabled automation triggers. I removed if statement to set it to unavailable but it doesnt change the value. Do you have any idea how to do?

Edit: I added a NOT condition to my automation. if the next alarm state equals unavailable, light wont turn on.

My alarm trigger is quite often not working. My alarm was set to 7.30, as you can see in the screenshot, one second before the alarm should be triggered, it changes to unavailable.

Do others have experienced this as well? And is there a work around to get the trigger working reliable?

NEXT_ALARM

trigger:
  - platform: time
    at: sensor.phone_wietse_next_alarm
  - platform: state
    entity_id:
      - group.persons
    to: 'home'

condition:
- condition: and
  conditions:
  - condition: state
    entity_id: group.persons
    state: 'home'
  - condition: not
    conditions: 
      - condition: state    # If media player is already on, don't execute
        entity_id: media_player.lg_sl10yg
        state: 'playing'

action:
  - service: script.play_radio_sb
  
mode: single

This does not happen to me, but I think the reason is that your phone and HA are not exactly synced in time or the phone deletes the alarm just before it rings.

I would solve it with a template trigger to trigger 10-15 seconds before the alarm.
If it’s important that they start at the same time as the alarm then delay the automation in the actions first.

{{ as_datetime(states('sensor.phone_wietse_next_alarm')) - timedelta(seconds=10) }}
1 Like

Sorry, I thought this thread was stale. I had to set a dummy alarm. eg. I set an android alarm for 2026 or something so that it falls back to that. It’s not elegant at all but I think I seen mention of something alarm clock related in a recent homeassistant update; I am about to get back to investigating a better solution and will keep you posted :grinning:
The other issue was if an android timer was set it triggerd the automation.

I was trying the change you suggested, but it gave me the following error. Can you tell me what i’m doing wrong?

trigger:
  # - platform: time
  #   at: sensor.phone_wietse_next_alarm
  - platform: template
    value_template: "{{ as_datetime(states('sensor.phone_wietse_next_alarm')) - timedelta(seconds=10) }}"
  - platform: state
    entity_id:
      - group.persons
    to: 'home'
2022-03-30 07:29:59 WARNING (MainThread) [homeassistant.components.template.trigger] Error evaluating 'template' trigger for 'Alarm Turn on Radio': TypeError: unsupported operand type(s) for -: 'NoneType' and 'datetime.timedelta'

That’s a warning that you did not have an alarm set (I think).
I’m not sure how to get around it but it shouldn’t make much difference.

1 Like

The major issue with your template is that you aren’t comparing your alarm value to the current time. Without a comparison your template will never trigger the automation. From the docs:

The trigger will fire if the state change caused the template to render ‘true’ (a non-zero number or any of the strings true , yes , on , enable ) when it was previously ‘false’ (anything else).

The warning is due to either a typo in your template or the phone not having an alarm set. You can use an if: clause to get rid of the warning caused by not having an alarm set:

trigger:
  - platform: template
    value_template: >-
      {% set x = states('sensor.phone_wietse_next_alarm') %}
      {% if x not in ['unavailable', 'none', 'unknown'] %} 
      {{ now() >= x | as_datetime | as_local - timedelta(seconds=10) }}
      {%else%} False {% endif %}
1 Like

Thank you for the response. Your template is working, only needed to change the “timedelta” to 1 minute. I think it has to do with the “now()” updating only once a minute.

For others who want to use the working template, this one is working:

trigger:
  - platform: template
    value_template: >-
      {% set x = states('sensor.phone_wietse_next_alarm') %}
      {% if x not in ['unavailable', 'none', 'unknown'] %} 
      {{ now() >= x | as_datetime | as_local - timedelta(minutes=1) }}
      {%else%} False {% endif %}