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:
So actually I’m pretty happy
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:
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' }}
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
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] }} }" }'