Why my template sensor isnt working?

Hey there
I did create 3 template sensors. Two of them are running, but 1 shows “not awailable”.
I have no idea why… Here is the code - it’s the third one not running “absoluteluftfeuchtigkeitunterschiedbuero”

template:
  - sensor:
# Errechnet aus der relativen Luftfeuchtigkeit und Temperatur außen die absolute Luftfreuchtigkeit in Gramm pro Kubikmeter
    - name: "absoluteLuftfeuchtigkeitaussen"
      unique_id: "AbsoluteHumidityAussen"
      unit_of_measurement: "g/m^3"
      state_class: "measurement"
      state: >
          {{ ( 1000*e**(19.016-(4064.95/(float(states('sensor.teichhalle_temperature'))+236.25)))*100/(461.66*(float(states('sensor.teichhalle_temperature'))+273.15)) * float(states('sensor.teichhalle_humidity'))/100 | float) | round (2) }}

# Errechnet aus der relativen Luftfeuchtigkeit und Temperatur im Innenraum die absolute Luftfreuchtigkeit in Gramm pro Kubikmeter
    - name: "absoluteluftfeuchtigkeitbüro"
      unique_id: "AbsoluteHumidityBuero"
      unit_of_measurement: "g/m^3"
      state_class: "measurement"
      state: > 
          {{ ( 1000*e**(19.016-(4064.95/(float(states('sensor.shellyhtbuero_temperature'))+236.25)))*100/(461.66*(float(states('sensor.shellyhtbuero_temperature'))+273.15)) * float(states('sensor.shellyhtbuero_humidity'))/100 | float) | round (2) }}
# Errechnet den Untschied der Luftfeuchte von innen und außen in Gramm pro Kubikmeter

    - name: "absoluteluftfeuchtigkeitunterschiedbuero"
      unique_id: "AbsoluteHumidityDifferenceBuero"
      unit_of_measurement: "g/m^3"
      state_class: "measurement"
      state: >
          {{ ( float(states('sensor.absoluteluftfeuchtigkeitbüro')) - float(states('sensor.absoluteLuftfeuchtigkeitaussen')) ) | float | round (2) }}

When in doubt paste in Developer Tools → Templates
There are some weird things in your code, like putting float infront of the state. But that could be the correct way for all i know :smiley:
Anyway you need to define default values for when the sensor isnt getting a value. Also in the Dev tools this would show up at once

 {{ ( (states('sensor.absoluteluftfeuchtigkeitbüro')) | float(0) - 
(states('sensor.absoluteLuftfeuchtigkeitaussen')) | float(0) ) | round (2) }}

So also check if both sensors give a valid value.

Hi,

I think your use of float() is the problem.

Try this:

state: >
  {{ ( states('sensor.absoluteluftfeuchtigkeitbüro'))|float(0) - states('sensor.absoluteLuftfeuchtigkeitaussen')) ) | float(0) | round (2) }}

…note that float(0) means the sensor will result in ‘0’ if unavailable. You can change this to what you want.

Check your entity_ids. It won’t be sensor.absoluteluftfeuchtigkeitbüro with an ü, nor will it be sensor.absoluteLuftfeuchtigkeitaussen with an L.

You can also make your templates more readable with line breaks. Using my guess at your actual entity IDs:

      state: >
          {{ (states('sensor.absoluteluftfeuchtigkeitburo')|float(0) - 
              states('sensor.absoluteluftfeuchtigkeitaussen')|float(0))
             |round(2) }}
1 Like

Thank you guys.
I changed my sensors names as @Troon told me.
And used the code from @Dujith, which is same as @jchh’s Code, but missing a }.
But that was a problem even a beginner like me could solve :o)

I think we both had typos - I was missing a ( before the 2nd states(. However, noticing this made me look closer at the line and you can remove some brackets.

I have split it onto different lines so the remaining bracket stands out as applying the |round(2) filter to the result of the subtraction. No other brackets are needed.

state: >
  {{
    (
       states('sensor.absoluteluftfeuchtigkeitburo') |float(0)
     - states('sensor.absoluteluftfeuchtigkeitaussen') |float(0)
    )
    | round(2)
  }}

thank you :o)

Blame copy paste on a phone for missing that } :smiley:
But glad it got figured out, i missed the ü too. Already edited my post if anyone comes across this