Cumulative counter and last 10 minutes value

Hello, I’m new to the forum and did not find the solution for my problem.

I have sensors configured in hass that give cumulative index values since they are active (water sensors, their values increase by 1 each 1 liter).
I successfully managed to create daily, monthly, yearly sensors using utility-meter.
But I would like to have another sensor with the value over the last 5 or 10 min.

→ What is the best way to add a sensor which would make a difference (substract) between current value and value 5 or 10 min ago ?

Many thanks for the help you can provide :slight_smile:

Welcome to HA. :slight_smile:

Have a look at the time throttle filter integration option. I haven’t used that option, so I can’t say for certain. Once you have that, you can create a template sensor that calculates the difference between the current value and that one.

A more manual approach would be to create an automation that only triggers every x min (5 or 10, in your case). In the action, you can access the from state (i.e. the value from x min ago) and set the value of an input number. Similar to the above, you can create a template sensor to calculate the difference between the current value and the input number.

I think the two options will work slightly differently. The first may update in blocks/intervals of x min (it says it will skip over values, so may do this with some kind of delay) but the second will be more like a running difference, as it will recalculate every time the current sensor value changes.

Many thanks for your contribution.
I succeeded :slight_smile:

To help others, I share my config :

  1. Getting the value 5min ago:
- platform: filter
  name: "water_efs1_5minAgo"
  entity_id: sensor.water_efs1
  filters:
    - filter: time_throttle
      window_size: "00:05"
      precision: 2
  1. Compute the difference between now and 5 min ago :
- platform: template
  sensors:
    water_efs1_5min:
      friendly_name: 'water_efs1_5min'
      unique_id : water_efs1_5min
      value_template: "{{ (states('sensor.water_efs1')|float - states('sensor.water_efs1_5minAgo')|float)|round(3) }}"
      unit_of_measurement: "L"
      device_class: energy

The device class is not the good one, and I don’t know which one to use for water consumption…

Hope helping others back :wink:

You’re welcome.

There isn’t one, unfortunately, but at least you have the unit of measurement. I would remove the device class if I’m you (which will make it none, or you set it explicitly to None). If you use an incorrect device class, there could be integrations (for example) that could scan a domain and look for devices/sensors of a specific class. Take for example the recently released energy management feature – this might then pick up your water meter.

The device classes also come with nice little icons, so if that is something you want, you can set a static icon via the customisation options. If you want the icon to change based on state (or something else), you can use an icon template.

Well it seems that my 1-day long test did not succeed :frowning:

For an unknown reason, it seems that 5minago sensors is not always moving. Over a night without any value change on the main sensor (increment fixed to a specific value over 5 hours or more), the 5minago seems to be not updated and remains with a gap from the main sensor even after 5 minutes as configured. So difference computed is not 0 whereas counter was not changed…

Maybe picture is more clear instead of hazardous explanation :slight_smile:

I need to find another way to get a sensor with value 5 min ago that is continously refreshed and moving over time…

Now trying this config:

- platform: derivative
  source: sensor.water_efs1
  name: water_efs1_last5min
  round: 1
  unit_time: min
  time_window: "00:00:05"

Let’s wait for some time to see if data is correct.

For the previous config, restarting hassio has reset the 5minAgo sensor to the main sensor.

So last post did not succeed too with solution tried…

This is the solution I used, after some days feedback :slight_smile:

I use the “variable” integration, with this code in configuration.yaml :

variable:
  water_history_ecs1:
    value: 'none'
    restore: true

Then, an automation fills the variable attributes every 5 seconds, shifting values from newer to older, thanks to an automation (automation.yaml) :

###################################################
- alias: update water temp history
  initial_state: 'on'
  trigger:
    - platform: time_pattern
      seconds: '/5'
  action:
      - service: variable.set_variable
        data:
          variable: water_history_ecs1
          attributes: 
            history_24: "{{ state_attr('variable.water_history_ecs1', 'history_23') }}"
            history_23: "{{ state_attr('variable.water_history_ecs1', 'history_22') }}"
            history_22: "{{ state_attr('variable.water_history_ecs1', 'history_21') }}"
            history_21: "{{ state_attr('variable.water_history_ecs1', 'history_20') }}"
            history_20: "{{ state_attr('variable.water_history_ecs1', 'history_19') }}"
            history_19: "{{ state_attr('variable.water_history_ecs1', 'history_18') }}"
            history_18: "{{ state_attr('variable.water_history_ecs1', 'history_17') }}"
            history_17: "{{ state_attr('variable.water_history_ecs1', 'history_16') }}"
            history_16: "{{ state_attr('variable.water_history_ecs1', 'history_15') }}"
            history_15: "{{ state_attr('variable.water_history_ecs1', 'history_14') }}"
            history_14: "{{ state_attr('variable.water_history_ecs1', 'history_13') }}"
            history_13: "{{ state_attr('variable.water_history_ecs1', 'history_12') }}"
            history_12: "{{ state_attr('variable.water_history_ecs1', 'history_11') }}"
            history_11: "{{ state_attr('variable.water_history_ecs1', 'history_10') }}"
            history_10: "{{ state_attr('variable.water_history_ecs1', 'history_9') }}"
            history_9: "{{ state_attr('variable.water_history_ecs1', 'history_8') }}"
            history_8: "{{ state_attr('variable.water_history_ecs1', 'history_7') }}"
            history_7: "{{ state_attr('variable.water_history_ecs1', 'history_6') }}"
            history_6: "{{ state_attr('variable.water_history_ecs1', 'history_5') }}"
            history_5: "{{ state_attr('variable.water_history_ecs1', 'history_4') }}"
            history_4: "{{ state_attr('variable.water_history_ecs1', 'history_3') }}"
            history_3: "{{ state_attr('variable.water_history_ecs1', 'history_2') }}"
            history_2: "{{ state_attr('variable.water_history_ecs1', 'history_1') }}"
            history_1: "{{ states('variable.water_history_ecs1') }}"
      - service: variable.set_variable
        data:
          variable: water_history_ecs1
          value: "{{ states('sensor.water_ecs1') }}"

