MQTT Select won't publish the new selected value

Hello,
I’m trying to set-up MQTT Select with mitigate results. I’m running 2022.3.0
Here is the current code:

- platform: mqtt
  name: "Sélecteur"
  object_id: "611"
  unique_id: "611"
  options:
    - "Off"
    - Level1
    - Level2
    - Level3
  state_topic: "domoticz/out/611"
  value_template: >
    {% if value_json.svalue1 ==  "0" %} Off
    {% elif value_json.svalue1 == "10" %} Level1
    {% elif value_json.svalue1 == "20" %} Level2
    {% elif value_json.svalue1 == "30" %} Level3
    {% endif %}
  command_topic: "domoticz/in"
  command_template: >
    {% if states("select.611") == "Off" %} 
      {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": 0 }
    {% elif states("select.611") == "Level1" %} 
      {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": 10 }
    {% elif states("select.611") == "Level2" %} 
      {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": 20 }
    {% elif states("select.611") == "Level3" %} 
      {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": 30 }
    {% endif %}
  optimistic: false
  qos: 0
  retain: true

From the remote side, I get the Select updated as expected. However, when an option is selected on HA side, the value that was set before it has been changed by the dropdown menu gets published whereas it is expected that the new value gets published. In developer/template, I checked that the template provides the expected result.

Could someone help me to spot the error?
Thank you

My guess would be that the state of select.611 is not yet updated when command_template is evaluated.

Try this:

- platform: mqtt
  name: "Sélecteur"
  object_id: "611"
  unique_id: "611"
  options:
    - "Off"
    - Level1
    - Level2
    - Level3
  state_topic: "domoticz/out/611"
  value_template: >
    {% if value_json.svalue1 ==  "0" %} Off
    {% elif value_json.svalue1 == "10" %} Level1
    {% elif value_json.svalue1 == "20" %} Level2
    {% elif value_json.svalue1 == "30" %} Level3
    {% endif %}
  command_topic: "domoticz/in"
  command_template: >
    {% if value == "Off" %} 
      {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": 0 }
    {% elif value == "Level1" %} 
      {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": 10 }
    {% elif value == "Level2" %} 
      {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": 20 }
    {% elif value == "Level3" %} 
      {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": 30 }
    {% endif %}
  optimistic: false
  qos: 0
  retain: true

I would also try to further simplify the template:

- platform: mqtt
  name: "Sélecteur"
  object_id: "611"
  unique_id: "611"
  options:
    - "Off"
    - Level1
    - Level2
    - Level3
  state_topic: "domoticz/out/611"
  value_template: >
    {% if value_json.svalue1 ==  "0" %} Off
    {% elif value_json.svalue1 == "10" %} Level1
    {% elif value_json.svalue1 == "20" %} Level2
    {% elif value_json.svalue1 == "30" %} Level3
    {% endif %}
  command_topic: "domoticz/in"
  command_template: >
    {% set value_map = {
           "Off": 0,
           "Level1": 10,
           "Level2": 20,
           "Level3": 30,
        }
    %}
    {"command": "switchlight", "idx": 611, "switchcmd": "Set Level", "level": {{ value_map[value] }}}
  optimistic: false
  qos: 0
  retain: true

@ondras12345 : thank you you so much for your help. I confirm your guess, state is not yet defined, which is BTW disturbing, passing the value is the solution. And setting a dictionnary is much more elegant.
May I ask a side question? I’m wondering whether options of the select menu can be dynamically defined. I read a few things on this matter without having found the solution.

I think you would have to use MQTT discovery to redefine the entity each time you want to change the options. There seems to be no other easy way to do it.

I prefer not to use MQTT AD in order to keep control on what is created and how. I guess I’ll have to define an automation and use the set_options service, isnt’it?

I find very hard to understand the difference between select and input_select. I must admit I’m lost at this point. Any insight that could be helpfull?

I believe the set_options service is only available for input_select.
The difference between input select and MQTT select is that input select is a helper. That means it’s value is purely virtual and does not represent the state of real hardware. MQTT select, on the other hand, gets its state from MQTT messages and sends them when it is requested to change state. The MQTT messages can then control a real hardware device.
If MQTT select does not support what you need, you might have to implement its functionality using an input select and create an automation that sends MQTT messages when its state changes.