I’ve got an autom that allows me to ask a trigger command in Alexa and leveraging the Dummy Light approach this allows the automation to pull out a sensor value from my EV (specifically the battery charge, but whatever).
The value is then to be sent back to the originating Echo to be read out.
I can hardcode the Echo it outputs to, which does work, but as said above I really need it to go back to where the command was issued.
Can anyone help me with yaml syntax, I’m an idiot!
alias: EV Automations
triggers:
- entity_id: light.alexa_virtual
to: "on"
trigger: state
conditions: []
actions:
- action: alexa_media.update_last_called
data: {}
- delay: "00:00:05"
- data_template:
target:
- "{{ states.sensor.last_alexa.state }}"
data:
message: >-
{% if (state_attr('light.alexa_virtual', 'brightness') | int / 255 * 100
) | int == 10 %}
The battery is currently at {{ states('sensor.lsjwh4093pn104681_soc') }} percent
{% elif (state_attr('light.alexa_virtual', 'brightness') | int / 255 *
100 ) | int == 20 %}
The electric range is {{ states('sensor.lsjwh4093pn104681_range') }} miles
{% else %}
There is no routine set up for that value.
{% endif %}
action: notify.alexa_media
- action: light.turn_off
entity_id: light.alexa_virtual
initial_state: "on"
mode: single
*the error i get with the above is:
Error: Error rendering data template: ValueError: Template error: int got invalid input ‘None’ when rendering template ‘{% if (state_attr(‘light.alexa_virtual’, ‘brightness’) | int / 255 * 100 ) | int == 10 %} The battery is currently at {{ states(‘sensor.lsjwh4093pn104681_soc’) }} percent {% elif (state_attr(‘light.alexa_virtual’, ‘brightness’) | int / 255 * 100 ) | int == 20 %} The electric range is {{ states(‘sensor.lsjwh4093pn104681_range’) }} miles {% else %} There is no routine set up for that value. {% endif %}’ but no default was specified
You have too many data keys… data_template was deprecated 3 years ago but (in most cases) will still be resolved to data which means you have two data keys at the same level.
- action: notify.alexa_media
data:
target: "{{ states('sensor.last_alexa') }}"
message: >-
{% set index = (state_attr('light.alexa_virtual', 'brightness')
| int(0) / 255 * 100) | int(0) %}
{% if index == 10 %}
The battery is currently at {{ states('sensor.lsjwh4093pn104681_soc') }} percent
{% elif index == 20 %}
The electric range is {{ states('sensor.lsjwh4093pn104681_range') }} miles
{% else %}
There is no routine set up for that value.
{% endif %}
That is certainly an error, but would that give the error shown? I’m reading that as a problem rendering the template. Probably due to one of the entities referenced either being wrong or having a value inconsistent with what’s expected
I’m happy to be wrong. Every day is a school day, as they say.
The error message is saying that, at the point the call was made, the brightness value was none (likely the light is off) … and, since it was missing a default value for the int() filter, it doesn’t know what to do. I had already included defaults in the config above.
Just note that, assuming OP is using the correct entity ID, the lack of defaults wouldn’t usually affect the actual automation. This is because it triggers when the light turns “on”, at which point brightnesswill have a value.
Yes, quite possibly the error is a red herring. The actual experienced ‘error’ is that the autom runs, following the voice command, but nothing comes back out of the Echo device. When the Alexa entity is hardcoded, it works.
Didgeridrew, following your guidance i’ve overwritten the ‘bad’ part with your proposed. Can’t save as i get the error:
Error in parsing YAML: end of the stream or a document separator is expected (line: 7, column: 1)
Below is the full yaml again but with the proposed edits. The erroring line is this line:
action: notify.alexa_media
alias: EV Automations
triggers:
- entity_id: light.alexa_virtual
to: "on"
trigger: state
conditions: []
- action: notify.alexa_media
data:
target: "{{ states('sensor.last_alexa') }}"
message: >-
{% set index = (state_attr('light.alexa_virtual', 'brightness')
| int(0) / 255 * 100) | int(0) %}
{% if index == 10 %}
The battery is currently at {{ states('sensor.lsjwh4093pn104681_soc') }} percent
{% elif index == 20 %}
The electric range is {{ states('sensor.lsjwh4093pn104681_range') }} miles
{% else %}
There is no routine set up for that value.
{% endif %}
- action: light.turn_off
entity_id: light.alexa_virtual
initial_state: "on"
mode: single
The full automation with the corrections should look like:
alias: EV Automations
triggers:
- entity_id: light.alexa_virtual
to: "on"
trigger: state
conditions: []
actions:
- action: alexa_media.update_last_called
data: {}
- delay: "00:00:05"
- action: notify.alexa_media
data:
target: "{{ states('sensor.last_alexa') }}"
message: >-
{% set index = (state_attr('light.alexa_virtual', 'brightness')
| int(0) / 255 * 100) | int(0) %}
{% if index == 10 %}
The battery is currently at {{ states('sensor.lsjwh4093pn104681_soc') }} percent
{% elif index == 20 %}
The electric range is {{ states('sensor.lsjwh4093pn104681_range') }} miles
{% else %}
There is no routine set up for that value.
{% endif %}
- action: light.turn_off
target:
entity_id: light.alexa_virtual
mode: single