Template Mapper for value | need help for my

Hello together,
I can not find the error in my mapper for a value template.
I have got some programmed esp32 devices, which uses the nice mqtt discovery functions, to add a mqtt entity. They just send a ‘2’, ‘3’, etc. etc to the correct topic.

Everything works fine, if I program the value template like this with these if-statements:

"val_tpl":"{% if (value|int) == 3 %} 'arming' {% elif (value|int) == 5 %} 'armed_home' {% elif (value|int) == 6 %} 'armed_away' {% elif (value|int) == 9 %} 'pending' {% elif (value|int) == 10 %} 'triggered' {% elif (value|int) == 11 %} 'disarmed' {% else %} this.value {%endif %}"

So actually I’m pretty happy :slight_smile:
But now I want to clean up the coding with this mapper functions:

"val_tpl":"{{ {% set mapper = ['unavailable','arming','arming','arming','arming','armed_home','armed_away','xx1','xx2','pending','triggered','disarmed','unavailable'] %} {% set state = ((value)|int) %} {{ mapper[state] if state in mapper else 'unknown' }} }}"

I can not find any issues, also these topics are really helpful:

I also have been looking at this topic:

But I always got this WARNING from HA, for all states:

2024-07-06 10:34:08.103 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'unknown' is undefined when rendering '{% set mapper = [unavailable,arming,arming,arming,arming,armed_home,armed_away,xx1,xx2,pending,triggered,disarmed,unavailable] %} {% set state = ((value)|int) %} {{ mapper[state] if state in mapper else unknown }}'
2024-07-06 10:34:08.104 WARNING (MainThread) [homeassistant.components.mqtt.alarm_control_panel] Received unexpected payload: 1
2024-07-06 10:34:11.062 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'unknown' is undefined when rendering '{% set mapper = [unavailable,arming,arming,arming,arming,armed_home,armed_away,xx1,xx2,pending,triggered,disarmed,unavailable] %} {% set state = ((value)|int) %} {{ mapper[state] if state in mapper else unknown }}'
2024-07-06 10:34:11.063 WARNING (MainThread) [homeassistant.components.mqtt.alarm_control_panel] Received unexpected payload: 2

you aren’t making a dictionary; you just made a list.

{% set items = ['unavailable','arming','arming','arming','arming','armed_home','armed_away','xx1','xx2','pending','triggered','disarmed','unavailable'] %}
{% set index = value | int(0) %}
{{ items[index] if 0 <= index < items | length else 'unknown' }}

Thank you for your help, I really appreciate this.
unfortunately I get the same error (or warning message in HA log) :frowning:

I do not understand your explanation regarding list and dictionary.

  1. You changed the 2 variables name mapper -> items and state-> index, which make more sense but do not change anything.
  2. You added a second if condition in case index < 0

I changed the entire if statement to work with indexes instead of keys. And the code works fine.

You’re not looking at the correct template then or you’re doing something else wrong

1 Like

I’m really sorry, yes you are completely right! :slight_smile:

Your code is working properly, I have tested within this template tool and it gives back the correct alarm state name: => xx1
And my version is definitely not working => unknown
Sorry i have been too impatient :zipper_mouth_face:

And you are also right, there must be somewhere else the problem:
The logging say, that ‘arming’ is undefined… that’s strange, because that is exactly HA demands. (I have been tested this, by sending just ‘arming’ without any value template and it works.)

2024-07-08 00:22:28.157 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: ‘arming’ is undefined when rendering ‘{% set items = [unavailable,arming,arming,arming,arming,armed_home,armed_away,xx1,xx2,pending,triggered,disarmed,unavailable] %} {% set index = value | int(0) %} {{ items[index] if 0 <= index < items | length else unknown }}’
2024-07-08 00:22:28.157 WARNING (MainThread) [homeassistant.components.mqtt.alarm_control_panel] Received unexpected payload: 1

You’re not using the template I provided. You’re using a template where the items in the list do not have quotes, which is the cause of that error. Just copy/paste the template I provided, no need to alter it.

Hey, thanks. I have used your template for sure. (copy and paste)!
The problem is behind the template, when formatting the string for sending it from my mcu. I’m confused with the single and double quotes, but there is the problem:

This is the short version when I’m sending your template.

'{"name":"xxx","unique_id":"xxx","stat_t":"xxx","val_tpl":"{ {% set items = ["Test0","Test1"] %} {% set index = value | int(0) %} {{ items[index] }} }" }'

This version results in the HA dev tool:

{“name”:“xxx”,“unique_id”:“xxx”,“stat_t”:“xxx”,“val_tpl”:“{ Test1 }”

While my first version, which is working fine, looks like:

{"name":"xxx","unique_id":"xxx","stat_t":"xxx","val_tpl":"{% if (value|int) == 0 %} 'test0' {% elif (value|int) == 1 %} 'test1' {% else %} this.value {%endif %}" }

This version results in the HA dev tool:
{“name”:“xxx”,“unique_id”:“xxx”,“stat_t”:“xxx”,“val_tpl”:" ‘test1’ " }

So there are quotes missing in the first version, but I do not know how to add additional ones.