Sonos Alarm Trigger

Hello,

I’m very new to HA and i didn’t really find an answer for my automation purpose. It shouldn’t be too difficult i think, sorry if this has already been replied.
What i want to do is use the Sonos Alarm function as a trigger to start an automation for a gradual increasing volume, as well as gradually lighting up my Nanoleaf lights.
I have no problem with the ascending volume+light automation itself, but i cannot find a way to have a correct trigger.
There is no simple “Alarm Start” trigger in Sonos integration, only Alarm on/off switch or Sonos Starts Playing. The problem is that i want the automation to run only when it is an alarm, not when i play during the day…
So i tried to create an automation with Sonos Playing trigger + a time condition to check that the systems starts around the alarm time. With the “Time” condition i don’t know how to check the sonos alarm value, it only seems to accept fixed values. With the “Numerical” condition i can find the sonos_alarm time value but i don’t understand how to format the “before” and “after” values to say for example “sonos_alarm_time + 5min” and “sonos_alarm_time - 5min” as min-max values.
I don’t know if i’m clear…

Sorry if i’m a total noob to this and thanks for your help!

Based on what I can find, there’s nothing in the Sonos integration that reports when an alarm occurs. Each alarm time appears as a switch but that’s for enabling/disabling the alarm’s schedule.

I used Developer Tools > Events to see what kind of events are produced at the moment the alarm occurs, in the hope of spotting a Sonos-specific event that uniquely identifies the alarm. However, a flurry of events occur and all were related to media_player changing state, volume changes, etc. Basically, the usual stuff when a media_player starts playing but nothing to indicate it was initiated by a scheduled alarm.

To be fair, maybe there is such an event and I simply failed to see it in the list. Dozens of events occurred and the oldest ones scrolled away and were eliminated from the list.

You might want to post it as a Feature Request. jjlawren maintains/enhances the Sonos integration and has been open to other people’s suggestions.

The switch representing the alarm contains an attribute called time. Its value is a string like 07:00:00.

If the value of the recurrence attribute is DAILY, you can make a Template Sensor with a timestamp device_class to represent the alarm time.

For example, here’s the Template Sensor:

template:
  - sensor:
      - name: Sonos Alarm Example
        device_class: timestamp
        state: "{{ today_at(state_attr('switch.sonos_alarm_whatever', 'time') }}"

which can then be used in an automation with a Time Trigger:

alias: example
trigger:
  - platform: time
    at: sensor.sonos_alarm_example
condition: []
action:
  ... etc ...

Things get a bit more complicated if recurrence is not simply DAILY.

Thanks a lot for your answer!

Indeed that’s what i thought about the alarm not sending a status thru the Sonos integration.
And the solution proposed would be problematic because i actually doesn’t have the alarm on a daily basis.
That’s why i was more thinking of using the Media Play Start event as a trigger (which works but is just not selective enough to catch only alarm play) and add a time condition to allow the action to start only if current time is between ± 5minutes of the alarm switch time. Do you think i could then used the Template Sensor you proposed to compare with current time in the Condition of Automation?
Thanks

In that case, the strategy is to make the template interpret the value of recurrence. I mentioned it was a bit more complicated because this is what it looks like when Repeat is set to Monday, Tuesday, Thursday, and Friday:

ON_1245

It reports WEEKDAYS when Repeat is Monday through Friday. It’s WEEKENDS for Saturday and Sunday. For a single day, like just Sunday, it’s ON_0.

In other words, it reports weekdays as integers where Sunday is 0 and Saturday is 6. The exception is when Repeat is a contiguous range of all weekdays or the entire weekend.

Based on that information, the template can parse the value of recurrence and determine if the current weekday, using now().strftime('%w'), matches it.

Let me know if you want to pursue this approach and I can help you construct the template.

Assume that Repeat is set to weekends and Time is 07:00:00. If anyone manually turns on the speaker around the same time on a weekday, the Template Condition you proposed will allow it and assume the Sonos speaker’s alarm had activated (because the condition is only checking the time, not the day of the week).

What I proposed above is to also take the value of recurrence into account.

Scratch all of that! I just realized that there’s an existing attribute, scheduled_today, that informs you if the alarm is scheduled to occur today (by indicating true or false). :man_facepalming:

You can simply use it in a Template Condition.

alias: example
trigger:
  - platform: time
    at: sensor.sonos_alarm_example
condition: "{{ state_attr('switch.sonos_alarm_whatever', 'scheduled_today') }}"
action:
  ... etc ...

Better yet, you can dispense with the Template Sensor and just do everything in a single Template Trigger:

alias: example
trigger:
  - platform: template
    value_template: >
      {% set s = 'switch.sonos_alarm_whatever' %}
      {{ state_attr(s, 'scheduled_today') and now().replace(microsecond=0) == today_at(state_attr(s, 'time') }}
condition: []
action:
  ... etc ...
2 Likes

Sorry for the late reply but i was able to test it only today using the Template Condition and Sensor and it works like a charm, thanks a lot!
I only had to add a second condition because not only my alarm is not on daily basis but also it changes from one week to another. So basically i put alarm on/off regarding the situation. And the ‘scheduled today’ attribute doesn’t care of the alarm being put on or off, it only cares on the days selected on the setup of the alarm.
But for the rest that’s perfect! Thanks again!

1 Like