Gpiod water flow sensor YFB*- count of pulses converting in different working ranges and aggregating to Liters

new to home assistant but so far it has gone quite well looking through the docs adn forum and while I have dug around I am honestly a bit lost and can use some advice on how to get data from a counter

  1. converted and
  2. managed differently during different sampled amounts over time (i think this is needed as it has working ranges - as I understand at lower ranges the fluid will sneak past and at higher rates the momentum of the wheel impacts the result) (where i play to calibrate at different flow rates)

background:

  1. raspberry pi installation where via HACS i have used gpiod to connect a water flow sensor to the gpio pins. so no ESPhome (which seems would make this easier with some built in functions and it seems more people that have done this)
  2. working so far and I am able to get a counter i believe every time it pullses and into a counter: “counter water_flow_sensor”

what I think I should do is take a sample say every 5s or 1s and based on the number of pulses in the given period I can decide how to manage it and from there combine them into total liters as well as hopefully max, min, median giving even L/hr L/day, etc…

Seems a template may be the way to do this? If so, I am not wrapping my head around how to take a sample over a specified time and given its value then converting the value ?

your help is appreciated!

so investigating more and it seems I am close … but at the moment the conversion is not quite working … what I have now is the following

counter:
  water_meter_pulses_interval:
    name: Water Meter Pulses Interval
    initial: 0
    step: 1

automation:
  - alias: Count Water Meter Pulses in Interval
    trigger:
      - platform: state
        entity_id: binary_sensor.gpiod_gpiod_23_water_flow_sensor
        to: 'on'
    action:
      - service: counter.increment
        target:
          entity_id: counter.water_meter_pulses_interval


  - alias: Reset Water Meter Pulse Counter
    trigger:
      - platform: time_pattern
        seconds: '/5'
    action:
      - service: counter.reset
        target:
          entity_id: counter.water_meter_pulses_interval

sensor:
  - platform: template
    sensors:
      water_flow_rate:
        friendly_name: "Water Flow Rate"
        unit_of_measurement: "l/min"
        value_template: >
          {% set pulse_count = states('counter.water_meter_pulses_interval') | int %}
          {% set threshold = 10 %}  # Example threshold for switching flow rate factors
          {% set pulses_per_liter_low = 10 %}  # Pulses per liter for lower flow rate
          {% set pulses_per_liter_high = 6 %}  # Pulses per liter for higher flow rate

          {% if pulse_count <= threshold %}
            {% set flow_rate = (pulse_count / pulses_per_liter_low) * (60 / 5) %}
          {% else %}
            {% set flow_rate = (pulse_count / pulses_per_liter_high) * (60 / 5) %}
          {% endif %}

          {{ flow_rate | round(2) }}

utility_meter:
  quarter_hourly_water_usage:
    source: sensor.water_flow_rate
    cycle: quarter-hourly
  hourly_water_usage:
    source: sensor.water_flow_rate
    cycle: hourly
  daily_water_usage:
    source: sensor.water_flow_rate
    cycle: daily
  weekly_water_usage:
    source: sensor.water_flow_rate
    cycle: weekly
  monthly_water_usage:
    source: sensor.water_flow_rate
    cycle: monthly

the *_water_usage just comes up 0 , at least after the 5s intervals are zero’d (as seen in the template developer interface) :confused:

Any ideas why that could be the case? Seems it should be ok in that the intervals every 5s are being tracked (i see this in the dashboard as a sensor)…

if I understand templates they should just be applied over the data that is underlying and that data exists - I am flummoxed. :upside_down_face:

is there someway to access the data that is being plotted on the interval that I can see in the dashboard and apply the template to that data set?

got it working here albeit maybe a bit strangely, nonetheless I am glad it is now working … now to try to calibrate!