Translated values to save every 5s

trying to get a sensor value, translated (via an automation?) which then resets (hopefully) the counter of the sensor and saves the value - I have tried now to do this through an automation - but I would appreciate some help as:

→ the translated value based on the number of the count during the 5s seems to work but is not being saved as I thought it would within the automation “capture flow rate and reset pulse counter” (reset of the pulse counter seems to be working)

here is the yaml I am using:

#ref : https://github.com/jdeneef/ha_gpiod
binary_sensor: 
  - platform: gpiod
    sensors:
    - name: "water_flow_sensor" #eg YFB10 
      port: 23
      debounce: 0

#####
# Input number to store the flow rate every 5 seconds
input_number:
  water_flow_rate_5s:
    name: Water Flow Rate (5s)
    min: 0  
    max: 100000
    step: 0.01
    unit_of_measurement: "l/min"
    
counter:
  water_meter_pulses_interval:
    name: Water Meter Pulses Interval
    initial: 0
    step: 1
  water_meter_pulses_total:
    name: Water Meter Pulses Total
    initial: 0
    step: 1
    
automation water_meter_counter:
  - 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: Count Water Meter Pulses total
    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_total
          
# Automation to capture the flow rate every 5 seconds and then reset the counter
  - alias: Capture Flow Rate and Reset Pulse Counter
    trigger:
      - platform: time_pattern
        seconds: '/5'
    action:
      - variables:
          pulse_count: "{{ states('counter.water_meter_pulses_interval') | int }}"
      - service: input_number.set_value
        data:
          entity_id: input_number.water_flow_rate_5s
          value: >
            {% set threshold = 10 %} 
            {% set pulses_per_liter_low = 10 %}
            {% set pulses_per_liter_high = 6 %}
            
            {% 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) }}
      - service: counter.reset
        target:
          entity_id: counter.water_meter_pulses_interval

# Template sensor to display the latest captured flow rate
sensor:
  - platform: template
    sensors:
      water_flow_rate:
        friendly_name: "Water Flow Rate"
        unit_of_measurement: "l/min"
        value_template: "{{ states('input_number.water_flow_rate_5s') }}"


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

any help is appreciated!!

I think I got it!!

what i did is change to the following, removing the unneeded template and connect the meter values to the *_5s input value

#ref : https://github.com/jdeneef/ha_gpiod
binary_sensor: 
  - platform: gpiod
    sensors:
    - name: "water_flow_sensor" #eg YFB10 
      port: 23
      debounce: 0


#####
# Input number to store the flow rate every 5 seconds
input_number:
  water_flow_rate_5s:
    name: Water Flow Rate (5s)
    min: 0
    max: 100000
    step: 0.01
    unit_of_measurement: "l/min"
    
counter:
  water_meter_pulses_interval:
    name: Water Meter Pulses Interval
    initial: 0
    step: 1
  water_meter_pulses_total:
    name: Water Meter Pulses Total
    initial: 0
    step: 1
    
automation water_meter_counter:
  - 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: Count Water Meter Pulses total
    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_total
          


# Automation to capture the flow rate every 5 seconds and then reset the counter
  - alias: Capture Flow Rate and Reset Pulse Counter
    trigger:
      - platform: time_pattern
        seconds: '/5'
    action:
      - variables:
          pulse_count: "{{ states('counter.water_meter_pulses_interval') | int }}"
      - service: input_number.set_value
        data:
          entity_id: input_number.water_flow_rate_5s
          value: >
            {% set threshold = 10 %} 
            {% set pulses_per_liter_low = 10 %}
            {% set pulses_per_liter_high = 6 %}
            
            {% 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) }}
      - service: counter.reset
        target:
          entity_id: counter.water_meter_pulses_interval


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

adding here to help anyone else looking for a similar solution

my method that finally helped me debug the issue was to migrate the main automation into the automation helper screen which with the help texts helped me find my error!

Not clear to me ;)…

it’s coming from esphome data ?

I would use state with times of 5sec in trigger and directly fill the sensor. no need of input, counter etc.

no, it is using gpiod … a reed sensor with pulses directly connected to the gpio on the rpi that is running the home assistant.