Template Sensor with template comparison | Easy Thing for Pros

Hello folks,

I want to create a template sensor which shows a ‘YES’ if the template is true and a ‘NO’ if the template is not true. The Template is a simple comparison between two entities.

I just can’t find the right starting point, although the solution is probably super simple.

Thats my comparison template:

{{ states(‘sensor.absolute_luftfeuchtigkeit’) | float <
states(‘sensor.thermal_comfort_ausen_absolute_humidity’) | float }}

To be clear. If the “sensor.absolute_luftfeuchtigkeit” is higher than the “sensor.thermal_comfort_ausen_absolute_humidity” it should reply a “YES”. If not, a “NO”

{{ 'YES' if states('sensor.absolute_luftfeuchtigkeit') | float(0) >
states('sensor.thermal_comfort_ausen_absolute_humidity') | float(0) else 'NO' }}

Note I reversed the comparison because your text suggested higher, not lower. Had I exchanged the words, then equal values would produce a different answer.

I also fixed the smart quotes. (Thanks Didgeridrew for noticing). Templates don’t like the curly ones, you should turn that of in your keyboard settings or it will drive you nuts.

I also added defaults for float conversion. This prevents errors, but may produce unexpected results when one of the sensors is unavailable. You should use an availability template if you use this in a template sensor.

Also note this cannot be used in a binary sensor anymore, because you asked for specific texts, not on and off.

1 Like

Your template has a “smart quote” infestation… :upside_down_face:

1 Like

I copied it without noticing, apparently. They must be contagious :slight_smile: YES and NO were added by me, they are fine. I’ll fix the others.

1 Like

Thanks for the fast reply! That helped me a lot! :grinning: … and a bit chatGPT :grin:

My code in the configuration.yaml would now look like this:

template:
  - sensor:    
      - name: Luftfeuchtigkeit Vergleich
        unique_id: luftfeuchtigkeit_vergleich
        state: >-
          {{'YES' if states('sensor.absolute_luftfeuchtigkeit') | float(0) >
              states('sensor.thermal_comfort_ausen_absolute_humidity') | float(0) else 'NO'}}
        availability: >-
          {{ states('sensor.absolute_luftfeuchtigkeit') not in ['unknown', 'unavailable'] and
             states('sensor.thermal_comfort_ausen_absolute_humidity') not in ['unknown', 'unavailable'] }}

Your post wasn’t the solution. that was given to you by edwin. you should mark their post as the solution.

And it’s not advised to use ChatGPT. It likes to hallucinate when it comes to HA code and a novice can’t tell where the problem is so it leads to frustration for you and those trying to help you.

I don’t understand the problem. ChatGPT perfectly explained a template sensor in the new format and helped me with the availability and syntax. The template at the beginning was mine.

Well, sometimes AI is not wrong, but more often than not it is convincing looking nonsense.

I’m not saying this is you. You asked a normal question, I was happy to help. But as much as I like to help people learn by helping them find mistakes in things that they put some thought in, I really, really hate it when people ask ChatGPT to spit out something they do not understand, and then ask me to invest my spare time to fix mistakes in it. Time they were too lazy to put in. And believe me, it happens quite often. So much so, I sometimes wonder if I should stop helping.

So if anyone wants to ask ChatGPT or another LLM, then they are welcome to do so. But also ask AI to fix the problems, not me. Ever. Because AI does not understand. It mimics.

1 Like

I didn’t say it always hallucinates. I said it likes to hallucinate. So much so that using ChatGPT to help someone else is banned on the forum and if you use ChatGPT in your own post then you have to disclose that’s where it came from.

I see. I only used ai to better understand the new format of the template sensor and to correct the syntax of my code. I have always used the legacy format and never made a comparison in a sensor. Generally, I know how to use Home Assistant and would never ask for help here for ai generated code, which I have no idea about.

I didn’t want to cause any trouble with my statement. Many thanks for your help and opinion. I really appreciate it.