homeassistant-attributes icon indicating copy to clipboard operation
homeassistant-attributes copied to clipboard

A specific case creates ValueError

Open r-jean-pierre opened this issue 1 year ago • 1 comments

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?

r-jean-pierre avatar Apr 15 '24 19:04 r-jean-pierre

Yes, there is. The code needs adjustments

pilotak avatar Apr 26 '24 13:04 pilotak