Another Template question

I have a watersoftener which reports the weight of the unit but only every few hours. The template is configured as:

 - platform: template
    sensors:
      softner_weight:
        friendly_name: "Softner Weight"
        unit_of_measurement: 'Percentage'
        value_template: "{{ states('sensor.water_softner_weight') | float * 10000 | float(0)}}"

But every time I start HA, I get the error below. I thought I added the default by adding: | float(0) - but that doesn’t seem to work. Could anyone suggest how I fix this?

TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{{ states('sensor.water_softner_weight') | float * 10000 | float(0)}}' but no default was specified') while processing template 'Template<template=({{ states('sensor.water_softner_weight') | float * 10000 | float(0)}}) renders=4>' for attribute '_attr_native_value' in entity 'sensor.softner_weight'

You’re second float isn’t doing anything, and it doesn’t need a default. You’re converting the 10000 from an integer to a float. You need to convert the state from your sensor to a float, not the other.

{{ states('sensor.water_softner_weight') | float(0) * 10000 }}
1 Like

Hello doubledutch,

It sounds like it may be using MQTT or something that needs to refresh at boot?

You could ignore it if it only happens then.

If it is MQTT, you could turn on retain for that value so it isn’t cleared at restart.

You could test if the value is not unknown or unavailable in the template string and if it is, don’t try to read it.

You could also provide a default.

Thanks! That fixed it.