How to tell when a Sonos Alarm is finished or stopped?

I’m trying to set up an automation that will run when a Sonos alarm ends. I’ve tried tried triggering on the state of the alarm but that actually indicates if the alarm is enabled, not that it’s “running”.

I can see there are some attributes like Time (when it starts) and Duration, I just don’t know how I can add those together to get the end time? I guess if I could do that then I could make it into a template?

Does anyone have an idea how to do this or a better idea?

I’ve figured it out. You can use this template in the Developer Tools to play around with the values from your alarms (replace the alarms name to your entity). You can then make a Template sensor based on those values.

{% set trigger_time_dt = today_at(state_attr("switch.sonos_alarm_150", "time")) %}
{% set trigger_time = as_timestamp(trigger_time_dt) %}

{% set duration_dt = today_at(state_attr("switch.sonos_alarm_150", "duration")) %}
{% set duration = (duration_dt.hour * 3600) + (duration_dt.minute * 60) + (duration_dt.second) %}

{% set end_time = trigger_time + duration %}

Trigger: {{ trigger_time_dt }}
Trigger Time Stamp: {{ trigger_time }}

# Convert the duration value to a date. Sonos alarms can run for 23 hours and 59 minutes so they can't go over a day. We can then extract the hours, minutes and seconds to add to the current date.

Duration: {{ state_attr("switch.sonos_alarm_150", "duration") }}
Duration Date: {{ duration_dt }}
Duration: {{ duration }}

# Remember as_local to convert it back to your current timezone

End: {{ as_local(as_datetime(end_time)) }}
End Time Stamp: {{ end_time }}

This Template gives me an output like follows;

Trigger: 2024-10-02 06:30:00+01:00
Trigger Time Stamp: 1727847000.0

# Convert the duration value to a date. Sonos alarms can run for 23 hours and 59 minutes so they can't go over a day. We can then extract the hours, minutes and seconds to add to the current date.

Duration: 01:30:00
Duration Date: 2024-10-02 01:30:00+01:00
Duration: 5400

# Remember as_local to convert it back to your current timezone

End: 2024-10-02 08:00:00+01:00
End Time Stamp: 1727852400.0
1 Like