Sonos Alarm Switch

EDIT: This custom integration is now part of the SONOS core integration.

SONOS alarms are quite hard to use with the default sonos integration.
They can be quite easy to track if you integrate this custom_component!

The sonos_alarm component discovers automatically every alarm in your SONOS Network and creates a switch that lets you toggle alarms or track your alarms (attributes show details about your alarm).

Available on HACS or GitHub - AaronDavidSchneider/SonosAlarm: HomeAssistant custom component to control your SONOS Alarm

No configuration needed, just download it to the custom_component folder (or let HACS do this for you) and search for Sonos Alarm in your integrations panel.

3 Likes

Is there any way to add an alarm?

Hello @devastator,
due to the underlying library and its limitations you need to add an alarm in your sonos app.
Once you added it there, it will be discovered by this component.

Is it possible to use the time attribute as trigger in an automation? I’m trying to use as_timestamp filter to remove the seconds without success. Can you help me? Thanks in advance

You could use masking. Here is an example:

{{state_attr('switch.sonos_alarm_1',"time")[:2]}}

which gives the hour or

{{state_attr('switch.sonos_alarm_1',"time")[3:4]}}

which gives the minutes.

1 Like

Thank you, I’m using it like this:

- platform: template
  sensors:
    sonos_alarm_hours:
      friendly_name: 'Sonos Alarm Hours'
      entity_id: switch.sonos_alarm_1
      value_template: '{{state_attr("switch.sonos_alarm_1","time")[:2]}}'

- platform: template
  sensors:
    sonos_alarm_minutes:
      friendly_name: 'Sonos Alarm Minutes'
      entity_id: switch.sonos_alarm_1
      value_template: '{{state_attr("switch.sonos_alarm_1","time")[3:5]}}'

I save the values inside an input_datetime:

- id: Set_Input_date_time_sonos_alarm
  alias: Set Alarm time for Sonos Wake Up Alarm
  trigger:
    platform: state
    entity_id: sensor.sonos_alarm_time
  action:
  - service: input_datetime.set_datetime
    entity_id: input_datetime.sonos_alarm
    data_template:
      time: "{{states('sensor.sonos_alarm_hours')}}:{{states('sensor.sonos_alarm_minutes')}}:00"

And use it to turn on an input_boolean for motion sensor automations when the alarm is triggered:

- id: Turn_on_motion_lights_after_Wake_Up_Alarm
  alias: Turn on motion lights after Wake Up Alarm
  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == states('sensor.sonos_alarm') }}"
  condition:
    condition: state
    entity_id: input_boolean.motion_lights
    state: 'off'
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.motion_lights 
2 Likes

And the alarm time displayed in lovelace:
image

Looks good! I personally prefer AppDaemon for automations. I therefore created an app that does similar things (https://github.com/AaronDavidSchneider/SonosAlarmAutomation)

1 Like

is there a way to combine your script with hue wake up routines?

I don’t see why this wouldn’t be possible. However, I do not have hue lights and can therefore test nothing.
Maybe its even possible to use the input_numbers that are needed for this script for your hue wake up routine.

I am a total beginner when it comes to HA. I installed your script via HACS and now have the Lovelace to turn on/off my Sonos alarms. This works very well but i don‘t know how to access this.

I looked into the folders which were installed but there are no yaml files. How can i access the code so when i turn one Sonos alarm another script (for hue wake up) will run also?

Hi, no problem!
You can simply create an automation that is triggered when the state of the alarm switch has been changed. Find more about triggers here: https://www.home-assistant.io/docs/automation/trigger/

@AaronDavidSchneider with core release 2021.6 is this custom integration now pulled into the core?

Yes indeed!

1 Like

@AaronDavidSchneider awesome! thanks for your fab work :grinning:

1 Like

@AaronDavidSchneider thanks for your work with the component. I have Home Assistant running on Docker, and for some unknown reasons alarm switches were not generated using the official SONOS component. Now it works.

However moving from Openhab to Home Assistant I was missing a significant part in the SONOS integration, which was available on Openhab. “Alarm triggered by Sonos Player XYZ”. With that you could do easy automation with all other devices such as lights and especially for my kids the activation of window blinds after five minutes.

With Home Assistant it was more difficult to get into that state, everyone is still adding and deleting Alarms on Sonos App. So I used this template which searches for all Alarm configured for a certain player and if scheduled today is true, it will check if Alarm Time and Time now() is equal.

{% set ns = namespace(total = 0) -%}
{% for entities in states.switch -%}
{% if entities.attributes | regex_search(’^.Alarm Schlafzimmer.$’) and
entities.attributes.scheduled_today == True -%}
{%- set seconds = strptime(entities.attributes.time, ‘%H:%M:%S’).second -%}
{%- set minutes = strptime(entities.attributes.time, ‘%H:%M:%S’).minute -%}
{%- set hours = strptime(entities.attributes.time, ‘%H:%M:%S’).hour -%}
{%- set alarmtime = ‘{:02}:{:02}’.format(hours, minutes) -%}
{%- if alarmtime == ‘{:02}:{:02}’.format(now().hour, now().minute) -%}
{% set ns.total = ns.total + 1 -%}
{% endif -%}
{% endif -%}
{% endfor -%}
{{ns.total > 0}}

Trigger is Sonos Player starts playing,
Template above must be true
Action what ever you desire