Edit I have updated the code to add more features please use the code present in this first post not in subsequent posts Latest code will always be here on the first post , or on my GitHub page GitHub - lonebaggie/Home_Assistant-Config: Home Assistant Configuration in the Automation folder
@ duceduc thanks for testing
I have re-written this using a node-red flow
This script simplifies the tasks of passing entity details for Alexa to speak using the brilliant Alexa speech module. It will also enable Alexa to speak any entity or attribute value within Home assistant by just asking.
To get Alexa to speak entity values normally requires complex Jinja2 templates to be created. Compare the following
patio door is {{states.binary_sensor.patio_door.state|replace(âoffâ,âclosedâ)|replace(âonâ,âopenâ)}}
with
patio door is (e01s)
Which obviously outputs either the âpatio door is openâ or âpatio door closedâ
The code (e01s) will output the state of the entity with the correct device type without restoring to any code.
Home assistant automatons do not allow the use of any persistent data. However, input selects are effectively lists. So, this project uses two Input selects to hold the data.
By using a virtual bulb, we can use the Alexa App to trigger the automation.So, we can do the following
" Alexa is the patio door open"
âThe Patio door is closedâ
This requires a virtual bulb. The brightness levels are used to trigger the input select which in turn parses the text and output the results to Alexa.
OK we need the following items
light
- platform: template
lights:
alexa_virtual:
friendly_name: "Alexa Dummy Light"
turn_on:
turn_off:
set_level:
Replace media_player entities in the echo group with your Alexa devices. This will ensure the speech is delivered to the last alexa spoken to.
group
echos:
name: All Amazon Echo's
entities:
- media_player.lr_echo
- media_player.echo_show_5
- media_player.k_dot
sensor
platform: template
sensors:
last_alexa:
value_template: >
{{ expand(states.group.echos) | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}
input_select
See notes below:-
entity_list:
name : Entity List
options:
- binary_sensor.front_door
- binary_sensor.patio_door
- sensor.home_temperature
- sensor.zoe
- light.living_room
speak_list:
name: Speak List
options:
- do not use
- The current time is (tt)
- The current date is (td)
- The current temperature of (e02f) is (e02s)
- The battery level of (e03f) is (e034)
- The (e04f) is at (e04b) % brightness
The entity list contains all the entities you wish to parse (so replace the entities with the ones you wish to use). Lists start at 0. So, in the above example entity list 0 is the front door sensor and entity list 3 is the hall motion sensor.
The speak list contains all the sentenceâs you wish to send to Alexa to speak. These corresponds to the brightness level of the virtual bulb so brightness 1 corresponds the âcurrent time isâ. Please ensure the first item is âdo not useâ. This is used by the scripts to reset the input select.
The items to parse are contained within brackets () and have the following syntax.
(tt) = Current time
(td) = Current date
(gr) = Greeting [Morning,Afternoon,Evening] based on current time
(eNN[s,$,S,f,b,B.[0-9]]) = Entity to parse
-
NN = the number in the entity list. Use 01 for 1 etc
-
s = State with unit of measure (uom) added
-
S = State to 1 decimal place with uom added
-
$ = State only
-
f = Friendly name
-
b = Brightness (%)
-
B = Brightness (level)
-
[0-9] = Attribute number
(cNN) = Chain two speak lines together to give speak lines greater than 255 chars.
- NN = number in speak list
Only one (cNN) can be added to each speak entry . It should be at the end of the line
Be very careful using the chain feature as you can loop the speech !
If you require an attribute not listed above use the attribute number as listed in the developer tools states attribute column . The first attribute of an entity will be 0 the second will be 1 up to 9 the tenth attribute of the entity.
If you need additional features , such as rounding or mathematical calculations on a state or attribute, the code will accept standard Jinja2 blocks {{}}. Note (eNNs) may include the uom so will not be suitable for mathematical calculations.
Here are some examples using the entity list above
The (e00f) is (e00s)
The Front door is closed
Good (gr) todayâs date is (td)
Good Morning todayâs date is the 1st of October 2020
The (e04f) is set at (e04b) %
The Living room light is set at 80 percent
The indoor temperature is (e02s)
The indoor temperature is 21.45 celsius
The indoor temperature is (e02S)
The indoor temperature is 21.0 celsius
The indoor temperature is {{(e02$)|round(0)}} degrees
The indoor temperature is 21 degrees
The following automation scripts are used
Input Select to speak.yaml
Dummy light to input select.yamll
These scripts are very long , so be careful when copying. The main script is triggered by the speak
list input select . You can test this by adding the input select to you Lovelace screen. You can test each line to ensure the sentence is correct.
Once you are happy with the speech. You can push the virtual bulb to Alexa with the Nabu Casa cloud or use the Emulated Hue. The Dummy light to item select script will trigger the input select once the brightness level is triggered by the Alexa app.
Enjoy.