A very easy substraction in the action of an automation gives me headaches

I’m pretty new here and probably make a beginners mistake I created 2 variables (var.) I have the variable integration installed and it works fine.

peugeot_508e_last_energy:
friendly_name: “Peugeot 508e_Last_Energy”
state_class: measurement
unit_of_measurement: “Kwh”
icon: mdi:gauge
peugeot_508e_today_energy:
friendly_name: “Peugeot 508e_Today_Energy”
state_class: measurement
unit_of_measurement: “Kwh”
icon: mdi:gauge

I have an automation who is filling the var.peugeot_508e_last_energy This is without problem. And I can display the value.
Now I have an automation wich is calculating the differens between the original and the stored value. It works fine in the template simulator.

“{{ ‘%0.2f’ | format (states(“sensor.north_porch_peugeot_508e_142_energy”) |
float - states(“var.peugeot_508e_last_energy”) | float ) }}”

gives a real value “15.02”
When i put his in the action of an automation It doesn’t work and gives an unknown

service: var.set
data_template:
entity_id: var.peugeot508e_today_energy
value: >-
“{{ ‘%0.2f’ | format (states(“sensor.north_porch_peugeot_508e_142_energy”) |
float - states(“var.peugeot_508e_last_energy”) | float ) }}”

I tried a lot of differnt formats but no result

So i need some help here please

Remove the double-quotes wrapping the template.

value: >-
  {{ '%0.2f' | format(states("sensor.north_porch_peugeot_508e_142_energy") | float(0) - 
    states("var.peugeot_508e_last_energy") | float(0) ) }}

I also recommend that you supply the float filter with a default value. For example, float(0) means that if float cannot convert the supplied value, it will use 0.

Thank you for your help. Found out that there was a missing underscore in the entity. Somtimes you copy and make a mistake in deleting things. Stupid.
entity_id: var.peugeot508e_today_energy should have been entity_id: var.peugeot_508e_today_energy

You still need to remove the double-quotes.

The reason why is because you are using a multiline template. The >- means the template starts on the next line and the double-quotes will be included in the final result.

Thanks I already dit your suggestions and it works well. Thanks again