I have been using the Alexa Media component as a TTS delivery mechanism
and the Alexa App routines function, to ask Alexa about the state of various entities
Edit updated program to render Grammatical sentences (unlike me )
EDIT replaced this program with
For example, Alexa is the patio door open.
I have been using individual input boolean to trigger these responses. This has proved cumbersome. I finally hit upon the idea of using a dummy light bulb. This allows the Alexa app to use the brightness levels and gives a 100 triggers with one entity. To create a dummy bulb in HA you need to create a blank light template
- platform: template
lights:
alexa_virtual:
friendly_name: "Alexa Dummy Light"
turn_on:
turn_off:
set_level:
This bulb can then be exposed to Alexa via Alexa Cloud or emulated Hue.
I have written the following Appdeamon script to respond to the trigger event from the dummy bulb
#
# This program responds via ALexa TTS to trigger events generated by the Alexa App routines function.
# It responds with generated spoken description of state of the triggered entity.
#
# It requires a template light switch is created in HA
#
# - platform: template
# lights:
# alexa_virtual:
# friendly_name: "Alexa Dummy Light"
# turn_on:
#
# turn_off:
#
# set_level:
#
# This dummy light switch brightness level is used to generate 100 unique triggers.
# Each trigger is matched to a list of entities to create the following type of Alexa conversations
#
# "Alexa is the patio door open" (created in the Alexa app Routine)
# "Please wait while I check" (created in the Alexa app Routine)
# Set dummy Light bulb brightness to 1% (created in the Alexa app Routine)
# "The patio door is closed" (created by this app)
#
# Program requires the following parameters
#
#
# AlexaReport:
# module: AlexaReport
# class: Alexareport
# trigger: < Enity name of virtual bulb>
# device: < Entity name of Alexa media device
# ent:
# - < Entity of sensor to obtain state >
# - < Next entity >
# - < and so on >
#
# 1st entiy is triggered on brightness of 1% second on 2% etc
# Version 1.1 Added better English
#
#
#
#
#
import appdaemon.plugins.hass.hassapi as hass
class Alexareport(hass.Hass):
def initialize(self):
# get vars
self.trigger=self.args["trigger"]
self.alexa = self.args["device"]
# check if dummy bulb triggered
self.listen_state(self.alexareport,self.trigger,new="on")
def alexareport (self, entity, attribute, old, new, kwargs):
self.log(("{} triggered").format(self.trigger))
#setup list for entities
ent = []
# dim value of dummy light
value = self.get_state(self.trigger, attribute = "brightness")
# Convert brightness 0 to 255 as persentage then -1 as python list start 0 not 1
psent = int(round(value/255*100)) -1
#get entity id from args and add to ent list
for entity in self.args["ent"]:
ent.append(entity)
# Test if dim value matches listnumber in array -1 as array start at 0
if psent > (len(ent) -1) :
# Dim value does not match entry in list error and turn off dummy light
self.log("Message {} value out of range ignored".format(psent+1))
self.turn_off(self.trigger)
else:
# get entity details
obj = ent[psent]
fname = self.friendly_name(obj)
state = self.get_state(obj)
uom = self.get_state(obj, attribute = "unit_of_measurement")
dc = self.get_state(obj, attribute = "device_class")
# if values are null set as blank to stop errors when concatenating strings
if uom == None :
uom = ""
if dc == None :
dc = ""
# check if entity is plural or singular (is or are)
if fname.endswith("s" ):
sorp = " are "
else :
sorp = " is "
#construct alexa reply
mess = "The " + fname + sorp + dc + " " + state + " " + uom
# Check entity type to change on/off to appropriate values. Add as required
mess = mess.replace("opening on","open")
mess = mess.replace("opening off","closed")
mess = mess.replace("moisture on","wet")
mess = mess.replace("moisture off","dry")
mess = mess.replace("motion on","triggered")
mess = mess.replace("motion off","off")
mess = mess.replace("temperature","")
mess = mess.replace("humidity", "")
mess = mess.replace("pressure","")
# replace symbols and SI with words. Add as required
mess = mess.replace(" "," ")
mess = mess.replace("°C","Centagrade")
mess = mess.replace("km","Kilometres")
mess = mess.replace("GiB","Gigabytes")
mess = mess.replace("MiB","Megabytes")
mess = mess.replace("lx","Lux")
mess = mess.replace("%","Percent")
mess = mess.replace("mbar","Milibar")
mess = mess.replace("hPa","Hectopascal")
mess = mess.replace("lm","Lumin")
self.log("Message {} is {}".format(psent+1,mess))
#send message to alexa and turn off dummy bulb
self.call_service('media_player/alexa_tts', entity_id=self.alexa, message=mess)
self.turn_off(self.trigger)
Documentation is in the program, but the real trick is using the Alexa App Routines to trigger the dummy light bulb, you can use any scripts, node-red or even the template bulb to react to the trigger event.
So in the Alexa App, you can create a new routine
“Alexa is the patio door open” (created in the Alexa app)
“Please wait while I check” (created in the Alexa app)
Set dummy Light bulb brightness to 1% (created in the Alexa app)
“The patio door is closed” (response triggered by brightness level)
Hope this is useful