Hi,
I have an ESP8266 which is used to measure the ullage of my heating fuel tank, since the tank is far away it is a battery-operated sensor. It measures the empty space between the sensor and the level of the fuel (called ‘ullage’), so by subtracting this ullage number from the known tank height, I know how high the level of the fuel in the tank is.
I’m using Tasmota and its Maxbotics HRXL (docs) component so I didn’t have to write the code myself. It connects via wifi to my MQTT broker, sends the measurement and then goes back to sleep. Because Tasmota always configures an LWT offline message, the sensor itself becomes unavailable in HA once it enters sleep.
I tried configured the following template sensor to 1) filter out the ‘unavailable states’ and 2) convert the ullage to the fuel height. Unfortunately I only get ‘0’ values from this template sensor…
template:
- sensor:
- unique_id: "5930f3d5-0c71-4e91-95df-20ba98a3e01f"
unit_of_measurement: "mm"
state_class: measurement
state: >
{% set ullage = states('sensor.tasmota_hrxl_distance')[:-3] %}
{% if is_number(ullage) %}
{{ 1350 - ullage }}
{% else %}
{{ states('sensor.fuel_tank_height') | int }}
{% endif %}
The idea here is that whenever the tasmota sensor has an ‘unavailable’ state (and thus the is_number()
check fails), the template sensor should report its previous value. When a valid number is returned (the [:-3]
format string is used to subtract the mm
string from a valid measurement, then subtract it from the 1350mm known tank height, and return the resulting number as the value.