Mqtt value template | mqtt select options

Mqtt value template | mqtt select | options

Hello together, I need help with creating a simple template. (This syntax confuses me :wink: I appreciate everyone how has got an idea.)

I have got some programmed esp32 devices, which uses the nice mqtt discovery functions, to add a mqtt select entity.

I do not want to send the names of the options; I think the number (enum) should be enough. Therefore, I need a simple template. The command template works perfect:

{{ this.attributes.options.index(value) }}

I have been tested this successfully, to get always the second option.

{{ this.attributes.options.2 }}

But the value template I do not get to work (for select option by index):

{{ this.attributes.options(value) }}

It would help if you could to provide some code so we can see what you’ve done so far… We won’t be able to help if all we have is ‘I want to do this thing with this other thing’.
How to help us help you - or How to ask a good question.

Hey, thanks for your message.

Sorry I did not know that my issue or wish is not clearly described.
I do not know if the “code” is really useful here, this is the mqtt message for discovery.

{{"name":"Mode Select","unique_id":"mode_select","stat_t":"esp_02_g/MD/se","cmd_t":"esp_02_g/cmd","cmd_tpl":" {{ this.attributes.options.index(value) }}","options":["Initialize","Led-Test","BLight","Motion","UltraSonic","Alarm-Sys","Lightning","Sys-Info"],"optimistic":"false","avty_t":"esp_02_g/state","device":{"identifiers":["esp_02"]}}

So if HA send the order for “Motion” to the micro-controller via the command topic and with help of the properly working command template

{{ this.attributes.options.index(value) }}

”motion” will become a “3” and send via mqtt. And then the microcontroller is acknowledging the order with the correct state to the state topic. Right now it only works if the microcontroller sends “Motion” back, but I do want to change is. Just a “3” should be enough. Therefore I need a value template.

So actually it is just about template and not about my coding by the way. I’m pretty close, I have been tested this successfully, to get always the 2. option. Now I just need the value inside

{{ this.attributes.options.2 }}

{{
“name”:“Mode Select”,
“unique_id”:“mode_select”,
“stat_t”:“esp_02_g/MD/se”,
“cmd_t”:“esp_02_g/cmd”,
“cmd_tpl”:" {{ this.attributes.options.index(value) }}",
“options”:[
  “Initialize”,“Led-Test”,
  “BLight”,“Motion”,
  “UltraSonic”,“Alarm-Sys”,
  “Lightning”,“Sys-Info”
  ],
“optimistic”:“false”,
“avty_t”:“esp_02_g/state”,
“device”:{
  “identifiers”:[“esp_02”]
}}

That’s the discovery string so you can actually read it.
So you send some value to esp_02_g/cmd and you read the value at esp_02_g/MD/se and it’s not the same?

I suggest a more random character string like a uuid as the unique_id BTW, using the same name will confuse you. That value is for machines and you don’t have access to look it up inside of HA.

Thanks for formatting my discovery string… I just copied it out of my coding from the micro-controller.

It think you still did not understand the problem…(of course I can change my coding on µc) But it actually does not matter.

So you send some value to esp_02_g/cmd

Just here (for the template) I need some help: If you don’t use any value template HA need always options name as a string for the state topic!!! → And I do not like this.

That’s why I need to configure a value template. So HA can just receive the index for the state topic and changes the state!

There is an example here:

I still don’t fully understand your question either, but that’s a better example than in the docs.

Please format your code with the </> button, not as a blockquote. The way you’re doing it is messing up all of the quotes.

perhaps this “mqtt select” and attr “option” is not pretty clear to you?

Whatever, here I’m pretty sure, it is just an small syntax error in my template… this ninja template is really powerful and nice but the syntax is a bit strange (at least for me, who only knows c-coding :wink:

Thanks for the mqtt select example here in the forum. I saw this already.
Well to be honest: This way I really want to ( you should ) avoid:

  1. First, saving unnecessary traffic and messages is still important for me (even nowadays) and not only a good habit.
  2. Save redundant code and duplication of definitions

Unnecessary traffic can be avoided by using template for command topic and state topic. That’s good.
But he “maps” more or less 3 times the list of strings to the options (enumerations). Well in HA select options have automatically enumerations, which can be luckily selected by options[z]
{state_attr('select.xxx','options')[z] }}

That is the reason I could already eliminate double definitions with the command template:
{{ this.attributes.options.index(value) }}

When I find the error in my value template I will post it here.

yes I found it now:
I did not know that strings have to be converted to int.

value_template":"{{ this.attributes.options[(value | int)] }}

1 Like