Asking Alexa for information

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.

I did create the last_called sensor and also added 0.5 delay before the update_last_called and before the notify.alexa_media_last_called which did seem to help and yes they are the older echoes without the cloth so maybe that was why.

  action:
  - delay:
      seconds: 0.5
  - service: alexa_media.update_last_called
  - delay:
      seconds: 0.5
  - 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
        {% elif (state_attr('light.dialog', 'brightness') | int / 255 * 100 ) | int == 20  %}
          The EV battery is currently at {{ states('sensor.charging_level_hv') }} percent
        {% elif (state_attr('light.dialog', 'brightness') | int / 255 * 100 ) | int == 30  %}
          The electric range is {{ states('sensor.remaining_range_electric') }} kilometers
        {% else %}
          There is no routine set up for that value.
        {% endif %}
      data:
        type: tts
  - service: light.turn_off
    entity_id: light.dialog

I have also added two more routines but for some reason this one doesn’t work {% elif (state_attr('light.dialog', 'brightness') | int / 255 * 100 ) | int == 30 %} keeps going down to the there is no routine set up bit and I have no idea why, the brightness is set to 30 in the routine itself.
Can anyone spot anything incorrect (the value of that sensor is 0 at this moment)

The code looks fine, but check the arithmetic that comes out of your template if you paste it into the template editor in Developer Tools.

Just paste {{ (state_attr('light.dialog', 'brightness') | int / 255 * 100) | int }} into the template editor, trigger Alexa to run the range routine, and make sure the output from the template is actually returning 30. Sometimes you can get weird rounding errors… 30% of 255 is 76.5 rounded up it works, rounded down it fails:

{{ ((76.5)|int / 255 * 100) | int }} returns 29

If that’s what you see in the template editor, your options are to just change what’s after “==” to 29, or to modify your templates to compensate by rounding:

{% elif ((state_attr('light.dialog', 'brightness') | int) / 255 * 100) | round() | int == 30 %}

I would suggest modifying the templates since you could run into similar arithmetic issues if you create messages for 50%, 70%, and 90%.

Yup that was it, coming back as 29 and now 30 with the round() and works ok, thanks :slight_smile:

Based on your Idea’s i’ve create a Node-Red Version of your Sequence:

(Dummy, Trigger and Condition Handling does Node-Red)

Thanks! :slight_smile: