Sonos Door and Window Alerts from Home Assistant

Published on Sunday 01 November 2020
Categories: Home Automation |

Home Assistant

I the past I have used the open source smart home software called Home Assistant on a Raspberry Pi to monitor our homes sensors which has information from our temperature sensors, electric and gas meters.

I then changed to uploading the data to a custom website at after problems with the Home Assistant software corrupting the database and losing historic data.

Last month I decided to try Home Assistant again on a new Raspberry Pi 4, and found that the software was much more polished and refined and far easier to detect the devices on our home network including the Sonos speakers and Synology NAS drives.

Sensors I also purchased a Zigbee USB dongle and a door sensor (Aqara door & window contact sensor) earlier in the year and tried using this on the Raspberry Pi and after installing a Home Assistant addon called Zigbee2MQTT, it detected the door sensor and automatically added it to the Home Assistant environment.


We decided to purchase additional sensors and install them on the front and back doors and the two Velux windows in the loft workshop.

Home Assistant has a section called Automations which allows you to setup actions which are based on a trigger state, this can be a time trigger or other action such as a sensor or switch being in a certain state or other trigger. A condition such as a time period or other requirement and this can then run an action such as sending an alert to a mobile device or other network device.

As we have Sonos smart speakers in the home I decided to create an automation to alert us if the loft windows are left open after a set time of the day with a spoken message on the speaker and also alert on the mobile phone.

The full details for adding automations are on


If you have the file manager add-on installed or Samba access you can edit the “automations.yaml” file directly and paste the following into the file. Customise the device names to match your devices.

- id: '123456789123'
  alias: Loft Left Window Open
  description: ''
  - platform: state
    entity_id: binary_sensor.loft_window_one_contact
    to: 'on'
  - condition: time
    after: '20:00'
  - service: notify.mobile_app_yourphone
      message: Loft Left Window is open
      title: Loft Left Window is open
  - service: script.sonos_say
      sonosdevice: your-sonos-device
      message: Loft Left Window is open
  mode: single

You will need to duplicate this script to work with a second window or door sensor if required.

You need to add a script to the “scripts.yaml” file to communicate with the Sonos speaker.

This is a modified version of the demo script on which adds automatic length detection for the spoken words.

  alias: Sonos TTS script
  - service: sonos.snapshot
      entity_id: media_player.{{ sonosdevice }}
  - service: sonos.unjoin
      entity_id: media_player.{{ sonosdevice }}
  - service: media_player.volume_set
      entity_id: media_player.{{ sonosdevice }}
      volume_level: 0.3
  - service: tts.google_translate_say
      entity_id: media_player.{{ sonosdevice }}
      message: '{{ message }}'
      cache: false
  - delay:
      seconds: 1
  - delay: >-
      {% set duration = states.media_player[sonosdevice].attributes.media_duration %}
      {% if duration > 0 %}
        {% set duration = duration - 1 %}
      {% endif %}
      {% set seconds = duration % 60 %}
      {% set minutes = (duration / 60)|int % 60 %}
      {% set hours = (duration / 3600)|int %}
      {{ [hours, minutes, seconds]|join(':') }}
  - service: sonos.restore
      entity_id: media_player.{{ sonosdevice }}
  mode: single  

After restarting the Home Assistant software, the new automation should be functioning.



Leave a comment

About Me

Brian Dorey

Welcome to my blog, here you will find my projects and other things.
I make websites and manufacturer and sell expansion boards for the Raspberry Pi range of computers.

AB Electronics Raspberry Pi shop

Social Media

Popular Posts

Small image for Raspberry Pi Zero W external antenna mod

Raspberry Pi Zero W external antenna mod
04 MARCH 2017

Small image for Ford Sync 3 USB Drive Music Playlists

Ford Sync 3 USB Drive Music Playlists
22 OCTOBER 2016

Small image for Echo Dot 3rd Gen Smart speaker Teardown

Echo Dot 3rd Gen Smart speaker Teardown
21 JULY 2019

Small image for DIY Pick and Place V2 Project Complete

DIY Pick and Place V2 Project Complete
23 NOVEMBER 2013