Template not accepted

Hello,
I have tested a template with the develloper template tool and it gives correct feedback
Template is :

Blockquote

{% if is_state(‘input_select.days’, ‘Week’)%} 2 {% elif is_state(‘input_select.days’, ‘Week-end’) %} 1 {% else%} 0 {% endif %}

Blockquote

When I include this template to give a value 2, 1, 0 to a new sensor with :

Blockquote

- platform: template
*** sensors:***
*** days_num1 :***
*** value_template : “{{% if is_state(‘input_select.days1’, ‘Semaine’)%} ‘2’ {% elif is_state(‘input_select.days1’, ‘Week-end’) %} ‘1’ {% else %} ‘0’ {% endif %}}”***
*** friendly_name: Jours 1***

Blockquote

The configuration check give the message :
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: unexpected ‘%’) for dictionary value @ data[‘sensors’][‘days_num1’][‘value_template’]. Got “{{% if is_state(‘input_select.days1’, ‘Semaine’)%} ‘2’ {% elif is_state(‘input_select.days1’, ‘Week-end’) %} ‘1’ {% else %} ‘0’ {% endif %}}”. (See ?, line ?).
What do I wrong ? I do not find the solution on the forum
Thanks in advance for your king help
Patou

Please use the </> button

Remove the outer curly braces from your template . Also you don’t need quotes around ‘2’, ‘1’, ‘0’ unless you want your value to actually be ‘2’. I suspect you really want 2. Finally you need to use the </> selector not the blockquote you used to have the text formatted properly in the question above.

1 Like

You can also use inline-if style which is slightly more compact:

- platform: template
  sensors:
    days_num1:
      friendly_name: Jours 1
      value_template: >
        {{ 2 if is_state('input_select.days1', 'Semaine') else 1 if is_state('input_select.days1', 'Week-end') else 0 }}

Basically it reads like: 2 if Semaine else 1 if Week-end else 0

The > symbol means the template starts on the next line. In that case, the template should not be delimited with any quotes. If you omit the > symbol then the tempalte must begin in the same line and must be delimited with quotes, like this:

      value_template: "{{ 2 if is_state('input_select.days1', 'Semaine') else 1 if is_state('input_select.days1', 'Week-end') else 0 }}"
1 Like

Thanks for your quick reaction it work …
I am at the beginning of my learnng !!
Patou

Your second template uses the inline if/then/else but then nests them to produce more options.
I am going to have to study that as it’s VERY compact.
Though it ‘may’ be a tad more difficult to ‘read’ (human wise) and thus maintain.
Still … “HFUHRUHURR”
:+1:
:smiley: