Ok, so currently the last_alexa sensor doesn’t work reliable, but I’m hopefully that this will change soon with an update.
So this is one an automation where I want to use it.
I’d like to have have a look on it from an more experiences user like @petro
Maybe it’s possible to make this more compact.
the input_boolean is setup in alexa to I can turn it on with a routine. This triggers this automation:
The thing is that that the example above works in the dev_template (=shows the entity) and doesn’t work when you use it in the sensor.yaml file. After a restart the state will stay empty (another user also reported this in the alexa thread).
Do you know how do solve this? Maybe you can try it yourself to see if you also get this behaviour.
That’s how template sensors work. Your top value template doesn’t have any entity_id’s in it, so the template doesn’t know when to trigger a change. The template below does update, because it has the entities it needs to trigger an update.
if you pair the value_template with the list of media players, the top template will work.
I have the following service call in an action and it works as expected:
- service: media_player.alexa_tts
data_template:
entity_id:
- media_player.computer_room_dot
- media_player.kitchen_dot
- media_player.livingroom_dot
#- media_player.master_bedroom_dot
- media_player.garage_dot
- media_player.big_room_dot
message: >
{% if states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[5] is defined %}
Attention!,,,Attention!,,,The National Weather Service Has issued a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[5] }}
{% elif states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[4] is defined %}
Attention!,,,Attention!,,,The National Weather Service Has issued a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[4] }}
{% elif states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[3] is defined %}
Attention!,,,Attention!,,,The National Weather Service Has issued a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[3] }}
{% elif states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[2] is defined %}
Attention!,,,Attention!,,,The National Weather Service Has issued a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[2] }}
{% elif states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[1] is defined %}
Attention!,,,Attention!,,,The National Weather Service Has issued a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[1] }}
{% else %}
Attention!,,,Attention!,,,The National Weather Service Has issued a {{ states.sensor.nws_alerts.attributes.spoken_desc.split('\n\n-\n\n')[0] }}
{% endif %}
I think you miss understood me. I’m referring to listing out devices for updating sensor templates. @h4nc was mentioning how his template sensor was not updating. And it’s because this template doesn’t contain any entity_id’s.
value_template: >
{%- for entity in states.media_player -%}
{%- if state_attr(entity.entity_id, 'last_called') == True -%}
{{ entity.entity_id }}
{%- endif -%}
{%- endfor -%}
So, if he adds the entities it will update. Like so
entity_id:
- media_player.1
- media_player.2
value_template: >
{%- for entity in states.media_player -%}
{%- if state_attr(entity.entity_id, 'last_called') == True -%}
{{ entity.entity_id }}
{%- endif -%}
{%- endfor -%}
Then I mentioned how he could shorten the template if he wanted to. A bit more complicated if you don’t understand it, but it gets the same goal “the last used alexa”.
I got the template without the entities from the alexa component docs
So this should be changed, because other users might also have issues with that.
But I don’t know how to change that there.
Edit: so there is no way to do this without specifying the entities? Maybe a more automatic way that uses all media players that start with or contain “alexa”
That is just a limitation of template sensors, template binary_sensors, value_templates for template lights etc, triggers, and wait_templates. You always have to list out the items to cause the sensor to update. There is no way to do this without specifying the media_players. Unless you move to appdeamon or some other package. Home assistant only listens to things in templates that it can find. So when it ‘hears’ a change, it updates.
The only time you don’t need to list out entities in templates is inside actions/scripts or conditions. Because those trigger off something else (the trigger).
The only drawback of your template sensor is that if you add a dot, you have to update the template with an additional if statement. The template I provided and the one @h4nc came up with using the for loop only require the addition of a new entity_id in the entity_id list. So it’s essentially a difference of writing the if statement. Either way, it doesn’t matter cause you still gotta update the template. It just depends on how much work you want to do.
I have this in all of my automations that I use to call alexa and get a response. I use the dummy light setup that @lonebaggie resommended. I dont use appdaemon, but the overall process is the same.
So trigger it sh
- alias: "Alexa Report"
trigger:
- platform: state
entity_id: light.alexa_virtual
to: 'on'
action:
- service: homeassistant.update_entity
data:
entity_id: sensor.last_used_echo
- service_template: "notify.{{ states('sensor.last_used_echo') }}"
data_template:
message: messgages here based on light level
- service: light.turn_off
entity_id: light.alexa_virtual
I have never had an issue with the last echo. I will say I’m using the script at the moment, I may move over the the newer component once I can use notify for my TTS setup
First let me say, I love this component. I have weather alerts broadcast through the house during storms. I recently setup a page where I can type in messages for each device, or broadcast typed in messages to all devices. I can even email messages to specific or all devices. (I use Appdaemon for my development). I think I remember seeing messages about this a while back, but have lost them. How can I send a audio file to this? Lets say I want to send the sound of a dog barking whenever the door opens and no one is home to my den alexa.
it’s my understanding that, unfortunately, there’s no way to do it unless you can find the soundtrack on one of the audio channels already provided by Amazon.
The last_alexa sensor will have the last Alexa that was spoken to as it’s value (now that I’m using the right configuration.yaml from @petro.
However, when I execute a script from one echo device (office) it responds back to that office device. If I then go to another echo device (kitchen) and do the same, it responds from the office device. Then, because I just used the kitchen, the kitchen becomes the last_alexa so if I repeat the command in the kitchen (or office) the response comes out of the kitchen.
If I used the office for that repeat command, the last_alexa value updates to the office after the kitchen has responded. Has anyone else seen this behavior and how have you gotten around it?
I’m guessing that it has to do with scan_interval rather than being updated by which alexa the request came from but continuing execution before passing it to the sensor.
Does anyone have any tricks to circumvent this out of sync condition?
Thanks.