Last Alexa Sensor using Keaton Taylor Alexa Media Player custom component

Unfortunately, I never got to grips with Home Assistant automations , so I use Python with Appdaemon

The principle however is very simple

to create a two way conversation with Alexa.

  • create a routine in the aleax app. to say "is the patio door open " or whatever you have a sensor for

  • Trigger a dummy bulb to 10% (doesn’t matter the value , 1 bulb 100 different triggers)

  • get alexa to say "let me check this for you " . This will buy you some time while the last alexa sensor updates

  • Trigger a HA automation on the dummy bulb switching to 10%

  • check if the patio door is open or closed and create a TTS response to the last alexa sensor saying “the patio door is (opened or closed)”.

  • Set the dummy bulb back to 0%

My Parser script just allows you to create variations on the above without having to program multiple automatons. I was taught many, many years ago when compute time was very expensive “only write a program once”

Thanks for making that clear. As for now I’d like to keep it simple, if I have more ideas for that, than I will possible switch to your solution.
But currently for testing wha it can do, I think I can stay with ha automations.

However, I do have another issue now. I don’t want to spam the forum a third time with that concern, so I pointing to my question (last_called doesn’t update). Maybe you have an idea about it, as your are experienced with cookies. I have no idea what causes this, but maybe its a cookie issue?
See the edit seciton here

Thank you.

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:

- alias: Alexa Routine washing machine
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: input_boolean.alexa_routine_wash
    to: 'on'
  action:
    - service: homeassistant.turn_off
      entity_id: input_boolean.alexa_routine_wash
    - delay: "00:00:02"
    - service: alexa_media.update_last_called 
    - delay: "00:00:01" #not sure about the delays?
    - service: homeassistant.turn_on
      data_template: 
        entity_id: >
          {% if is_state("binary_sensor.wash_state", "on") %}
            script.alexa_routine_wash_on
          {% else %}
            script.alexa_routine_wash_off
          {% endif %}  

in scripts.yaml

alexa_routine_wash_on:
  sequence:
    - service: media_player.volume_set
      data_template:
        entity_id: '{{ states.sensor.last_alexa.state }}' 
        volume_level: 0.3
    - service: media_player.alexa_tts
      data_template:
        entity_id: '{{ states.sensor.last_alexa.state }}' 
        message: "Washingmachine on"

alexa_routine_wash_off:
  sequence:
    - service: media_player.volume_set
      data_template:
        entity_id: '{{ states.sensor.last_alexa.state }}' 
        volume_level: 0.3
    - service: media_player.alexa_tts
      data_template:
        entity_id: '{{ states.sensor.last_alexa.state }}'
        message: "Washingmachin off"

and I also have an automation for a questioned value:

- alias: Alexa Routinen Outside temp
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: input_boolean.alexa_routine_outsidetemp
    to: 'on'
  action:
    - service: homeassistant.turn_off
      entity_id: input_boolean.alexa_routine_outsidetemp
    - delay: "00:00:02"
    - service: alexa_media.update_last_called  
    - delay: "00:00:01"
    ##
    - service: media_player.volume_set
      data_template:
        entity_id: '{{ states.sensor.last_alexa.state }}' 
        volume_level: 0.3
    - service: media_player.alexa_tts
      data_template:
        entity_id: '{{ states.sensor.last_alexa.state }}'
        message: "Outside temperature {{ states.sensor.netatmo_outside_temperature.state }} degree C"

I think you just need to have the full entity id in all those alexa state places:

 'media_player.{{ states.sensor.last_alexa.state }}'

‘media_player.{{ states.sensor.last_alexa.state }}’

I had that before, but I changed my last_alexa sensor to include the media_player. at the beginning.

So it’s ok to have it like this way. I changed it because the example in the wiki (alexa component) shows this:

  value_template: >
    {%- for entity in states.media_player -%}
      {%- if state_attr(entity.entity_id, 'last_called') == True -%}
        {{ entity.entity_id }}
      {%- endif -%}
    {%- endfor -%}   

and this will report the full entity name.
So I changed mine to this:

{% if is_state_attr('media_player.echo_dot_1','last_called', true) %}
  media_player.echo_dot_1
{% elif is_state_attr('media_player.echo_dot_2','last_called', true) %}
  media_player.echo_dot_2
{% else %}
  none
{% endif %}

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.

You can also shorten the template

sensor:
  - platform: template
    sensors:
      last_alexa:
        entity_id:
        - media_player.1
        - media_player.2
        value_template: >
          {{ states.media_player | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}

I don’t think so…

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 %}

Unless I’m misunderstanding what you mean.

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”.

        entity_id:
        - media_player.1
        - media_player.2
        value_template: >
          {{ states.media_player | selectattr('attributes.last_called','eq',True) | map(attribute='entity_id') | first }}
1 Like

IOk, thanks @petro

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).

1 Like

Whats wrong with the template sensor I specified ?

Nothing wrong with that, I just wanted to know why the other one from the docs doesn’t work.

I also like compact code, so I like @petro’s new suggestion the most.

No problem, but @petro is correct you need to specify the items or they will not update

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.

Agreed your solution is neater . I thought 4 Alexis’s was extravagant ! Once added to your yaml , unless you have a Amazon addiction , its done

Lol, I only have 2 alexa’s. I think you’re all nuts.

EDIT: But I am rethinking the amount I have… maybe i need more…

:smiley:

I recently read a post where someone mentioned he has 13 alexa’s.

I was like “dude, how big is your house” :wink:

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.

@Petro - your yaml code works like a charm with the alexa/media_player version 0.10.2.
Thank you.

1 Like