Populating a sensor with historical quantity

I have been using a water meter monitor that I built from this post, and I’m still loving it: 'Universal' Water Usage Monitoring Using ESPHome

Is there a place in the ESPHome YAML that I can put the meter’s current value, since the little device thinks it started counting at zero and doesn’t know about the 1.5 million gallons that this house has used in the previous forty years? :smile:

When I simply updated the number (Developer Tools → State → Update), it didn’t change the number that the actual sensor is counting, only the quantity that Home Assistant is counting. As a result, the Energy Dashboard is displaying negative value for today because I entered a huge quantity and the sensor has only counted a couple of hundred gallons since I restarted it today.

Thanks again for any suggestions!

https://esphome.io/components/sensor/#offset

Thanks – an offset seems like a good approach!

That should work as long as the 8266 never resets the value to zero, right? Currently that’s stored in a global variable – so do I just need to add

restore_value: yes

to my ESPHome config file?

And is there any way to increase the initial_value programmatically (i.e., from the same lambda that increments the pulse count)?

Thanks!!

You can create a utility_meter entity in HA and use your water meter entity as the source. And then you can calibrate that utility meter to set it at whatever value you want. This is the easist method. Even if your ESPHome device resets to zero the utility meter entity will keep counting up.

If you want your ESPHome device to show the right value but using an offset in your ESPHome config doesn’t provide enough flexibility, you can set your device up to receive a value via an action from HA. This is the more complicated route, but allows you to set the value in real time rather than having to put it into a config and then flash the device.

This is what I use in my gas meter device, modify it to suit your needs:

# Enable Home Assistant API
api:
  encryption:
    key: "your key here, if you use one"
  services:
    - service: set_meter_reading
      variables:
        meter_reading: float
      then:
        - logger.log:
            format: "Setting Meter Reading: %.1f"
            args: [meter_reading]
        - globals.set:
            id: counter_total
            value: !lambda |-
                return int(meter_reading * ($pulses_per_measurement_unit * 1.0));
##### Set meter reading from HA using the action tab in developer tools:
## action: esphome.<device_name>_set_meter_reading
## data:
##   meter_reading: "125000"
2 Likes