question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Template error in Template Editor while it works fine as custom template sensor

See original GitHub issue

The problem

I hope this is the right place to report this.

I use this code in a template sensor which works fine in that template sensor. However, when I use this code in the Developer Tools->Template Editor I get an error.

The code is: {% set sensors = states.sensor | selectattr(‘attributes.device_class’, ‘==’, ‘battery’) | selectattr(‘attributes.state_class’, ‘==’, ‘measurement’) | rejectattr(‘state’, ‘in’, [‘unavailable’, ‘unknown’]) | list %}

The error is: UndefinedError: ‘homeassistant.util.read_only_dict.ReadOnlyDict object’ has no attribute ‘device_class’

What version of Home Assistant Core has the issue?

Core 2022.5.0

What was the last working version of Home Assistant Core?

core-2022.4.6

What type of installation are you running?

Home Assistant Supervised

Integration causing the issue

No response

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

{% set sensors = states.sensor 
  | selectattr('attributes.device_class', '==', 'battery') 
  | selectattr('attributes.state_class', '==', 'measurement') 
  | rejectattr('state', 'in', ['unavailable', 'unknown'])
  | list %}

Anything in the logs that might be useful for us?

No response

Additional information

No response

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:23
  • Comments:24 (1 by maintainers)

github_iconTop GitHub Comments

7reactions
smarthomejunkiecommented, May 19, 2022

This can be solved by first checking if the attributes are defined for the entity.

For instance:

{% set sensors = states.sensor 
  | selectattr('attributes.device_class', 'defined') 
  | selectattr('attributes.state_class', 'defined') 
  | selectattr('attributes.device_class', '==', 'battery') 
  | selectattr('attributes.state_class', '==', 'measurement') 
  | rejectattr('state', 'in', ['unavailable', 'unknown'])
  | list %}

Now you won’t get this error anymore

4reactions
elkartocommented, May 19, 2022

This can be solved by first checking if the attributes are defined for the entity.

For instance:

{% set sensors = states.sensor 
  | selectattr('attributes.device_class', 'defined') 
  | selectattr('attributes.state_class', 'defined') 
  | selectattr('attributes.device_class', '==', 'battery') 
  | selectattr('attributes.state_class', '==', 'measurement') 
  | rejectattr('state', 'in', ['unavailable', 'unknown'])
  | list %}

Now you won’t get this error anymore

Well, it seems much better to do that, and it works!!

However, the behaviour in the template editor and in the custom template sensor should be the same.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is anyone else having problems with templates?
The template error you reported is merely a symptom of the, as yet unidentified, underlying problem.
Read more >
Templates and Custom Sensors in Home Assistant - YouTube
Templates and Custom Sensors are a more advanced feature in Home Assistant. With Templates and Custom Sensors you can format and convert ...
Read more >
Cisco DNA Center User Guide, Release 2.2.3
Step 1. In the Cisco DNA Center GUI, click the Menu icon ( ) and choose Tools > Template Editor. Step 2. In...
Read more >
PRTG Manual: Create Device Template
To successfully create a device template, you must add sensors to the device (either manually or via the auto-discovery) and the sensors must...
Read more >
Template not working as a sensor : r/homeassistant
You are using a deprecated way of defining template sensors. Use the new one. What does not work exactly?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found