Hi folks, I’ve created a template sensor to show me the friendly name and last command issued to my Alexa voice assistants. It works, but I’m getting this error in the logs:
TemplateError(‘UndefinedError: No first item, sequence was empty.’) while processing template ‘Template("{% set last_alexa_1 = expand(‘group.voice_assistants’) | selectattr(‘attributes.last_called’,‘eq’,true) | map(attribute=‘entity_id’) | first %}{{ state_attr(last_alexa_1,‘friendly_name’) }}")’ for attribute ‘_attr_native_value’ in entity ‘sensor.last_alexa_friendly’
TemplateError(‘UndefinedError: No first item, sequence was empty.’) while processing template ‘Template("{% set last_alexa_2 = expand(‘group.voice_assistants’) | selectattr(‘attributes.last_called’,‘eq’,true) | map(attribute=‘entity_id’) | first %}{{ state_attr(last_alexa_2,‘last_called_summary’) }}")’ for attribute ‘_attr_native_value’ in entity ‘sensor.last_alexa_command’
…and here’s my YAML:
template:
- sensor:
- name: last_alexa_friendly
state: "{% set last_alexa_1 = expand('group.voice_assistants') | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') | first %}{{ state_attr(last_alexa_1,'friendly_name') }}"
- name: last_alexa_command
state: "{% set last_alexa_2 = expand('group.voice_assistants') | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') | first %}{{ state_attr(last_alexa_2,'last_called_summary') }}"
Can anyone tell me what this means, or what I need to do to fix it?
ok, so you can optimize your template a bit and you don’t need to use state_attr. Ultimately your problem is that you have an emtpy list when using first. So you must provide a fallback via a default. Try this template instead:
Thanks so much. This works for the name, but not the ‘last_called_summary’ attribute (which represents the last voice command issued to that device).
If I strip out the ‘map’, I can see the attribute is there (see below), but I just get back ‘unknown’. Is this because I need to do something to handle spaces? Sorry for noddy question.