Real newbie need help on MQTT switch

Hi

It’s my first step into HA and it look’s like a real mightmare to setup my own project based on ESP32 into HA.

My ESP32 is publishing a JSON payload into HA MQTT broker under homehorn topic and HA should send commands on device_1 topic.
It’ look’s like that :

{
    "Device": 1,
    "PTEC": "HP",
    "IINST1": 9,
    "IINST2": 1,
    "IINST3": 2,
    "PCONSO": 2760,
    "Mesure_niveau_puits": 0,
    "Sat_pompe": 0,
    "Temperature": 22,
    "Etat_porte": "close",
    "Etat_hammam": "OFF"
}

I’d like to toggle the hammam on & off.
Unfortunately, the switch doesn’t get the status from state_topic & value_template.
Maybe my understanding is wrong

Thank for you support

Here the yaml file I wrote :

  customize:
    # Add an entry for each entity that you want to overwrite.
    sensor.etat_hammam:
      icon: mdi:hot-tub
      friendly_name: Etat Hammam
      device_class: Binary_Sensor.none
      
sensor device_1:
  - platform: mqtt
    name: "Etat_Hammam"
    state_topic: "homehorn"
    value_template: "{{ value_json.Etat_hammam }}"

switch:

  - platform: mqtt
    name: "Hammam"
    state_topic: "homehorn"
    value_template : "{{ value_json.Etat_hammam }}"
    command_topic: "device_1"
    payload_on: "{\"device\":1,\"function\":5,\"cmd\":\"ON\"}"
    payload_off: "{\"device\":1,\"function\":5,\"cmd\":\"OFF\"}"
    state_on: "Marche"
    state_off: "Arret"

The default values for state_on: and state_off: are ON and OFF (see documentation). That is precisely what Etat_hammam normally reports so there is no need for state_on: "Marche" and state_off: "Arret" in the switch’s configuration. Remove them.

switch:
  - platform: mqtt
    name: "Hammam"
    state_topic: "homehorn"
    value_template : "{{ value_json.Etat_hammam }}"
    command_topic: "device_1"
    payload_on: "{\"device\":1,\"function\":5,\"cmd\":\"ON\"}"
    payload_off: "{\"device\":1,\"function\":5,\"cmd\":\"OFF\"}"

Thks Taras for your quick support.

Even without state_on and off I can’t get the status in the switch but it works now if I display the entity in a card.
I triple checked the topic and value template.

Can I use value_template in a switch ? Json doesn’t like to work …

After spending hours on google I understood that “payload_on” & “payload_off” was not well used. I just removed it

but didn’t get why the state doesn’t work in the switch where as it works perfectly in the sensor with exactly the same state_topic or value_template…
Any idea ?

switch:
  - platform: mqtt
    name: "Hammam"
    state_topic: "homehorn"
    value_template : "{{ value_json.Etat_hammam }}"
sensor device_1:
  - platform: mqtt
    name: "Etat_Hammam"
    state_topic: "homehorn"
    value_template: "{{ value_json.Etat_hammam }}"

I finaly gave up…
I implemented a dedicate topic where I manage a payload without JSON but only ON/OFF.
It works…

  - platform: mqtt
    name: "Hammam"
    state_topic: "device_1/state/fct_5"
    command_topic: "device_1/cmnd/fct_5"