Estimated water usage with pulse meter

Hi everyone :wave:

I’m attempting to estimate my water usage in the front yard with simulated pulse meter sensor.
I currently have my sprinklers setup to be controlled with esphome. I’m now trying to set the rate and total usage via lamda when a value turns on then turn back to zero when the valve turns off. The rate works as expected the total does not increment and returns to 0 when the valve turns off.
Here is my current yaml for the sprinklers.

captive_portal:

time:
  - platform: sntp
    id: my_time

web_server:
  port: 80
  
sprinkler:
  - id: lawn_sprinkler_ctrlr
    main_switch: "Frontyard Sprinklers"
    auto_advance_switch: "Frontyard Sprinklers Auto Advance"
    reverse_switch: "Frontyard Sprinklers Reverse"
    multiplier_number: "Frontyard Sprinkler Multiplier"
    repeat_number: "Frontyard Sprinkler Repeat"
    valve_overlap: 10s
    valves:
      - valve_switch: "Zone 1"
        enable_switch: "Enable Zone 1"
        run_duration_number: 
          name: "Zone 1 Run Duration"
          unit_of_measurement: min
          initial_value: 5
        valve_switch_id: lawn_sprinkler_valve_sw0
      - valve_switch: "Zone 2"
        enable_switch: "Enable Zone 2"
        run_duration_number: 
          name: "Zone 2 Run Duration"
          unit_of_measurement: min
          initial_value: 5
        valve_switch_id: lawn_sprinkler_valve_sw1
      - valve_switch: "Zone 3"
        enable_switch: "Enable Zone 3"
        run_duration_number: 
          name: "Zone 3 Run Duration"
          unit_of_measurement: min
          initial_value: 5
        valve_switch_id: lawn_sprinkler_valve_sw2
sensor:
  - platform: pulse_counter
    name: "Water Rate Estimate"
    id: water_rate
    pin: D2
    unit_of_measurement: 'gal/min'
    #internal_filter: 100ms
    update_interval: 30s
    accuracy_decimals: 2
    filters:
      - lambda: !lambda |-
          if (id(lawn_sprinkler_valve_sw0).state ) {
            return 3.5;  
          } else {
            return 0;
          }
    total:
      name: "Water Usage Total Estimate"
      unit_of_measurement: "gal"
      id: water_total
      accuracy_decimals: 2
      device_class: water
      state_class: total_increasing
      filters:
      - lambda: !lambda |-
          if (id(lawn_sprinkler_valve_sw0).state ) {
            return 3.5;  
          } else {
            return 0;
          }

switch:
  - platform: gpio
    id: lawn_sprinkler_valve_sw0
    pin: D5
  - platform: gpio
    id: lawn_sprinkler_valve_sw1
    pin: D6
  - platform: gpio
    id: lawn_sprinkler_valve_sw2
    pin: D7

any thoughts or advice is very appreciated.

In case anyone is interested I cheated and added a global variable to keep track to total usage.

added this as a total usage

globals:
  - id: total_water_usage
    type: float
    restore_value: no
    initial_value: '0.0'

Then after seeing it work, added the estimate for the other zones after checking if the valve is on.

sensor:
  - platform: pulse_counter
    name: "Water Rate Estimate"
    id: water_rate
    pin: D2
    unit_of_measurement: 'gal/min'
    #internal_filter: 100ms
    update_interval: 60s
    accuracy_decimals: 2
    filters:
      - lambda: !lambda |-
          if (id(lawn_sprinkler_valve_sw0).state ) {
            return 3.8;  
          } else if (id(lawn_sprinkler_valve_sw1).state ) {
            return 1.6;  
          } else if (id(lawn_sprinkler_valve_sw2).state ) {
            return 2.4;   
          } else {
            return 0;
          }
    total:
      name: "Water Usage Total Estimate"
      unit_of_measurement: "gal"
      id: water_total
      accuracy_decimals: 2
      device_class: water
      state_class: total_increasing
      filters:
      - lambda: !lambda |-
          if (id(lawn_sprinkler_valve_sw0).state ) {
            return id(total_water_usage) += 3.5;  
          } else if (id(lawn_sprinkler_valve_sw1).state ) {
            return id(total_water_usage) += 1.6;  
          } else if (id(lawn_sprinkler_valve_sw2).state ) {
            return id(total_water_usage) += 2.0;  
          } else {
            return id(total_water_usage) ;
          }

Not the greatest but I can add a physical sensor later if I wish, and it will adapt to changes in value run time.