Home Assistant Community

Last Alexa Sensor using Keaton Taylor Alexa Media Player custom component

#1

I have been using the alexa python script https://blog.loetzimmer.de/2017/10/amazon-alexa-hort-auf-die-shell-echo.html?m=1 for some time now mainly for the last alexa option which returns the last echo or dot to have spoken. This script is not 100% reliable and requires the cookie to be updated every couple of weeks.

I noticed when I updated https://github.com/keatontaylor/custom_components/tree/master/media_player to the latest version 1.0.2 it now has an attribute last_called. This can be used to create a sensor which returns the last alexa to speak

EDIT version 1.1.0 now supports service call “alexa_media.update_last_called” which updates last called immediately

- platform: template
    sensors:
      lalexa:
      friendly_name: 'Last Alexa'
      value_template: >-
        {% if is_state_attr('media_player.lr_dot','last_called', true) %}
          lr_dot
        {% elif is_state_attr('media_player.k_dot','last_called', true) %}
          k_dot
        {% elif is_state_attr('media_player.fr_dot','last_called', true) %}
          fr_dot
        {% elif is_state_attr('media_player.br_echo','last_called', true) %}
          br_echo
        {% else %}
          none
        {% endif %}

Just replace my media_player entity id and names , with the correct names of your devices.

1 Like

Echo Devices (Alexa) as Media Player - Testers Needed
Zigbee2mqtt: getting rid of your proprietary Zigbee bridges (Xiaomi, Hue, TRADFRI)
Running alexa_remote_control.sh inside Hass.io
Running alexa_remote_control.sh inside Hass.io
Echo Devices (Alexa) as Media Player - Testers Needed
#2

Thanks for that example. As you mentioned this sensor is now also possible with the newest version (1.0.0) of alexa mediaplayer.

Is this also true for the alexa mediaplayer component and if yes how do I care about that cookie

Also I would be interessted in an automation example that triggers a tts to the last triggered alexa device.
Are there other usecases for that sensor?

0 Likes

#3

I have never had a cookie timeout with the alexa mediaplayer component . That’s way I’m glad the last alexa option is now supported and I can drop using the script.

I use the last alexa option to converse with echo’s dotted around the house , to ensure the correct echo responds. see the following

0 Likes

#4

Uh, that really looks difficult to set that up. I will try.
And you already use the sensor.last_alexa from the mediaplayer component. Is it fast enough in your setup. This sensor takes forever for me to update.

0 Likes

#5

@petro so that last alexa sensor gives (if it works, currently it doesn’t work, but it probably work soon) the name of the alexa device last called.

The state of that sensor could be echo_dot_test

Is it possible to use this state to build an entity?

  action:
    - service: media_player.alexa_tts
      data_template:
        entity_id: 'media_player.{{ states.sensor.last_alexa.state }}'
        message: "Testmessage"

Would this work?

EDIT: Nevermind @petro this works. Even a blind squirrel finds a nut once in a while. :wink:

1 Like

#6

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”

0 Likes

#7

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.

0 Likes

#8

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"
0 Likes

Echo Devices (Alexa) as Media Player - Testers Needed
#9

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

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

#10

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

0 Likes

#11

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

0 Likes

Echo Devices (Alexa) as Media Player - Testers Needed
#12

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.

0 Likes

#13

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

Echo Devices (Alexa) as Media Player - Testers Needed
#14

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”

0 Likes

#15

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

#16

Whats wrong with the template sensor I specified ?

0 Likes

#17

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.

0 Likes

#18

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

0 Likes

#19

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.

0 Likes

#20

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

0 Likes