I'm too stupid: Substracting two values

Hi. Title says it all. Please, pretty please tell me my mistake.

my (complete) templates.yaml is as follows:

- sensor:
    #### Substraktion
    name: "Validity Live"
    unique_id: 84d3ab17-c7c1-4345-9705-2c11702faa9b
    state: >
      {{ '%0.1f' | format (states('sensor.t_h_sensor_2_temperature_2')  -
                            states('sensor.openweathermap_temperature')) }}
    unit_of_measurement: "°C"

I got rid of the error message which was because of some spacing or brackets or something, took me an hour. It just doesn’t do anything, still.

I just want a dummy variable of live outside temperature and reported outside temperatur difference.

If someone can tell me how to make that an absulte, that’d be a bonus.

Update:

This works. Don’t really know why. Guess I have to float the string values.

- sensor:
    #### Substraktion
    name: "Validity Live"
    unique_id: 84d3ab17-c7c1-4345-9705-2c11702faa9b
    state: >
      {{ (states('sensor.t_h_sensor_2_temperature_2')|float  -
          states('sensor.openweathermap_temperature')|float) }}
    unit_of_measurement: "°C"

A string value is just a sequence of characters, like “abc” so your original equation is like executing math against text. Using float designate the state as a number.

the biggest thing to remember that states are always strings. Even if it looks ike a number it’s really just a string.

so both of these are strings:

sensor.some sensor = hello

states('sensor.some_sensor') = 'hello' --> string

sensor.some_other_sensor = 15

states('sensor.some_other_sensor') = '15' --> string

sensor.some_other_third_sensor = 230.45

states('sensor.some_other_third_sensor') = '230.45' --> string

that said you can’t subtract strings. so you need to convert them to a number first - either integer (int) or float (float).

But that only applies to the entity’s state. it’s attributes will always be what they look like.