Changing logic to text, Jinja

What am i doing wrong or don’t understand ?

I have a template in jinja who’s giving the wanted result in the editor but I cannot find how to implement in the lovelace screen.
The file is named niveau.ninja and placed in custom_templates.
Looks to me it’s doing nothing, I tried to put the template in the config file but that gives me a lot of errors, so help wanted to understand why or what to do.
I prefer jinja, that’s more clear.

sensor:
  - platform: template
    sensors:
      niveau_piscine:
        friendly_name: "Niveau"
        unit_of_measurement: "State"
        value_template: >-
          {% set mapper =  {
            '4095' : 'bon',
            '0' : 'à remplir' } -%}
          {%- set state =  states('sensor.niveau_piscine_2') -%}
          {{ mapper[state] if state in mapper else 'inconnu' }}

Tks in advance, regards Robert.

Then each file must contain one or more macros:

{% macro foo(smth) -%}
  ...
{%- endmacro %}

Docs

Thanks for your reply Ildar,

The text is ‘For example, you might define a macro in a template’,
then I understand you MIGHT, not you MUST.
I don’t know what to define in a macro.

I assume you mean .jinja? The jinja files in custom_templates are for macros only, that is why you gor the response above. But what you display seems to be a template sensor, which is yaml (with jinja inside).

If you have a template.yaml it goes in there (everyting indented one level less), if you have no template.yaml file it can also be placed below below a template: line in configuration.yaml. If you are unclear on the difference between the two methods, check:

Then try this as a starting point:
add this into configuration.yaml:

sensor:
  - platform: template
    sensors:
      niveau_piscine:
        friendly_name: "Niveau"
        unit_of_measurement: "State"
        value_template: >-
          {% set mapper =  {
            '4095' : 'bon',
            '0' : 'à remplir' } -%}
          {%- set state =  states('sensor.niveau_piscine') -%}
          {{ mapper[state] if state in mapper else 'inconnu' }}

Thanks Edwin and Ildar,

I have put it in the configuration for the moment, gives me a entitiy sensor.niveau_piscine_2 Integration: Template but still no conversion.

What do you mean “no conversion”?
What is a state of your entity?
Use Dev tools → State to see what is inside your entity.
Also, use Dev tools → Template for testing your templates.

The problem in the config file is that you have to restart every time.

In your reply is the state command in
states(‘sensor.niveau_piscine’) missing, I added it and still no result however it works in the template editor

You don’t have to restart Home Assistant to reload Template entities.

Use this:

Developer Tools → YAML → Reload Template Entities

Also when in the configuratution file ?

The entity is 0 the result in the editor ‘à remplir’ but from the template in the config file ‘unknown’

  • The entity_id of the Template Sensor you have configured is sensor.niveau_piscine.

  • The entity_id referenced by the Template Sensor in its value_template is sensor.niveau_piscine.

To compute its value it is referencing its own value. That’s incorrect.

What is the entity_id of the sensor that actually measures the pool level and reports a number? It cannot be the Template Sensor itself.

Docs:

Either reload templates as Taras proposed - or create template sensors in UI:


(although creating template sensors in UI has less possibilities than crating them in yaml)

You’re right, my system has changed automaticly the sensorname by adding _3
I’ve changed the code but won’t help me.
In the template editor works it, in the configuration file it still won’t.

The UI is too complicated for me Ildar, I don’t know what to fill in.
I’ve programmed in Z80 machine language, doïng so now in Python is already a big step.

OK, let’s try it together:
Go to Settings → Helpers:

Select “Create helper”:

Select “Template”:

Select “template a sensor”:

Then define your sensor:

2 Likes

I have filled in exactly the same with the same error on the bottom line but HAS is not too happy with it.

So what you’re saying is that the example you posted here isn’t what is actually in your system? If that’s the case then it makes it exceptionally difficult for us to solve your problems.

The Template Editor only checks the Jinja2 template. It doesn’t validate the Template Sensor’s configuration.

Obviously you’re still doing something wrong in the configuration.yaml file.

It’s not python. The example you posted is a combination of YAML and Jinja2.

Creating a Template Sensor helper via the UI is far simpler than creating one using YAML. The UI also validates the Template Sensor’s configuration and reports its computed value.

The screenshot you posted still shows your template is self-referential. I have already pointed out that this is invalid.

What is the entity_id of the sensor that reports the pool’s level as a number? It cannot be the same entity_id as the Template Sensor you are creating.