Water sensor template (water tank) for Energy dashboard

Hi,

I have problem in configuring energy sensor for water consumption.

My situation is that I have water tank which is used for toilet sink and for watering garden etc…
Refill of water tank is done by the rain.
slika

(yes, currently empty :slight_smile: )

The current info of water tank I got from modbus integration:

#UNITRONICS
  - name: modbus
    type: tcp
    host: 192.168.X.XX
    port: 502
    timeout: 10
    sensors:
    - name: Trenutni nivo vode
      unit_of_measurement: L
      slave: 1
      address: 3
      data_type: int16
      scan_interval: 30

So, how to make sensor template which will produce me data so that I could use usage of water tank to the Energy dashboard - Water consumption

I tried many things but I did not get possibility to add this under “Add water source”.
slika

Anybody? How to achieve this?

Hi
Your sensor must have the following criteria

state_class: total_increasing
unit_of_measurement: m³
device_class: water
icon: mdi:water
friendly_name: Compteur eau Jour m3

It can be as well in ft3, L or gal

Phil

1 Like

Of that I am aware… but how to make sensor from template so that “sensor.trenutni_nivo_vode” will be used when consumption from tank is made…

So tank max level is 3400L and when water is used (negative value from current level; lets say that it goes from 3400L to 3300L) the template sensor must add consumption (100L). And when tank refill (let say from 3300L to 3400L) this value is not used and consumption is not changed…

When I try this:

  - platform: template
    sensors:
      vodostanje_poraba_vode:
        friendly_name: 'Vodostanje (poraba vode)'
        device_class: water
        icon: mdi:water
        unit_of_measurement: 'L'
        state_class: total_increasing
        value_template: {{states('sensor.trenutni_nivo_vode')}}

I do get value -32L (which is current state), but it should be 0, as no consumption was made…

And when I check configuration I got error:

So, completly confused…

Correct sensor (not knowing if it works correctly):

  - platform: template
    sensors:
      vodostanje_poraba_vode:
        unit_of_measurement: "L"
        device_class: water
        friendly_name: "Vodostanje (poraba vode)"
        value_template: '{{ states("sensor.trenutni_nivo_vode") }}'

And I need also to edit customize.yaml:

sensor.vodostanje_poraba_vode:
  state_class: total_increasing

But I see in sensor:

But it should be 0 as no consumption made… So it is step forward, but still not OK…

I also see now this sensor under Water usage option…

And option B which will probably works is to use helper:

We will see after first rain…

Raining and utility meter not working correctly as it’s getting value to +.

But it’s refilling, there is not consumption yet.

Also, I can not add this sensor to energy dashboard…

So, do anybody know how to solve this?

Anybody know how to solve this?

Am I the only one who wants to measure water tank consumption based on water tank level in energy dashboard?

SOLUTION:

Prerequisite:
You must have water tank with current water level in L.
slika

1: Utility Meter (https://www.home-assistant.io/integrations/utility_meter/)
Create “Utility Meter” which add consumption to your virtual sensor.

2: Create sensor with m³ unit measurement (not m3, but m³).

sensor.yaml:

  - platform: template
    sensors:
      vodostanje_povratna_voda:
        unit_of_measurement: "m³"
        device_class: water
        friendly_name: "Vodostanje (povratna voda)"
        value_template: >
          {% if states('sensor.vodostanje_skupna_poraba_vode') == 'unknown' %}
            {{ '0' | int  }}
          {% else %}
            {{ states('sensor.vodostanje_skupna_poraba_vode') | float * 0.001 }}
          {% endif %}

customize.yaml:

sensor.vodostanje_povratna_voda:
  state_class: total_increasing

CONCLUSION:

With that setting every hour you will get consumption under Energy dashboard for your water tank on which you use only level measurement on which you have consumption with L unit measurement.

slika

Hi,

I used your solution to get the water consumption using my ultrasonic sensor in my water tank.
Initially it worked, but now I’m getting a message in the log, that something is wrong with the sensor:

Invalid config for [template]: expected dictionary for dictionary value @ data['sensors']. Got [OrderedDict([('name', 'Wasserverbrauch Zisterne'), ('unit_of_measurement', 'm³'), ('device_class', 'water'), ('state_class', 'total_increasing'), ('friendly_name', 'Wasserverbrauch Zisterne'), ('state', "{% if states('sensor.gesamtverbrauch_zisterne') == 'unknown' %}\n {{ int(0) }}\n{% else %}\n {{ states('sensor.gesamtverbrauch_zisterne') | float * 0.001 }}\n{% endif %}\n")])]. (See /config/configuration.yaml, line 274).

This is how I implemented the solution - I use the “emptyness” of my water tank to always generate a new entry, when the water level sinks.
Since the upgrade to home assistant 2023.2.5 I had to remove the state class measurement from the sensor “Zisterne Füllstand” and “Zisterne Leerstand”, as I get an error message that this is not supported for device class water.

  - sensor:
      - name: "Zisterne Füllstand"
        unit_of_measurement: L
        device_class: water
        state: "{{(states('sensor.ultraschallsensor_zisterne')|float*(-3161.9)+4600)|round(0)}}"

  - sensor:
      - name: "Zisterne Leerstand"
        unit_of_measurement: L
        device_class: water
        state: "{{(states('sensor.zisterne_fullstand')|float*-1+3999)}}"



  - sensors:
      - name: "Wasserverbrauch Zisterne"
        unit_of_measurement: m³
        device_class: water
        state_class: total_increasing
        friendly_name: "Wasserverbrauch Zisterne"
        state: >
            {% if states('sensor.gesamtverbrauch_zisterne') == 'unknown' %}
              {{ int(0)  }}
            {% else %}
              {{ states('sensor.gesamtverbrauch_zisterne') | float * 0.001 }}
            {% endif %}
          

Alright, I just renamed the sensor and changed the state to this

state: "{{ states('sensor.gesamtverbrauch_zisterne') | float * 0.001 |default(0)}}"

Now the error message disappeared.

Great to hear…

Just adding a note here to thank you @kslb as I used your guide to measure the outflow from a sump pump and report it as a grid dashboard “water consumption” element so I can track variations over time.

Measurements are based on a Qidian ADC Water level sensor connected to a Shelly Uni and uses the sump tank dimensions to estimate the liters in the tank as the water level changes.

I did not run into any specific issue other than my customize configuration not being split in the same files as yours, but all in all it worked well.

Hi,

i also use your code to record the water consumption from my water tank. Thank you for sharing it.
But when i reload the yaml configuration the value of “Wasserverbrauch Zisterne” is added to the energy-dashboard.
Does anybody have the same problem or an idea how this can be prevented?
Many thanks in advance.

I used liter instead of m3 as unit_of_measurement. Now everything is working as it should.