Asking Alexa for information

So I can easily send HA sensors from HA to Alexa for things like switches/climate/etc and then I can create an Alexa routine in the app that I can set up my own Alexa - do something but what if I just want information.
For example I want to send my EV battery to Alexa and then create a routine where I say Alexa, what’s my ev battery level? and get a response but it looks like that doesn’t work since routines want you to perform an action, turn on/off, set temp, etc
Any work arounds around that?

Yes, you can create a dummie light.

You will examples in the alexa media player documentation as well.

First I created the dummie:


light:
  - platform: template
    lights:
      dialog:
        friendly_name: "Alexa Dummie-Licht"
        turn_on:
        turn_off:
        set_level:

Then an automation:


automation:
- id: alexa_antwortet
  alias: Alexa antwortet
  mode: single
  initial_state: on

  trigger:
  - platform: state
    entity_id: light.dialog
    to: 'on'

  action:
  - service: notify.alexa_media_echo_dotchen
    data:
      message: |
        {% if (states.light.dialog.attributes.brightness | int / 255 * 100 ) | int == 10  %}
           {% if is_state('binary_sensor.schlafzfenster', 'off') %} Schlafzimmerfenster ist geschlossen
           {% else %} Schlafzimmerfenster ist noch offen
           {% endif %}
        {% elif (states.light.dialog.attributes.brightness | int / 255 * 100 ) | int  == 20  %}
          Aktuell sind es {{ states('sensor.openweathermap_temperature') }} Grad
        {% else %}
          Heute wird es {{ states('sensor.openweathermap_forecast_condition') }}
        {% endif %}
      data:
        type: tts
  - service: light.turn_off
    entity_id: light.dialog


After that, I created a routine in the Alexa App. Action ist to turn on the dummie light with the given brightness, e.g. check of my window in the sleeping room: 10%.

Thanks for that I’m trying to figure out how I can change that to insert my ev battery sensor (sensor.ev_battery) not getting the 10% value you have in there, can you explain that in more detail.
This part: {% if (states.light.dialog.attributes.brightness | int / 255 * 100 ) | int == 10 %}

With the percentage you have the ability to create x routines in the Alexa app, so in my case 10 routines because I’ve chosen 10%-steps.
Routine 1 is my window check. When I ask Alexa: „Alexa check window in bedroom, the routine action is turn on dummie light with brightness 10%. These 10% are corresponding with the first if-rule in my automation above. That triggers the automation in HA.

Ok I understand that but that works for simple binary on/off sensors (which I have working) but I have a battery sensor with a 0-100 value.
I need it to come back and give me a battery percentage, which is different all the time so a simple if/else won’t work (unless there’s some trick to be applied?)

has also always different states, or am I misunderstanding you?

My bad, I just removed that part, let me try it now.

@pedolsky’s example will work, you just need to modify the statement to suit your needs by including your EV battery sensor information. The Alexa routine sets the dummy light’s brightness, then that triggers Home Assistant to tell Alexa to read the phrase related to that brightness percentage in the “message” portion of the service call.

So the action in your dummy light automation might look something like:

action:
  - service: notify.alexa_media_last_called
    data:
      message: |-
        {% if (states.light.dialog.attributes.brightness | int / 255 * 100 ) | int == 10  %}
          The EV battery is currently at {{ states('sensor.ev_battery') }} percent.
        {% else %}
          There is no routine set up for that value.
        {% endif %}
      data:
        type: tts
  - service: light.turn_off
    entity_id: light.dialog

Is that syntax correct? because I get this when I try to refresh automations?
image
But doing a check configuration doesn’t throw any errors
The code:

- id: alexa_test
  alias: Alexa Test
  mode: single
  initial_state: on

  trigger:
  - platform: state
    entity_id: light.dialog
    to: 'on'

  action:
  - service: notify.alexa_media_last_called
    data:
      message: |
        {% if (states.light.dialog.attributes.brightness | int / 255 * 100 ) | int == 10  %}
          The battery is currently at {{ states('sensor.solar_battery_soc'}} percent
        {% else %}
          There is no routine set up for that value.
        {% endif %}
      data:
        type: tts
  - service: light.turn_off
    entity_id: light.dialog
- id: alexa_test
  alias: Alexa Test
  mode: single
  initial_state: on

  trigger:
  - platform: state
    entity_id: light.dialog
    to: 'on'

  action:
  - service: notify.alexa_media_last_called
    data:
      message: >-
        {% if (state_attr('light.dialog', 'brightness') | int / 255 * 100 ) | int == 10  %}
          The battery is currently at {{ states('sensor.solar_battery_soc') }} percent
        {% else %}
          There is no routine set up for that value.
        {% endif %}
      data:
        type: tts
  - service: light.turn_off
    entity_id: light.dialog

Thanks for that I guess it didn’t like the | after message?
But when I ask Alexa… she just says OK and nothing after, strange because it was working just fine with the binary sensor.


That looks correct right?
And the sensor:

1 Like

No, a closing bracket was missing:

After soc'

Check your automation trace. Is it being triggered?

Yes it looks like it didn’t fire since I updated the automation for some reason:

The routine action requires an ‘on’ state of the dummie light.

Yes I was missing that and the dummy light needs to start with off not on.
Thanks all for the input, pretty sweet that it works :grinning:

Having an issue with this today.
I ask for battery, Alexa says checking battery and then nothing.
The automation is firing but looks like today it doesn’t know what last_media_called is?


This doesn’t make any sense because if I do Run Actions she says the battery level just fine without the error above

If you wanna use the Alexa last called function, you have to set it before (via custom sensor):

https://github.com/custom-components/alexa_media_player/wiki#last-called-device

If not, skip the last called part in your automation.

I don’t get it, how can it work ok when I manually trigger the automation and error out when trigger it via asking Alexa???
It should not work at all if that was the case?
Tried again just there and it worked, seems like it works randomly?

Since Alexa Media Player v 3.6.0, you don’t actually need to create your own “sensor.last-called” for basic notifications… the notify target notify.alexa_media_last_called is automatically created by the integration. But, some older echo devices don’t seem to cooperate with the last_called attribute from Alexa Activities API and the sensor is very useful for building room-aware automations and sensor reports.

One other thing to try… Sometimes it can be helpful to add a 1-2 second delay before the notify.alexa_media_last_called action. I think this work because it gives the API time to update the last_called attribute, but I’m not certain of that.