How to make measurement back to last value before device unavailable

hi, everyone, thankyou for read this

Please i need your help about my HA, i’m very noob coding in HA.

my problem is, i have digital water flow meter K24 that was i modification, using hall effect chip U18, that is working whell with measurement, but if my wifi network lost or power outage, measurement change to zero again, and i want ask how to make my total usage water, and measurement not affected when the device is unavailable (wifi network lost, or poweroutage).

i just search on this forum but i dont find anything that same.

Info System:

  • i use truenas (docker/container) system Nas Os
  • wemos d1 mini
  • sensor pulse hall effect (magnetic) read from turbine (K24 Digital Flow Meter)
esphome:
  name: kamarmandi308
  friendly_name: Termometer

esp8266:
  board: esp01_1m

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "codecodecode"

ota:
  - platform: esphome
    password: "codecodecode"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  manual_ip:
    static_ip: 192.168.1.23
    gateway: 192.168.1.1
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Termometer Fallback Hotspot"
    password: "5Aes8oS4hJPi"

captive_portal:

sensor:
  - platform: pulse_counter
    name: "Flow Rate Water"
    pin: GPIO5
    update_interval: 20s
    filters:
    - lambda: return (x / 76);
    unit_of_measurement: "L/Min"
    total:
      unit_of_measurement: 'L'
      name: 'Water Volume Total'
      accuracy_decimals: 1
      filters:
      - lambda: return (x / 76);

  - platform: dht
    pin: GPIO4
    model: AM2302
    temperature:
      name: "Temperature"
    humidity:
      name: "Humidity"
    update_interval: 60s

hall effect sensor

PS:
plese help me, and sorry for my poor english

My understanding is that Home Assistant and ESPHome assume that the devices will always be connected, and so data is assumed to have happened when it is received,

I am wondering whether you want
(a) for Home Assistant to keep showing the previous value until it receives a new value from your water flow meter ?
(b) for ESPHome on your ESP32 to detect that wi-fi is down, store the measurements and send all the individual measurements when the wi-fi comes back online ?
(c) for ESPHome on your ESP32 to detect that wi-fi is down, store a total of the measurements and send the total when the wi-fi comes back online ?


For (a), search the Home Assistant Community


For (b) you should consider using MQTT instead of the Native API. MQTT has a Quality of Service option value 3 which guarantees delivery of each message. However having several measurements logged at the same time after a pause may make your HA statistics look wrong.

There is also the possibility of a power outage loosing those readings which have not yet transmitted.


Option (c) seems easier - but still has some difficulties.

In ESPHome the Native API or MQTT protocols provide the actual connection over wi-fi. The Native API component also provides on_client_connected and on_client_disconnected triggers, and an api.connected condition you can check elsewhere.

Normally when a sensor is read, ESPHome attempts to send its value to Home Assistant. There is no check that the value was received, and when the sensor is next read its value replaces the previous value, and sent to Home Assistant.

But when we read the sensor, we can check whether API is currently connected, and do something different if it is not connected.
What should we do ? In the sensor’s on_value automation we could add the new value (in variable ‘x’) to the previous value, and keep adding until the API is connected.

Of course in this example it makes no sense to total temperature values - that was just a handy sensor to check the code syntax.

sensor:
  - platform: aht10
    variant: AHT20
    id: sensor_value
    temperature:
      name: AHT20 Temperature
      #####################################################
      #
      # if wi-fi is disconnected, accumulate this value 
      #
      on_raw_value:
        then:
          - if:
              condition:
                api.connected:
              else:
                - logger.log:
                    format:  "##### API is disconnected, so adding previous value %f to new reading %f"
                    args: [ id(sensor_value).state, x ]
                - lambda: |-
                    x += id(sensor_value).state;     // add sensor_value instead of replacing it

You should check the edge conditions - does it loose a measurement when the wi-fi drops, or connects ?

You might find it worthwhile to send the current sensor_value as soon as the API connects, with sensor.publish_state(0). Note that this executes the sensor_value above including its on_raw_value, which will add the 0 to the current sensor_value.

api:
  encryption:
    key: !secret esphome_api_encryption
  on_client_connected:
    - logger.log:
        format: "#####     >>>>>>>>>>  API Client '%s' connected with IP %s"
        args: ["client_info.c_str()", "client_address.c_str()"]
    #  catch up any measurements we may have missed
    - lambda: 'id(sensor_value).publish_state(0);'
    # note that publish_state calls sensor_value including its on_raw_value which ADDS the 0 argument to its current value
  on_client_disconnected:
    - logger.log: "#####     >>>>>>>>>> API client disconnected!"

Powerfail is different. If you store the measurement in global memory, ESP32 will remember the value - but you have probably lost those measurements which during the power outage.

The better approach is to prevent the power to the ESP32 failing, by running it from a battery.

Hei @donburch888 thank you so much for your replay, but i just find my answer from my friend, for make some template sensor that setting always available. btw thank you so much for your time