Template variable error: No first item, sequence was empty

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?

Thanks in advance.

Can you please format the code. Check out the FAQ point 11 for directions on that and edit your post.

Apologies - I think I’ve done this now but please let me know if it still needs attention.

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:

{{ expand('group.voice_assistants') | selectattr('attributes.last_called','eq',true) | map(attribute='name') | first | default('unknown') }}

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.

<template TemplateState(<state media_player.cabin=standby; volume_level=0.5, is_volume_muted=False, media_content_type=standby, media_position_updated_at=2022-02-28T14:45:22.835693+00:00, available=True, last_called=True, last_called_timestamp=1646059486509, last_called_summary=alexa hello, connected_bluetooth=None, bluetooth_list=[], friendly_name=Cabin, supported_features=56253 @ 2022-02-28T10:57:15.836721+00:00>)>

When mapping attributes, you need to include the full path to the attribute.

{{ expand('group.voice_assistants') | selectattr('attributes.last_called','eq',true) | map(attribute='attributes.last_called_summary') | first | default('unknown') }}

Just like you did in selectattr where you accessed last_called.

1 Like

Scholar and a gentleman, thank you!