@aimc I took your awesome template and app and put it to work tonight. But I also added tts with it for my sonos. I did this in SmartThings with the webcore component to speak announcements for my smoke alarms. Worked great for my kiddos. I have this one setup to fire off the initial announcement and then it will repeat itself every 30 seconds as long as the condition remains. Thank you.
import appdaemon.plugins.hass.hassapi as hass
#
# App to send notification when a sensor changes state
#
# Args:
#
# sensor: sensor to monitor e.g. sensor.upstairs_smoke
# idle_state - normal state of sensor e.g. Idle
# turn_on - scene or device to activate when sensor changes e.g. scene.house_bright
# Release Notes
#
# Version 1.0:
# Initial Version
class SensorNotification(hass.Hass):
def initialize(self):
if "sensor" in self.args:
for sensor in self.split_device_list(self.args["sensor"]):
self.listen_state(self.state_change, sensor)
self.media_player = 'group.speakers'
def state_change(self, entity, attribute, old, new, kwargs):
self.log("{} is in state {}".format(self.friendly_name(entity), new))
#self.notify("{} is in state {}".format(self.friendly_name(entity), new), name="ios")
if new != self.args["idle_state"] and "turn_on" in self.args:
self.turn_on(self.args["turn_on"])
if new == "Fire":
while new == "Fire":
self.fire_announcement(deviceID=entity)
self.fire_handle = self.run_in(self.fire_announcement, 30, deviceID=entity)
else:
self.cancel_timer(self.fire_handle)
if new == "CO2":
while new == "CO2":
self.co2_announcement(deviceID=entity)
self.co2_handle = self.run_in(self.co2_announcement, 30, deviceID=entity
else:
self.cancel_timer(self.fire_handle)
def fire_announcement(self,kwargs):
friendly_name = self.get_state(kwargs['deviceID'], attribute='friendly_name')
message = "The {} has detected smoke. Please exit the house immediately through the safest path." .format(friendly_name)
self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 100)
self.call_service("tts/google_say", entity_id=self.media_player, message=message)
def co2_announcement(self,kwargs):
friendly_name = self.get_state(kwargs['deviceID'], attribute='friendly_name')
message = "The {} has detected carbon monoxide. Please exit the house immediately through the safest path." .format(friendly_name)
self.call_service("media_player/volume_set", entity_id=self.media_player, volume_level = 100)
self.call_service("tts/google_say", entity_id=self.media_player, message=message)
edit: I commented out the notify for the moment while I’m still figuring out which notification platform I want to use. I’m probably also going to do a web call with IFTTT to call my cell phone and my wifes if we’re not home.