homeassistant-attributes
homeassistant-attributes copied to clipboard
A specific case creates ValueError
Hi,
I have for several month a sensor constructed such as:
- platform: attributes
friendly_name: "Last Month"
attribute: "Last month"
device_class: energy
unit_of_measurement: "kWh"
round_to: 1
entities:
- sensor.myenedis_xxx
and some other with the same philosophy
Recently, in the log, I receive a lot of:
2024-04-14 11:31:57.457 ERROR (MainThread) [custom_components.attributes.sensor] Could not attribute sensor for sensor.myenedis_xxx: ValueError: Template error: float got invalid input 'None' when rendering template '{% if states('sensor.myenedis_xxx') != 'unknown' and states('sensor.myenedis_xxx') != 'unavailable' %}{{ (state_attr('sensor.myenedis_xxx', 'Last month') | float) | round(1) }}{% else %} unknown {% endif %}' but no default was specified
which is quite understandable, the sensor sometimes outputs None
Is there a way to avoid it?
Yes, there is. The code needs adjustments