Published on
Sunday 01 November 2020
Categories:
Home Automation |

I the past I have used the open source smart home software called Home Assistant www.home-assistant.io 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 home.briandorey.com 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.
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 www.home-assistant.io/docs/automation.

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: ''
trigger:
- platform: state
entity_id: binary_sensor.loft_window_one_contact
to: 'on'
condition:
- condition: time
after: '20:00'
action:
- service: notify.mobile_app_yourphone
data:
message: Loft Left Window is open
title: Loft Left Window is open
- service: script.sonos_say
data:
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 www.home-assistant.io/cookbook/sonos_say which adds automatic length detection for the spoken words.
sonos_say:
alias: Sonos TTS script
sequence:
- service: sonos.snapshot
data:
entity_id: media_player.{{ sonosdevice }}
- service: sonos.unjoin
data:
entity_id: media_player.{{ sonosdevice }}
- service: media_player.volume_set
data:
entity_id: media_player.{{ sonosdevice }}
volume_level: 0.3
- service: tts.google_translate_say
data:
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
data:
entity_id: media_player.{{ sonosdevice }}
mode: single
After restarting the Home Assistant software, the new automation should be functioning.
Permalink