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…

Hi I tried to copy the above but tools i get ‘trigger’ is undefined and the sensor shows up as unavailable. I have confirmed the states of the entities defined are getting updated. Can someone please help?

#BMW Fuel Consumption
  - trigger:
      - platform: state
        entity_id: sensor.bmw_x5_remaining_fuel
      - name: "BMW X5 Trip Fuel Consumption"
        state: "{{ (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | round(2) }}"
        unit_of_measurement: "L"
        availability: "{{ states('sensor.bmw_x5_trip_fuel_consumption')|is_number }}"

  - trigger:
      - platform: state
        entity_id: sensor.bmw_x5_mileage
      - name: "BMW X5 Trip Distance"
        state: "{{ (trigger.to_state.state | float(0) - trigger.from_state.state | float(0)) | round(2) }}"
        unit_of_measurement: "km"
        availability: "{{ states('sensor.bmw_x5_trip_distance')|is_number }}"

Please post your example with proper formatting.

FAQ - Guideline 11 - Format it properly

Thanks @123 I just updated formatting of the code. Can you please advice what could be wrong.

The availability template is incorrect.

For example, the first one should reference sensor.bmw_x5_remaining_fuel not sensor.bmw_x5_trip_fuel_consumption.

Your availability is self-referencing. There’s no way for it to get out of the unavailable state. Try referencing the source sensor instead:

availability: "{{ states(trigger.to_state.entity_id) | is_number }}"

@123 and @mekaneck that now makes sense. Thanks you I will change the code and give it another try.