Jinja Help needed: How to display the value of a variable?

Hi,
I want to create a binary sensor whose status is formed from the difference of 2 temperatures. In addition, I would like to have the current temperature difference as an attribute.
In the template editor this is as expected:

But when I create the template sensor in the config.yaml, the attribute ist only rendered as td.

- binary_sensor:
  - name: "Heizkoerper_Diele"
    unique_id: "ffd69dd1-0278-4ced-8038-896b5ef38c58"
    state: >
      {% set td = states('sensor.testlumi_lumi_weather_72b24b04_temperature') | float() 
      - states('sensor.bt_klima_diele_temperature') | float() %}
      {% if(td > 1.5) %} on {%else%} off {% endif%}
    attributes:
      tempdiff:  td

I suspect that the variable “td” is no longer present. How can I solve this?

You need to put td in double clamps to make it print put its value, like {{ td }}

Nope, I get an error message from Studio Code Server:

You can try a couple things:

  1. Make td a float, or even round it to 2 decimals or so. Otherwise you can get 15 or whatever.
  2. Save it anyway. File Editor accepts {{ td }} for me
  3. Replace it with "{{ td }}" (quotes)?
- binary_sensor:
  - name: "Heizkoerper_Diele"
    unique_id: "ffd69dd1-0278-4ced-8038-896b5ef38c58"
    state: >
      {% set td = (states('sensor.home_temperature') | float() - states('sensor.terraza_temperature') | float() )|float|round(2) %}
      {% if(td > 1.5) %} on {%else%} off {% endif%}
    attributes:
      tempdiff: {{td}}

“{{td}}” is represented as {{td}}, no matter if integer or float, but at least no error message

None of the suggestions you have received so far is correct for the simple reason that a Jinja2 variable defined in an option is not defined outside of the option. Its “scope” is limited to the option where it’s defined.

The Jinja2 variable td is defined in the states option and cannot be referenced in another option, like you tried to do in tempdiff.

You will have to compute the value again in tempdiff.

- binary_sensor:
  - name: "Heizkoerper_Diele"
    unique_id: "ffd69dd1-0278-4ced-8038-896b5ef38c58"
    state: >
      {% set td = (states('sensor.home_temperature') | float(0) - states('sensor.terraza_temperature') | float(0)) | round(2) %}
      {% if(td > 1.5) %} on {%else%} off {% endif%}
    attributes:
      tempdiff: "{{  (states('sensor.home_temperature') | float(0) - states('sensor.terraza_temperature') | float(0)) | round(2) }}"

NOTE

You should supply the float filter with a default value. If you don’t and it receives a value it can’t convert (like unavailable) it will fail with an error message. In the example I posted, I have specified 0 as the default value.

You may also wish to consider using the availability option to confirm sensor.home_temperature and sensor.terraza_temperature have numeric values.


You may also wish to vote for this Feature Request:

1 Like

That scope definition is really annoying.
I checked that there were no loop scopes, because I knew that could cause issues, but I did not know this scope issue.
i am glad I do most of my stuff in NodeRed :slight_smile:

FWIW, like most programming tools, Node Red’s nodes and JavaScript variables also have a scope.

What’s missing in Template Sensors, but exists in scripts and automations, is the concept of a “script variable” (whose scope is global for the script/automation where it’s defined).

1 Like

Thank you for this explanation! Very informative.

I thought of the scoped variable (simply because I read about it last week, because I don’t know a lot about templating), but since it worked on Dev Tools Template, I assumed it wasn’t the case. My apologies for any misinformation!

The Template Editor is a Jinja2 processor for testing Jinja2 templates. It doesn’t process or validate YAML.

The Template Editor is unaware that state: and tempdiff: are separate YAML keys. It simply sees them as text and focuses exclusively on what it detects to be Jinja2 statements.

1 Like

Thanks for the explanation of the variable scope and the Template Editor. So I have to make the clalculation twice, one for the state and the other for the attribute, which is rather non-elegant.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic is resolved. This helps other users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

FYI you can link directly to the title

I can’t remember when that was added, but it was

Strange; the link I posted ends with #twoone-somebodys-answer-solved-it-28 so it should go directly to guideline 21 (but it doesn’t).

Link address:

https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#twoone-somebodys-answer-solved-it-28

Strange indeed