Template sensor calculation - whats wrong?

I have two temperature sensors and want to calculate the temperature difference and use that as a new sensor.

{{ states("sensor.temperature_livingroom") }} - {{ states("sensor.temperature_bedroom") }}

I used Dev Tools → Template to build the jinja code and this shows: “22 - 18”

The state of the new sensor is ‘unknown’ instead of ‘4’ what i expected.

Am i doing something wrong?

State values are strings. You need to convert them to numbers before subtracting them. Also the subtraction has to happen inside the template (not between two templates as you have shown above).

'{{ states("sensor.temperature_livingroom")|float(0) - states("sensor.temperature_bedroom")|float(0) }}'