Help: Unknown state in if statement in template

Hi there,

I have a question regarding my sensor template.
I read the electrical energy consumption of the house with an Hichi IR flashed with tasmota at the smart meter.

To get reasonable values I have this template set up:

template:
  - sensor:
      - name: "Strom_Zahlerstand"
        unique_id: Stromkasten_gesamt_Zaehlerstand
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        state: >
          {% if states('sensor.tasmota_sm_1_8_0') == 'unavailable' or states('sensor.tasmota_sm_1_8_0') < '1' %}
            {{ states('sensor.strom_zahlerstand') }}
          {% else %}
            {{ (states('sensor.tasmota_sm_1_8_0') | float / 1000) | round(2) }}
          {% endif %}

But I get the following error in the logs:

Logger: homeassistant.helpers.template_entity
Source: helpers/template_entity.py:344
First occurred: 11:00:52 (1 occurrences)
Last logged: 11:00:52

TemplateError('ValueError: Template error: float got invalid input 'unknown' when rendering template '{% if states('sensor.tasmota_sm_1_8_0') == 'unavailable' or states('sensor.tasmota_sm_1_8_0') < '1' %} {{ states('sensor.strom_zahlerstand') }} {% else %} {{ (states('sensor.tasmota_sm_1_8_0') | float / 1000) | round(2) }} {% endif %}' but no default was specified') while processing template 'Template<template=({% if states('sensor.tasmota_sm_1_8_0') == 'unavailable' or states('sensor.tasmota_sm_1_8_0') < '1' %} {{ states('sensor.strom_zahlerstand') }} {% else %} {{ (states('sensor.tasmota_sm_1_8_0') | float / 1000) | round(2) }} {% endif %}) renders=8>' for attribute '_attr_native_value' in entity 'sensor.strom_zahlerstand'

Unfortunately I don’t get what it’s actually complaining about, or how I can fix it.

Is unknown the state of sensor.tasmota_sm_1_8_0 so that it can’t be converted to a float?

I am very happy for any help!
Samuel

template:
  - sensor:
      - name: "Strom_Zahlerstand"
        unique_id: Stromkasten_gesamt_Zaehlerstand
        unit_of_measurement: kWh
        state_class: total_increasing
        device_class: energy
        state: >
          {% if  states('sensor.tasmota_sm_1_8_0')|float(0) < 1 %}
            {{ this.state }}
          {% else %}
            {{ (states('sensor.tasmota_sm_1_8_0') | float(0) / 1000) | round(2) }}
          {% endif %}

In the if test the default value of 0 wil be used if the sensor can not be converted to a number. Thus “unavailable” will be less than 1. this.state is the template sensor’s value prior to the template being evaluated.

Thanks for your very fast reply!
Now I’m just confused by the float(0)shouldn’t this always evaluate to 0 ?

Thank you!
Samuel

This is the filter, not the function:

image

Thank you - now I get it!

Okay,

I just took the code @tom_l provided.

Now I get this error message:

ValueError: Sensor sensor.strom_zahlerstand has device class energy, state class total_increasing unit kWh and suggested precision None thus indicating it has a numeric value; however, it has the non-numeric value: unknown (<class 'str'>)

Somehow it still gets evaluated as unknown - which is of course not a numeric.

That will disappear the first time sensor.tasmota_sm_1_8_0 is greater than 1.

That’s strange. I thought that too, but this message gets logged on every startup - even if there have been a lot of actual readings in between.

After all, everything besides this message works, therefore we don’t have to put too much effort in solving this.