GPIO switch via MQTT: "OFF" works but "ON" no response

Hi all, I am new to UA. I am learning to control the GPIO switch via MQTT. I added some automations. I control the switch “on/off” via MQTT and publish the states of “on/off” via MQTT to server. The problem is that I can only control and publish “OFF” but NO RESPONSE when control and publish “ON”.

I modified my config file as below referring Can I control non MQTT entities with MQTT?

switch:
  - platform: rpi_gpio
    ports:
       17: led

Automation:
- alias: Switch on via MQTT
  trigger:  
  platform: mqtt
  topic: home/switch
  action:
     service: switch.turn_on
     entity_id: switch.led
  
 - alias: Switch off via MQTT
   trigger:  
     platform: mqtt
     topic: home/switch
  action:
    service: switch.turn_off
    entity_id: switch.led

- alias: Publish Switch on
  trigger:  
    platform: state
    entity_id: switch.led
    state: 'on'
  action:
    service: mqtt.publish
   data:
     topic: 'home/switch/state'
       payload: 'on'
  
- alias: Publish Switch off
   trigger:  
    platform: state
    entity_id: switch.led
    state: 'off'
  action:
    service: mqtt.publish
  data:
     topic: 'home/switch/state'
  payload: 'off'

Please ignore the indent problem. And when I use AWS IoT console to send

{
"switch.led": "on"
}

it has no response. But “off” works. I am wondering where I did wrong??

Thank you!

You have the same trigger in switch on and off. If there’s no payload given in the mqtt trigger, it triggers every time a value is sent. No matter what you send, it always triggers both automations, and the last is off.
Use payload: 'on' and payload: 'off' in the mqtt triggers and it shoud work.