Yaml/jinja2 if then else in event for openHasp button matrix

I am using an openHasp button matrix. On pressing one of the buttons, a “down” event is triggered and a variable “val” is returned whose content is the number of the button that was pressed.
The following code extract in the configuration.yaml file works fine:

     event:
        "down":
          - service: variable.set_variable 
            data: 
              variable: thermroom
              value: 'climate.trv_bed1'

but I need to set the value of the variable thermroom depending on which button of the button matrix was pushed. I am trying it this way:

       event:
         "down":
           - service: variable.set_variable
             data:
               variable: thermroom
               value: >
                 {% if val == 0 %}
                 'climate.trv_bed1'
                 {% if val == 1 %}
                 'climate.trv_bed2'
                 {% if val == 2 %}
                 'climate.trv_bed3'
                 {% endif %}

which gives me the following cryptic (at least for me as an unexperienced yaml coder) error message:

Invalid config for [openhasp]: template value should be a string for dictionary value @ data[‘openhasp’][‘plate’][‘objects’][7][‘event’][‘down’][0][‘data’]. Got OrderedDict([(‘variable’, ‘thermroom’), (‘value’, “{% if val == 0 %} ‘climate.trv_bed1’ {% if val == 1 %} ‘climate.trv_bed2’ {% if val == 2 %} ‘climate.trv_bed3’ {% endif %}”)]). (See /config/configuration.yaml, line 217).
I tried other ways of indenting the “ifs” but whatever else I tried, my StudioCodeServer editor complains about bad indentations…
Can anyone point me in the right direction with those conditional statements?

(other tests show me that the contents of the “val” are correct and correspond to the button that was pressed, so the problem is not in the “val” value)

Each if must be terminated by an endif
In you case, the “in-between” ifs must be elif

See

https://jinja.palletsprojects.com/en/2.10.x/templates/#if

1 Like

I am known to be rather absent-minded and this confirms it… thanks for waking me up… works fine now.