I’ve got home assistant monitoring the state of my sous vide cooker, and I’d like to send notifications when it reaches cooking temp. But it only makes sense to send that notification to people who are at home. I’m planning on implementing the same logic for other things - if I’m at home and my partner’s at work there’s no point her getting a notification that the washing machine has just finished its cycle. So I wrote a python_script to iterate through a device group, and only send notifications to devices that are at home.
I’m using the html5 push notifier, and gave all of my device trackers the same name as the devices in my html5_push_registrations.conf file. That means that I can just grab a device name, and get its corresponding notifier with some trivial string manipulation. My final script looks like this:
# Iterate through group.all_devices, and only send a notification
# to that device if it is at home.
# For this to work, it requires that device names match notifier names.
# In the case of the HTML5 push notifier, it means that device names
# in known_devices.yaml must match names in html5_push_registrations.conf
device_group = data.get('group', 'group.all_devices')
location = data.get('location', 'home')
title = data.get('title')
message = data.get('message')
service_data = {'title': title, 'message': message}
for entity_id in hass.states.get(device_group).attributes['entity_id']:
name = entity_id.split('.')[1]
state = hass.states.get(entity_id)
if state.state == location:
service_name = 'push_notify_{0}'.format(name)
hass.services.call('notify', service_name, service_data, False)
It only supports the most basic required attributes, but should be trivial to add more optional ones.
Hello @stibbons, I’ve done some minor changes to your script in order to update it to last version of HA and add a little functionality:
Added support for the data structure of the notification platform.
Added support for notifier_name which defaults to html5.
Made device_group required because group.all_devices has been deleted in 0.104.
Added services.yaml with the documentation of the new service.
Still pending:
Make optional data and title parameters.
Regards,
Carles
# python_scripts\notify_by_location.py
# Iterate through the devices of the group parameter
# and only send a notification to that device if it is at home (or at
# location parameter).
# For this to work, it requires that device names match notifier names.
# In the case of the HTML5 push notifier, it means that device names
# in known_devices.yaml must match names in html5_push_registrations.conf
message = data.get('message')
title = data.get('title') #TODO: make it optional
html5_notification_data = data.get('data') #TODO: make it optional
device_group = data.get('group')
location = data.get('location', 'home')
notifier_name = data.get('notifier_name', 'html5')
service_data = {'title': title, 'message': message, 'data': html5_notification_data}
for entity_id in hass.states.get(device_group).attributes['entity_id']:
name = entity_id.split('.')[1]
state = hass.states.get(entity_id)
if state.state == location:
service_name = '{0}_{1}'.format(notifier_name, name)
hass.services.call('notify', service_name, service_data, False)
#python_scripts\services.yaml
notify_by_location:
description: Sends an HTML5 PUSH notification to all the devices detected in home. The device ID in html5_push_registrations.conf and known_devices.yaml must be the same
fields:
message:
description: Body of the notification.
example: Notification message
title:
description: Title of the notification.
example: Notification title
data:
description: All the parameters supported by the notification platform used in YAML format.
example:
group:
description: The group of devices to take into account.
example: group.my_devices
location:
description: Optional. The location where devices has to be in order receive the notification. Defaults to home.
example: work
notifier_name:
description: Optional. The notifier name. Defaults to 'html5'.
example: my_notifier