Getting sensor previous value

Dear all
I cannot figure out how to get the previous value of a sensor in order to make some calculations regarding the actual value.
Could you please lend me a hand with this issue?
Thank you in advance.

What calculation are you trying to do, and how, a template sensor?

Hi Tom,
I have reactive power sensor and also active power sensor. I want when the reactive sensor is in certain range a counter (I have to create it) accumulates the active power.
I thought the better way to do it should be with a template sensor, but I have no clue on how to get the previous value of a sensor.
Any thought about this? Thank you in advance

Firstly, you don’t accumulate power. You accumulate energy.

So with that in mind, how about this:

  1. create a utility meter with two tariffs, accumulated and ignored. Feed it from your active energy sensor. If you don’t have this sensor you can create one using the Riemann sum integration from your active power sensor.
  2. use an automation to switch the tariffs triggered by a numeric state trigger that monitors your reactive power. When it is in range use the accumulated tariff, when it is out of the range use the ignored tariff.

Thank you tom
You’re right power in the time is energy.
But actually what I wanna do, is accumulate the energy in a variable (active power in the time) only when the reactive power sensor’s increase is between the boundaries (that’s why I need to know the previous value of the reactive power)

Which is exactly what my proposal does,

Hi tom
I guess I don’t get it or maybe I wasn’t clear enough. I need the the accumulator “counts” the energy only when the difference between the present and previous reactive power reading goes into a range. Not when the present reactive power reading goes into such range

Let’s assume your reactive power range is 24 to 42 and your utility meter is named “Active Energy”.

trigger:
  platform: state
  entity_id: sensor.reactive_power
  to:
action:
  choose:
    - conditions:
        - condition: template
          value_template: "{{ 24 < (trigger.to_state.state|float(0) - trigger.from_state.state|float(0)) < 42 }}"
      sequence:
        - service: select.select_option
          target:
            entity_id: select.active_energy
          data:
            option: accumulate 
  default:
    - service: select.select_option
      target:
        entity_id: select.active_energy
      data:
        option: ignore

This will accumulate energy in the sensor sensor.active_energy_accumulate only when the difference between the last and this reactive power measurement is in the range 24 to 42 W. Otherwise it gets stored in sensor.active_energy_ignore

You rock Tom!! we’re almost there.
As far as I see, in the sensors sensor.active_energy_accumulate and sensor.active_energy_ignore, the values accumulated will be those given for the sensor.reactive_power; this sensor will act as the trigger, but actually I’d like to accumulate the value of the another sensor; sensor.active_power

You’re right. Let’s forget for a minute that we’re talking about electricity. What I need is an automation which checks the difference between the present and previous value of a sensor, then if such difference is in a given range, it has to accumulate the value of ANOTHER sensor in a counter.
The sensors are actually sensor.reactve_energy and sensor.active_energy; these sensors are already up and running.
So, reactive energy as a trigger and active energy to be accumulate in a variable when the condition is satisfied.

I don’t know how many times I can say it. That is exactly what the automation above does.

1 Like

Sorry be patient with me, as you will have suspect, I’m a newbie. Where the sensor.active_energy is envolved in the automation? should I insert the word “sensor” in the sentence entity_id:? something like select.sensor.active_energy?