The last part is the shower detection, based on more than 5liters hot water detection over the 2 min period (24*5sec). The initial shower index is backup at the beginning at the shower. When end of shower is detected, the ecs.efs variable attributes are updated to get the consumed water :

- alias: "update shower state 1 on"
  initial_state: 'on'
  trigger:
    - platform: time_pattern
      seconds: '/5'
  condition:
     condition: and
     conditions:
       - condition: state
         entity_id: variable.water_shower1
         state: "off"
       - condition: template
         value_template: "{{ (states('sensor.water_efs1')|int - state_attr('variable.water_history_efs1', 'history_24')|int) > 5 }}" 
       - condition: template
         value_template: "{{ (states('sensor.water_ecs1')|int - state_attr('variable.water_history_ecs1', 'history_24')|int) > 5 }}"    
  action:
      - service: variable.set_variable
        data:
          variable: water_shower1
          attributes:
            efs: "{{ state_attr('variable.water_history_efs1', 'history_24') }}"
            ecs: "{{ state_attr('variable.water_history_ecs1', 'history_24') }}"
      - service: variable.set_variable
        data:
          variable: water_shower1
          value: "on"
###################################################""
- alias: "update shower state 1 off"
  initial_state: 'on'
  trigger:
    - platform: time_pattern
      seconds: '/5'
  condition:
     condition: and
     conditions:
       - condition: state
         entity_id: variable.water_shower1
         state: "on"
       - condition: template
         value_template: "{{ (states('sensor.water_efs1')|int - state_attr('variable.water_history_efs1', 'history_24')|int) <= 5 }}" 
       - condition: template
         value_template: "{{ (states('sensor.water_ecs1')|int - state_attr('variable.water_history_ecs1', 'history_24')|int) <= 5 }}"    
  action:
      - service: variable.set_variable
        data:
          variable: water_shower1
          attributes:
            efs: "{{ states('sensor.water_efs1')|int - state_attr('variable.water_shower1', 'efs')|int }}"
            ecs: "{{ states('sensor.water_ecs1')|int - state_attr('variable.water_shower1', 'ecs')|int }}"
      - service: variable.set_variable
        data:
          variable: water_shower1
          value: "off"

Finally it is notified to the notification group, with 3 group of the following code to have a color notification change depending on the amount of water used during the shower:

- alias: "Notification fin douche maison"
  trigger:
  - platform: state
    entity_id: variable.water_shower1
    to: "off"
  condition:
    condition: and
    conditions:
      - condition: template
        value_template: "{{ (state_attr('variable.water_shower1', 'ecs')|int) >= 12 }}" 
      - condition: template
        value_template: "{{ (state_attr('variable.water_shower1', 'ecs')|int) < 24 }}" 
  action:
    - service: notify.ALL_DEVICES
      data:
        title: "Bilan douche : moyen"
        message: "Douche maison terminée. {{ state_attr('variable.water_shower1', 'ecs') }} L eau chaude et {{ state_attr('variable.water_shower1', 'efs') }} L eau froide"
        data:
          color: "#F56D2D"

My code is probably complicated, since I’m beginner.
All comments are welcomed to improve the way I did with home assistant.

Hope helping other :slight_smile:

2 Likes

It’s old but i found this topic while trying to achieve something similar so i’ll share my solution :smiley:

I have a solar radiance sensor and wanted to have a sort of rolling average over the last 10 minutes.

I created an integration sensor to have the cumulate value over time.

Then I used an SQL sensor to get the a 10 mins old value from my sensor.

At this point, since I have 2 time series the NOW integral and the integral 10 minutes ago I just had to create a template sensor with NOW - OLD value.

And this provides me my 10mins average value.

finally created a template