Create sensor on conditional switch state

I have a liter-counter sensor for my well pump and 4 garden valves.

I would like to create 4 virtual sensors that count the liters for each valve

Every valeva is controller by a sonoff

how can I do?

I believe this is not a common setup.
You need to explain in details what you have and how that works in HA

@Maury77 : Is there a liter counter sensor for each valve? If not, you will need a method to divide the flow if two or more valves are open. If you have 4 or 5 sensors it would be easy. I suspect that you have one litre counter at the well and wish to determine the flow from there.

I have one sensor of water made with this system https://github.com/rossiluis22/water_meter

 - name: "MQTT Sensor Water garder"
   state_topic: "tele/water_garden_cons/SENSOR"
   value_template: "{{ ( value_json['COUNTER'].C1 | multiply(0.004166) | float(0) ) | round(2) }}"
   unit_of_measurement: "Lt"
   state_class: total_increasing

The valves is commanded from a SONOF CH4 and I want create un template sensor whe 1 valve is ON that count global liter in that moment.

when SONOFF 1 Is ON I want count the liter till to OFF

I do something very similar with my oil furnace to allocate usage to different zones. My furnace burns 0.65 GPH when it’s on. I have 4 different zones. So I allocate the oil burned to each zone.

The first step is to convert the water flow into an instant measurement like Liters Per Minute or Gallons Per Minute.

The second step is to create a sensor that tells you the number of valves active. Third step is to calculate the usage for each active zone.

Here are my template sensors.


      house_hvac_zones_in_demand:
        value_template: >-
          {{
             is_state("sensor.bedroom_hvac_action","heating") | int
             + is_state("sensor.studio_hvac_action","heating") | int
             + is_state("sensor.living_room_hvac_action","heating") | int
             + is_state("sensor.water_heater_hvac_action","heating") | int
          }}
      house_hvac_zone_oil_gph:
        value_template: >-
          {%- if states("sensor.house_hvac_zones_in_demand") | int == 0 %}
          0
          {%- else %}
          {{ 0.65 / states('sensor.house_hvac_zones_in_demand')|float }}
          {%- endif %}

Then for each zone, I have a sensor that reports the zones current GPH.

      bedroom_oil_usage_gph:
        value_template: '{{ iif(is_state("sensor.bedroom_hvac_action","heating"), states("sensor.house_hvac_zone_oil_gph"), 0.0)  }}'

Then for each zone, I have a package with utility meters / integration to sum up the above sensor.


homeassistant:
  customize:
    sensor.bedroom_heat_oil_usage:
      unit_of_measurement: G

sensor:
  - platform: template
    sensors:
      bedroom_heat_gallons_per_hour:
        unit_of_measurement: G
        value_template: >-
          {% if is_state('binary_sensor.furnace_burner_running','on') -%}
          {{ states("sensor.bedroom_oil_usage_gph") }}
          {%- else -%}
          0.0
          {%- endif %}
      bedroom_heat_total_oil_yesterday:
        value_template: >-
          {{ state_attr('sensor.bedroom_heat_oil_today_running','last_period') }}
        unit_of_measurement: G
      bedroom_heat_total_oil_week:
        value_template: >-
          {{ state_attr('sensor.bedroom_heat_oil_weekly_running','last_period') }}
        unit_of_measurement: G
      bedroom_heat_total_oil_month:
        value_template: >-
          {{ state_attr('sensor.bedroom_heat_oil_monthly_running','last_period') }}
        unit_of_measurement: G

  - platform: integration
    source: sensor.bedroom_heat_gallons_per_hour
    name: bedroom_heat_oil_usage
    unit_time: h
    method: left
    round: 2

utility_meter:
  bedroom_heat_oil_today_running:
    source: sensor.bedroom_heat_oil_usage
    cycle: daily
  bedroom_heat_oil_weekly_running:
    source: sensor.bedroom_heat_oil_usage
    cycle: weekly
  bedroom_heat_oil_monthly_running:
    source: sensor.bedroom_heat_oil_usage
    cycle: monthly

recorder:
  include:
    entities:
      - sensor.bedroom_heat_energy_today_running
      - sensor.bedroom_heat_oil_today_running
      - sensor.bedroom_heat_oil_weekly_running
      - sensor.bedroom_heat_oil_monthly_running
      - sensor.bedroom_heat_total_oil_yesterday
      - sensor.bedroom_heat_total_oil_week
      - sensor.bedroom_heat_total_oil_month
      - sensor.bedroom_heat_oil_usage
      - sensor.bedroom_heat_gallons_per_hour

So works great. I use Search / Replace scripts (aka sed) to auto generate this config for each zone.