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.
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.
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.