I am fairly new to Home Assistant, and I need help with better yaml code for my template sensor. This is my first post, so if it not according to standards, please let me know
I’ve got a water tank for which I custom built a proximity sensor to measure the height of the water from the top. This works perfect, and I am getting back the information via a Wemos D1 loaded with Tasmota and MQTT. Everything works fine, but I want to streamline my coding, and I want to customise my icons depending on the tank water level percentage from empty, i.e how full is the tank!
Can someone assist me in streamlining my coding for the percentage level calculation as in my sensor value_template?
I also then want to have a more dynamic icon linked to that percentage. Something that displays similarly to a battery level with various level of charge.
If you want to graph this information, you’ll need to remove your % and the word fault and make it have a unit of measurement %. Or if you make it a device_class: battery, it will auto make your icon into a battery symbol that separates on percentage. But it will look like a battery.
Thanks a lot for this, and the detailed explanation and code. This is exactly what I needed, but I could not figure out how to code the equations to do this.
The first example are working 100%, but when I used the last version of your code, for the real numeric values, I got the following error:
First time:
(It looks like the issue is with unit_of_measurement: % - Line 15 in my code)
Configuration invalid
Error loading /config/configuration.yaml: while scanning for the next token found character '%' that cannot start any token in "/config/includes/sensors/water_level.yaml", line 15, column 28
Second time:
So I removed that line (commented it out) and now I get the following error: Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'round') for dictionary value @ data['sensors']['water_level']['value_template']. Got "{% set water_level = states('sensor.water_tank_level') | float %} {% if water_level < 21 %}100 {% elif 21 <= water_level < 174 %}\n {{ ((10-((water_level -21)/17))*10) round(1) }}\n{% else %}0 {% endif %}". (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
Lastly, I see you refer to the Template Editor. Where do I get access to that?
Thanks for all the feedback. All your examples are now working 100%. Looking at everything I cannot believe how simple it was actually. And the quotes around the % sorted out that issue as well
I must make use of the template editor more. That will definitely make it easier in future
please allow me to jump in here, since Ive been following this solution, and noted it in my cookbook
I dont have the sensor.water_tank_level and still this happens:
How can this be?
Note that adding |int or |float to a non existent entity_id might makes that 0. So smaller than 21 in this case
I use that technique in cases of light brightness, (an Off light hasn’t got a brightness and with the |int you can still use templates and automations in the case) and it is really a hack to keep things going.
Yes, that’s because int returns zero when it cannot parse test like ‘unknown’. Which is what is happening. ‘unknown’ is not a number, so it defaults your value to zero.
@Jackie.P
Could you please share your final code for this? I’m i no way a programmer but would like to have a sensor like this to monitor a holding tank. Which sensor did you use and does it hold up in the long run given that it’s high humidity?
Cheers, Marcus
Everything is holding up fine. I’ve used a Wemos D1 mini and flashed it with Tasmota as I want all my devices to communicate with MQTT to HA, and I also like the Tasmota Web interface. I’ve not used the normal HC-SR04 ultrasonic sensor. I opted to go for the waterproof version (JSN SR04T) which basically looks the same as your park distance control sensor on your car bumper. Completely waterproof.
I’ve printed a small mounting holder for this and glued the sensor into this holder which then gets mounted right at the top in the middle of the tank at the highest level. So, the JSN-SR04T cannot measure a distance less than 20cm, but in my case that was no issue as the top of my tank where the sensor is mounted is just on about 20 cm above highest water level, so it worked out fine.
SO my WemosD1 is mounted in a box with the sensor attached with about 1.8meter of wire to the tank and have been working flawlessly for about 2 years now. The sensor is sometimes completely wet with condensation but still reports my levels 100% correctly
My final code:
############################################################
# Wemos D1 Sensor linked to HC-SR04 for Water Tank Level
# and DHT11 for Humidity & Temperature
############################################################
platform: mqtt
name: "Water_Tank_Level_From_Top"
state_topic: "tele/sonoffD1_1/SENSOR"
value_template: "{{ value_json.SR04.Distance }}"
unit_of_measurement: "cm"
################################################################
#
# Home Assistant - Tank Level Controller with Temp & Humidity
#
# Tank Info:
# Tank Total Height = 180cm (Bottom to Sensor Mount Height)
# Sensor level from Top of Tank to Overflow level = 20cm
# Outlet Pipe above Bottom = 10cm
# Usable Water Level Distance = 140cm (Outlet Pipe to Overflow)
#
################################################################
platform: template
sensors:
water_level_percentage:
unit_of_measurement: '%'
value_template: >-
{% set water_level = states('sensor.water_tank_level_from_top') | float %}
{% if water_level < 21 %}100.0
{% elif 21 <= water_level < 174 %}
{{ ((10-((water_level -21)/17))*10) | round(1) }}
{% else %}0
{% endif %}
Hope this helps but let me know if you need more info