How to define a helper that sums up the increments of another sensor?

Hi

I have a sensor that represent a quantity. This value can increase or decrease.

How can I define a sensor or some other kind of helper that keeps track of the total quantity that quantity was ever incremented?

Examples:

History for source sensor
1, 2, 10, 5
Total increment sensor
=> 10

History for source sensor
1, 2, 10, 5, 10
Total increment sensor
=> 15

History for source sensor
1, 2, 10, 5, 10, 0, 2
Total increment sensor
=> 17

Easiest way to set up in the UI would be a Number helper (watch the max value!) and an automation:

trigger:
  - platform: state
    entity_id: sensor.your_source
condition:
  - "{{ trigger.to_state.state|float(0) > trigger.from_state.state|float(0) }}"
action:
  - action: input_number.set_value
    target:
      entity_id: input_number.your_number
    data:
      value: >
        {{ states('input_number.your_number')|float(0)
           + trigger.to_state.state|float(0) 
           - trigger.from_state.state|float(0) }}

A more elegant way, but requiring YAML, would be a trigger-based template sensor with the same logic.

Tested the automation with a Number helper as the input and your final example:

1 Like

Cool thanks for the fast help.

I will look into the trigger-based template sensor first, if I don’t get it working, I will do the automation.

Why not just a utility meter helper?

Just set net_consumption: true so it can go both up and down.

@Troon
I didn’t get the triggered sensor working. So I tried the automation.

It almost worked, but the trigger.from_state.state is always 0. So I don’t get the diff working, it always adds the complete trigger.to_state.state value.

Any idea what might be the reason for that?

Here my automation

alias: Update Zwift Total Drops All Time Variable
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.test_drop
    above: "-1"
condition:
  - condition: template
    value_template: >-
      {{ (trigger.to_state.state|float(0)) > (trigger.from_state.state|float(0))
      }}
action:
  - action: input_number.set_value
    data:
      value: >
        {{ (states('input_number.toal_count')|float(0))
        + (trigger.to_state.state|float(0)) -
        (trigger.from_state.state|float(0)) }}
    target:
      entity_id: input_number.toal_count
  - action: input_number.set_value
    data:
      value: |
        {{ (trigger.from_state.state|float(0)) }}
    target:
      entity_id: input_number.drop_log
mode: single

For testing I tried to also write “trigger.from_state.state” into a log variable. But it stays 0.

“sensor.test_drop” is sensor with a fixed number, I updated manually, which will be later swapped out for a real sensor. What is also weird, if I use number variable as a trigger, then the automation does not triggered at all.

Any ideas?

Why are you using a numeric state trigger? That will only fire when the value of the entity changes from below -1 to above -1.

Ok. I got it working now.
I changed the trigger to state trigger.

But also my testing setup was not working. For testing I created template sensor, which returned a hard coded number. And for testing I manually adapted the number, by adapting the sensor configuration. An probably, that somehow reset the sensor, so that it lost its previous value.

When I tried it with a templated sensor, that read it value from an number input and changed the value of that input, the previous value is kept and the automation works as expected.

Thanks for your help.