Hello together, I need help with creating a simple template. (This syntax confuses me 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):
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.
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.
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
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!
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
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:
First, saving unnecessary traffic and messages is still important for me (even nowadays) and not only a good habit.
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.