I have 2 warnings in logswondering if anyone can help

First Warning.
in my sensor file a last call for my alexa’s and i’m getting this in my logs

Logger: homeassistant.helpers.template
Source: helpers/template.py:1779
First occurred: 6:33:25 PM (4 occurrences)
Last logged: 6:33:32 PM

Template variable warning: No first item, sequence was empty. when rendering '{{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') | first }}'

my code

- platform: template
  sensors:
    last_alexa:
      value_template: >
        {{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') | first }}

Second Warning.
My second warning is a in my sensor other sensor file the warning i’m getting is logs is.

Logger: homeassistant.helpers.template
Source: helpers/template.py:1291
First occurred: 6:33:25 PM (26 occurrences)
Last logged: 6:33:25 PM

Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ states('sensor.outside_lights_switch_watts') | float + states('sensor.lounge_switch_tv_watts') | float + states('sensor.lounge_switch_aircon_watts') | float + states('sensor.server_rack_switch_watts') | float + states('sensor.bedroom_switch_watts') | float + states('sensor.computer_switch_aircon_watts') | float + states('sensor.dining_room_fridge_freezer_watts') | float + states('sensor.kitchen_switch_freezer_watts') | float + states('sensor.kitchen_switch_fridge_watts') | float + states('sensor.main_bedroom_switch_watts') | float + states('sensor.back_room_light_powersensor') | float + states('sensor.front_room_light_powersensor') | float + states('sensor.dinning_room_light_powersensor') | float + states('sensor.hallway_light_2_powersensor') | float + states('sensor.kitchen_light_1_2_powersensor') | float + states('sensor.kitchen_light_2_2_powersensor') | float + states('sensor.lounge_room_light_powersensor') | float + states('sensor.main_bedroom_ceiling_light_2_powersensor') | float + states('sensor.main_bedroom_lamp_powersensor') | float + states('sensor.main_hallway_light_powersensor') | float + states('sensor.bathroom_light_2_powersensor') | float + states('sensor.bedroom_light_2_powersensor') | float + states('sensor.computer_room_light_powersensor') | float }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1

My code.

- platform: template
  sensors:
     total_power:
       entity_id: sensor.total_power_watts
       unit_of_measurement: "W"
       value_template: "{{ states('sensor.outside_lights_switch_watts') | float + states('sensor.lounge_switch_tv_watts') | float + states('sensor.lounge_switch_aircon_watts') | float + states('sensor.server_rack_switch_watts') | float + states('sensor.bedroom_switch_watts') | float + states('sensor.computer_switch_aircon_watts') | float + states('sensor.dining_room_fridge_freezer_watts') | float + states('sensor.kitchen_switch_freezer_watts') | float + states('sensor.kitchen_switch_fridge_watts') | float + states('sensor.main_bedroom_switch_watts') | float + states('sensor.back_room_light_powersensor') | float + states('sensor.front_room_light_powersensor') | float + states('sensor.dinning_room_light_powersensor') | float + states('sensor.hallway_light_2_powersensor') | float + states('sensor.kitchen_light_1_2_powersensor') | float + states('sensor.kitchen_light_2_2_powersensor') | float + states('sensor.lounge_room_light_powersensor') | float + states('sensor.main_bedroom_ceiling_light_2_powersensor') | float + states('sensor.main_bedroom_lamp_powersensor') | float + states('sensor.main_hallway_light_powersensor') | float + states('sensor.bathroom_light_2_powersensor') | float + states('sensor.bedroom_light_2_powersensor') | float + states('sensor.computer_room_light_powersensor') | float }}"

anyways around this as i dont know which one is through the error

hope someone can help
regards
Jeremy

The error messages are (suprisingly) precise.

{{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') | first }}

Obviously, states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') does not return a list. Probably because the ‘last_called’ attribute is empty (upon re-start).

{{ states('sensor.outside_lights_switch_watts') | float + ...

At least one of the states returns ‘unavailable’, which cannot be converted to a float. You can set a default value for that case:

{{ states('sensor.outside_lights_switch_watts') | float(default = 0)

thanks for your help

anyway to get around this?

Sorry, no. You can extend value_template to some slightly complex if list is empty, return empty string, else first entity_id script, that would make the warning disappear.

Thanks for that.

One last thing. do you see any issues with the this is formatted?

- platform: template
  sensors:
     total_power:
       entity_id: sensor.total_power_watts
       unit_of_measurement: "W"

i’m asking cos the message i’m getting is

Logger: homeassistant.components.template.sensor
Source: helpers/config_validation.py:796
Integration: Template (documentation, issues)
First occurred: 8:14:51 PM (1 occurrences)
Last logged: 8:14:51 PM

The 'entity_id' option near /config/sensors/power.yaml:149 is deprecated, please remove it from your configuration
  • platform: template
    sensors:
    name: total_power
    entity_id: sensor.total_power_watts
    unit_of_measurement: “W”

i've tried formatting it different ways but no luck..

still trying to get my had around this

regards

Instructions unclear? Remove entity_id and change sensor setup to:

- platform: template
  sensors:
     total_power_watts:
       unit_of_measurement: "W"

ummm yeah well i feel very dumb right now duh

Maybe something like this:

- platform: template
  sensors:
    last_alexa:
      value_template: >
        {% set last_player = states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id')  %}
        {{ last_player|first if last_player else none }}
1 Like

Is there actually a template for this sensor or are you just trying to set the unit. Because that can be done with customize. No need for a template sensor.

no success

Logger: homeassistant.helpers.template
Source: helpers/template.py:1779
First occurred: 8:29:36 PM (2 occurrences)
Last logged: 8:29:36 PM

Template variable warning: No first item, sequence was empty. when rendering '{% set last_player = states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') %} {{ last_player|first if last_player else none }}'

its something i put together. i didnt know could do this any other way

Yeah definitely dont do that. You can add a unit of measurement to any sensor using this:

Try this then:

- platform: template
  sensors:
    last_alexa:
      value_template: >
        {% set last_player = states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id')  %}
        {{ last_player|first if last_player|length > 0 else none }}

Try:

value_template: >- 
  {%- set last = (states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') | list ) -%}
  {{ last | first if (last | length > 0) else 'none' }}

still throws a error
and more

Logger: homeassistant.helpers.event
Source: helpers/template.py:408
First occurred: 8:48:37 PM (1 occurrences)
Last logged: 8:48:37 PM

Error while processing template: Template("{% set last_player = states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') %} {{ last_player|first if last_player|length > 0 else none }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 406, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1771, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 1291, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.9/site-packages/jinja2/environment.py", line 925, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
TypeError: object of type 'generator' has no len()

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 522, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 408, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: TypeError: object of type 'generator' has no len()

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:140
Integration: Template (documentation, issues)
First occurred: 8:48:37 PM (1 occurrences)
Last logged: 8:48:37 PM

TemplateError('TypeError: object of type 'generator' has no len()') while processing template 'Template("{% set last_player = states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') %} {{ last_player|first if last_player|length > 0 else none }}")' for attribute '_attr_native_value' in entity 'sensor.last_alexa'

stop throw errors but the sensor doesnt work anymore

Define “doesn’t work anymore”. Is the senor unknown, unavailable or is the sensor’s state ‘none’?

the way it works is it goes through each alexa device

reads the the attributes last_called in each device and if true it updates the sensor
the default value of last called is null

This value_template returns the String ‘none’, if no media_player matches the attribute filter. I am not familiar with Python / Jinja, maybe something other than ‘none’ must be returned. Maybe NoneType (without quotes).

@tom_l Can you help?

'none' or even none should be fine as a sensor state.

What does this return in the template editor:

{{ states.media_player | selectattr('attributes.last_called','eq',true) | map(attribute='entity_id') }}