Best practice to store difference from sensor state

Hi all,

I have a not so smart EV charger which I cannot use to record charging data (kwh charged), so I have to pivot to an automation triggered by the status change of the “EV charging” sensor yelded by the kia_uvo integration.

I created 2 helper numbers:

  • ev_energy_before
  • ev_energy_after

which, in my mind, should retain the value of the kwh in the EV battery before and after the charge so I can calculate the difference.

I made this automation that:

when “EV charging” sensor goes from off to on it records the value of the EV battery kwh in both number helper at the same time with the same action (so the difference should be 0)

when “EV charging” sensor goes from on to of it records the value of the EV battery kwh just in ev_energy_after

I created a template sensor which simply subtracts ev_energy_before from ev_energy_after and it seems to work fine but every time it resets (“EV charging” state change from off to on) there’s a suddend positive spike in the template sensor as if the ev_energy_after helper retains the old (higher) value some second more than ev_energy_before .

This gives me a pretty awful graph and I believe problems if I use the template sensor data for other calculations.

Is there any better way to achieve my “simple” goal (having a sensor holding the kwh charged in the EV)?

Thank you for any help or nudges :slight_smile:

A triggered template sensor utilising an availability template could possibly do it.

What are the entity ids for:

  • The battery energy.
  • The charging state.

Thank you @anon11644107 :slight_smile:

  • battery energy is sensor.garage_eeve_energia_rimanente_eeve
  • the charging state is binary_sensor.eeve_ev_battery_charge