Trigger bases sensor to calculate difference between previous & current state

I’m trying to create a template sensor to calculate the difference between 2 states for an entity.
the entity itself just is an ever increasing value


I tried adding the template sensor below , but the sensor is allways unknown


  • trigger:
    • platform: state
      • sensor.airco_keuken_energy
    • name: “Current Airco Keuken Power Current State”
      state: “{{ trigger.to_state.state - trigger.from_state.state | float | round(2)}}”

You are missing a float(0)… so your first state is a string

"{{ (trigger.to_state.state | float(0) - trigger.from_state.state | float(0) ) | round(2) }}"
state: "{{ (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | round(2) }}"

I did that now, but the result keeps being ‘Unknown’ state

It will report unknown until sensor.airco_keuken_energy changes state.

After I changed the code to

state: "{{ (states('trigger.to_state') | float(0) - states('trigger.from_state') | float(0)) | round(2) }}"

I got something else then Unknown, but it still doesn’t update

That’s not the template I suggested to you. Compare what I posted above to what you have and you’ll see they’re not the same.

1 Like

Hi, Yes, I had done that first, waited until there was an update of the base entity and it didn’t seem to work.
So then I changed the code to what I had above and it didn’t work either.
Now I changed it back to what you wrote and now it seems to work! Looks like it was another USB-moment for me… )

Next step I’m trying to achieve…

The from_state & to_state are 2 values in kWh.
It seems to be possible to also get the from_state.last_changed & to_state.last_changed
So from that I want to try and calculate the average W used in that period…