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
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.
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.
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
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