Configure a mqtt switch where one device is used to change state and another to get state

Hi, i have the following problem.
I built a small box to open my garage door. It is based on a esp32, running a small mqtt programm,. To open/close the garage I need do send the mqtt command

DUMSHOME/GarageMarc/CMND  with the payload ON

this will switch a relais, and I have to send a new command with the payload OFF to open the relais again. when the relais switches from off to on, the garagedoor gets an impulse and toggles the garagedoor, so when it is open it closes, and viceversa.
This is used by other tools and should remain n place.
The system does not return the state of the garagedoor
So I have a zigbee dorr sensor who delivers the following paylod

{"battery":97,"contact":true,"device_temperature":15,"linkquality":3,"power_outage_count":6,"trigger_count":0,"voltage":2995}

I would like to retrieve the v alue of contact to set the state of the switch
I think zhe ya,l file should be something like

# define GarageDoorMarc
- name: "GarageDoorMarc"
  state_topic: "zigbee2mqtt/GarageMarc"
  command_topic: "DUMSHOME/GarageMarc/CMND"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  retain: true

and I need to use a template to get the value, but the template seems to be applied to the command topic, and not to read the state value.

Once this works, i will build on automation to send an mqtt command with payload ON, and once the state is false, I will send a command with the payload OFF.
Alos, in a dashboard there should be a switch to open/close the garagedoor, but that’s when the MQTT switch works

You’ll need a value_template to extract the state from its topic (value_json.contact, I guess).
The value returned from the template will be compared to state_on / state_off

Thanks, but I still have a question : I have one command topic, and one stet topic, how is determined to which topic the value_template belongs ? is the order in which the definitions are in the yaml file of importance.
May I have a value_template for the command topic and for the state topic ?

i tried the following

- name: "GarageDoorMarc"
  unique_id : garagedoormarczigbee
  state_topic: "zigbee2mqtt/GarageMarc"
  value_template: "{{ value_json.contact }}"
  command_topic: "DUMSHOME/GarageMarc/CMND"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  state_on: "true"
  state_off: "false"
  retain: true

the mqtt topic content has the folllowing content:

{"battery":97,"contact":true,"device_temperature":13,"linkquality":0,"power_outage_count":6,"trigger_count":0,"voltage":2995}

but in my homeassistant entity GarageDoorMarc, the state is still unknown

Try

- name: "GarageDoorMarc"
  unique_id : garagedoormarczigbee
  state_topic: "zigbee2mqtt/GarageMarc"
  value_template: '{{ iif(value_json.contact == True, "ON, "OFF" }}'
  command_topic: "DUMSHOME/GarageMarc/CMND"
  qos: 1
  payload_on: "ON"
  payload_off: "OFF"
  retain: true

nope, state remains unknown