Broke my sensor filters

Hi,

While attempting to add a second water level sensor to my HA system I seem to have broken the working template/filter I had. I would really appreciate a second set of eyes (help) as I think i’m missing something obvious.

Background: I was successfully was filtering the output (voltage) from a pressure sensor using a template in HA’s main config (/config/configuration.yaml). To ‘calibrate’ and ‘translate’ the empty and full outputs (voltage) as a percentage.

sensor:

  - platform: template
    sensors:
      main_water_tank_level:
        friendly_name: "Main Water Tank Level"
        unit_of_measurement: '%'
        icon_template: mdi:gas-cylinder
        value_template: >
            {% set voltage = states('sensor.main_tank_water_voltage')|float %}
            {% if voltage > 0.50 %}
            {{((voltage - 0.71)/(2.00-0.71)*100)|round(0) }}
            {% else %}
            invalid
            {% endif %}

I needed a second filter as the second sensor has different calibration requirement a.k.a. different output voltages.

I was unable to create the second filter without throwing an error, and now for some reason even after I removed the second template sensor code I’m no longer getting any values returned from the first previously working template sensor.

when i try to add it to the dashboard…

Try this:

sensor:

  - platform: template
    sensors:
      main_water_tank_level:
        friendly_name: "Main Water Tank Level"
        unit_of_measurement: '%'
        icon_template: mdi:gas-cylinder
        value_template: >
            {% set voltage = states('sensor.main_tank_water_voltage')|float(0) %}
            {{ ((voltage - 0.71)/(2.00-0.71)*100)|round(0) }}
        availability_template: >
            {{ states('sensor.main_tank_water_voltage')|float(0) > 0.5 }}

You really should be using the new template sensor format for new sensors. In this case it would be (configuration.yaml):

template:
  - sensor:
      - name: "Main Water Tank Level"
        unit_of_measurement: "%"
        icon: "mdi:gas-cylinder"
        state: >
          {% set voltage = states('sensor.main_tank_water_voltage')|float(0) %}
          {{ ((voltage - 0.71)/(2.00-0.71)*100)|round(0) }}
        availability: >
          {{ states('sensor.main_tank_water_voltage')|float(0) > 0.5 }}

This is probably happening because you have a non-numeric state (the else “invalid” case) and a unit_of_measurement. This is no longer allowed. Unit of measurement can only be used with numeric sensors. The availability template will operate similarly (returning unknown for voltages less than or equal to 0.5) but will not cause this error.

The other reason this could be occurring is that you did not supply a default value for your float filter. If this receives input that can not be converted to a number it will cause an error that prevents the template loading. I have used a default of 0 which will cause the availability template to return false and set your sensor to unknown.

FYI: these unknown states will appear as gaps in your history graphs. And you can not just replace “invalid” with the string “unknown”. It is a special state that is not a string.

1 Like

Is there reason you don’t implement this logic on the ESPHOME side of things?

I prefer to locate most of that kind of stuff there and just send over what I need in HA.

Tom, I made that change. rebooted HA and the new sensor is ‘unavailable’.

the ‘source’ sensor is providing data…

i have this feeling i’m missing something obvious. :roll_eyes:

Your source sensor is below 0.5v. The template will only return valid results when that goes above 0.5V, as per your original template.

Also looking at your template equation, it is going to return negative % readings for voltages between 0.5v and 0.71v.

You probably want your availability template to be this if your equation is correct:

{{ states('sensor.main_tank_water_voltage')|float(0) >= 0.71 }}

pasted that code and rebooted and it’s still ‘unavailable’ for some reason. nothing telling in the logs. the physical sensor is sending data.

is there a way i can step up the logging level for the relevant component? I know its possible for MQTT.

Has your source sensor gone above 0.5V yet?

You won’t get any reading unless it does. That value is invalid. As you specified.

you were correct. I had a multiply filter commented out in the ESP device config so the reported voltages were low. However, it is still unavailable as an entity. the physical device entity is now reporting 1-1.2v after removing the comments and a reboot. still no banana.

What does this return in the Developer Tools → Template editor:

state: >
  {% set voltage = states('sensor.main_tank_water_voltage')|float(0) %}
  {{ ((voltage - 0.71)/(2.00-0.71)*100)|round(0) }}
availability: >
  {{ states('sensor.main_tank_water_voltage')|float(0) > 0.5 }}

i’ve changed the sensor names to make it less ambiguous.

I have some data coming through!
…what did it was:

  1. deleted the ESP device from ESPHome after renaming the sensor.
  2. amending the sensor names in the template and the high and low voltage ranges as you suggested.

thanks you SO MUCH for your help! I’ll swap out that ‘throw in sensor’ and re-calibrate.