I’m really struggling with it. Every time I try to manually set the value of sensor.id_4_pro_performance_150_kw_204_ps_odometer to the value it had when I charged it, and also set sensor.id_4_pro_performance_150_kw_204_ps_charging_state to “charging,” I still get the value that is behind sensor.id_4_pro_performance_150_kw_204_ps_odometer instead of the calculated one from the “else” section.
You’re defining last_charging_odometer in the if, so you can’t access it in the else. Move it out or just use odometer (but then your results will be 0).
I think what you need to do instead is to make an input_number helper to store the current odometer when it starts to charge, after calculating the previous delta (mileage) using the helper’s value and odometer.