How to use next alarm sensor

I can’t test it now since I’m making dinner but I assume you need to compare each alarm against the current time and get the maximum value.
This maximum can only be now at best, so whatever the alarm time should be higher value.
Somehow you need to compare these values then against each other.

Not an easy task if you ask me.

But I’m surprised you can get a time in the past as next alarm.
Sounds like a bug in your phone.

I am using the automation proposed from @Steven_Rollason that stores the next alarm in an input_datetime variable. This automation is not getting triggered, if no alarm is set. So the variable may still carry the value of the last alarm. Guess, in case the comparison is to difficult, an easy solution to the problem will be to create multiple automations using just one time (i.e.

 trigger:
  - platform: time
    at: input_datetime.his_phone_alarm
[...]
 trigger:
  - platform: time
    at: 07:30

with an additional helper boolean condition, that is set to true when the action is triggered, and reset at night, to ensure only one of the automations is triggered at any one day. At least to me this sounds more robust and simple, however it must be possible the otherway aswell, which would be more sophisticated/elegant :thinking:

Edit: To summarize the result would look like this. It will get a bit lenghty and a lot of duplicated code, but I guess it does the job. (Note “Her Phone” is not yet configured in Home Assistant, so her next alarm is not yet being stored)

# Save the time of the upcomming alarm set on his phone
- id: set_his_next_alarm_time
  alias: Set His Next Alarm Time
  description: Save the time of the upcomming alarm set on his phone
  trigger:
  - platform: state
    entity_id: sensor.oneplus_6t_next_alarm
  condition:
  - condition: template
    value_template: '{{ trigger.to_state.state not in ["unknown","unavailable"] }}'
  action:
  - service: input_datetime.set_datetime
    data:
      timestamp: '{{ as_timestamp(trigger.to_state.state) }}'
    entity_id: input_datetime.his_phone_alarm
  mode: single
# Actions to be triggered when his alarm goes off
- id: his_alarm_triggered
  alias: His Alarm Triggered
  description: Opens the shutters when alarm goes off
  trigger:
  - platform: time
    at: input_datetime.his_phone_alarm
  condition:
  - condition: state
    entity_id: input_boolean.is_awake
    state: 'off'
  action:
  - service: cover.close_open
    data: {}
    entity_id: cover.rollladen_buro
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.is_awake
  mode: single
# Actions to be triggered when her alarm goes off
- id: her_alarm_triggered
  alias: Her Alarm Triggered
  description: Opens the shutters when alarm goes off
  trigger:
  - platform: time
    at: input_datetime.his_phone_alarm
  condition:
  - condition: state
    entity_id: input_boolean.is_awake
    state: 'off'
  action:
  - service: cover.close_open
    data: {}
    entity_id: cover.rollladen_buro
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.is_awake
  mode: single
# Actions to be triggered when no alarm is set for the day
- id: no_alarm_set_today
  alias: Start the Day without Alarm
  description: Opens the shutters when no alarm is set for the day
  trigger:
  - platform: time
    at: 07:30
  condition:
  - condition: state
    entity_id: input_boolean.is_awake
    state: 'off'
  action:
  - service: cover.close_open
    data: {}
    entity_id: cover.rollladen_buro
  - service: input_boolean.turn_on
    data: {}
    entity_id: input_boolean.is_awake
  mode: single
- id: reset_is_awake
  alias: '[Reset is Awake]'
  trigger:
  - at: 00:30
    platform: time
  condition: []
  action:
  - service: input_boolean.turn_off
    data: {}
    entity_id: input_boolean.is_awake

Or you make a copy of the automation and just remove the ‘not’ from the condition and action set the datetime to 2099-12-31 or something.
That way the min value should reflect the values that is in future but not unavailable values.

Some good news on this front, the next version of HA will allow you to use the next alarm sensor as a trigger: https://github.com/home-assistant/core/pull/42899

This should make things a lot easier I hope :slight_smile:

6 Likes

hi your sensors can’t be compiled

  • platform: time_date
    display_options:

    • ‘time’
    • ‘date’
    • ‘date_time’
    • ‘date_time_utc’
    • ‘date_time_iso’
    • ‘time_date’
    • ‘time_utc’
    • ‘beat’
  • platform: template
    sensors:
    minutes_next_alarm_paul:
    friendly_name: “Minutes until Next Alarm XXXX”
    unit_of_measurement: ‘m’
    #entity_id: sensor.time
    value_template: >-
    {% set dummy = states(“sensor.time”) %}
    {{((states(‘sensor.mar_lx1a_next_alarm’)|as_timestamp|int - now()|as_timestamp|int)/60)|int}}
    availability_template: “{{ not is_state(‘sensor.mar_lx1a_next_alarm’,‘unavailable’) }}”
    attribute_templates:
    time: “{{ state_attr(‘sensor.mar_lx1a_next_alarm’,‘Local Time’) }}”

can’t recover code

Hi @brano1990,
I am not sure what you mean with “can’t be compiled”. Also I think formatting in your message did not work, so I do not know how to respond to your message.

@ArwynHarris I saw the PR is commited. Do you kwnow how to use this new way of using next alarm sensor?

The docs are always a good place to start :wink:

Thank you :slight_smile: I try with UI and forgot to check the doc.

Hi.
Everything works, but nxt alarm sensor is reporting GMT time, while here in Italy we are now GMT+1.
Time on my mobile is correct, but if I set next alarm at 7a.m., next alarm sensor shows it at 6a.m.
Is there a setting somewhere about this?
Thanks

check that your alarm app has the correct time zone set…we dont do any data manipulation…whatever android sends to us we send back.

What is the state?
My phone report in zulu time as the start and local time in attribute.

Oh… And is it just me that get the very strange text in the app now.
I have it set to english but pretty much every othwr line is in swedish

change the language in App Configuration as well. By default it sticks to device language and it is separate from HA front end as its part of Android.

Also what app are you using that makes it report package: unknown?

here is mine

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

then i have this automation

- 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:
  - data:
      entity_id: switch.kettle_power
    service: switch.turn_on
  - delay: '00:00:50'  
  - data:
      entity_id: light.his_side
    service: light.turn_on
  - choose:
      - conditions:
          - condition: state
            entity_id: sensor.day_night
            state: 'Night'
        sequence:
          - service: light.turn_on
            entity_id: light.kitchen
  mode: single

hope this helps

1 Like


I’m only talking about app language.

I use the stock alarm app on my phone. Don’t know much more than that :confused:

Timezone is correct, it’s the alarm that I use everyday.
I’ll do some more tests later.
Before going to bed alarm was showing 1 hour in advance, but this morning it was correct.

I guess we will still have to use a template if we want the next alarm time +/- ?
I tried offset: "-00:30:00" like the sun trigger but that didn’t work.

I think next_alarm sensor is not working well, at least on my Xiaomi MI9T Pro.

If I set alarm at 15:00, sensor shows 14:45
if I set 15:20, it shows 15:05
if I set 15:30, it shows 15:15
if I set 16:00, it shows 15:00
if I set 17:00, it shows 16:00

I can’t understand it

1 Like

now I set again 15:00, and it’s correctly showing 15:00
but also setting 16:00 shows 15:00
weird…

Search the forum, users who use xiaomi apps have the same issue. My recommendation stays the same, use another app that makes better use of the API.