How does esphome parse mqtt-json to perform the switch action?

esphome:
  name: mq
  friendly_name: mq

esp8266:
  board: esp12e
#external_components:
  # use rtttl and dfplayer from ESPHome's dev branch in GitHub
#  - source:
#      type: git
#      url: https://github.com/esphome/esphome
#      ref: beta
#    components: [ switch , mqtt ]
mqtt:
  broker: 192.168.10.160
  port: 1883
  username: ism
  password: admin888
  client_id: mq10001
# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "T3Nz9Ippk8By/rAyx8t3Q8o4D0c1eh5u88DAndztm2s="

ota:
  password: "d50b3a5af7e0e727d5ee3d8c26fa8e14"

wifi:
  ssid: "aikuai"
  password: "06336170007"
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Mq Fallback Hotspot"
    password: "qTz2H7U7btu4"

captive_portal:
output:
  - platform: gpio
    pin: 12
    id: 'generic_out'
switch:
  - platform: output
    name: "Generic Output"
    output: 'generic_out'

    on_turn_on:
      then:
        - mqtt.publish_json:
            topic: "/sys/mq10001/pub"  # 发布主题
            payload: '{"properties":{"switch":1}}'  # 修改开关状态的 JSON 格式
    on_turn_off:
      then:
        - mqtt.publish_json:
            topic: "/sys/mq10001/pub"  # 发布主题
            payload: '{"properties":{"switch":0}}'  # 修改开关状态的 JSON 格式
 
web_server:
  port: 80

I want to implement esphome received the mqtt message {“properties”:{“switch”:0}} in json format to turn off the switch, when I receive {“properties”:{“switch”:1}} to turn on the switch, does anyone know how to use it? As an example, I’ve tried to use automation but the compilation gets an error

ESPHome doesn’t even have blueprints, so this is clearly the wrong topic…
I would suggest you remove the configuration - blueprints topic and change it to the ESPHome topic.

You are trying to publish the final json format, not the correct way to use this action.

Re-read the docs, you need to specify the json in a format recognisable by the ArduinoJson library:

Can you help me write a full example to complement my config file

esphome:
  name: mq
  friendly_name: mq

esp8266:
  board: esp12e
#external_components:
  # use rtttl and dfplayer from ESPHome's dev branch in GitHub
#  - source:
#      type: git
#      url: https://github.com/esphome/esphome
#      ref: beta
#    components: [ switch , mqtt ]
mqtt:
  broker: 192.168.10.160
  port: 1883
  username: ism
  password: admin888
  client_id: mq10001
# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "T3Nz9Ippk8By/rAyx8t3Q8o4D0c1eh5u88DAndztm2s="

ota:
  password: "d50b3a5af7e0e727d5ee3d8c26fa8e14"

wifi:
  ssid: "aikuai"
  password: "06336170007"
  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Mq Fallback Hotspot"
    password: "qTz2H7U7btu4"

captive_portal:
output:
  - platform: gpio
    pin: 12
    id: 'generic_out'
switch:
  - platform: output
    name: "Generic Output"
    output: 'generic_out'

    on_turn_on:
      then:
        - mqtt.publish_json:
            topic: "/sys/mq10001/pub"  # 发布主题
            payload: '{"properties":{"switch":1}}'  # 修改开关状态的 JSON 格式
    on_turn_off:
      then:
        - mqtt.publish_json:
            topic: "/sys/mq10001/pub"  # 发布主题
            payload: '{"properties":{"switch":0}}'  # 修改开关状态的 JSON 格式
 
web_server:
  port: 80

I want to implement esphome received the mqtt message {“properties”:{“switch”:0}} in json format to turn off the switch, when I receive {“properties”:{“switch”:1}} to turn on the switch, does anyone know how to use it? As an example, I’ve tried to use automation but the compilation gets an error。Can you write a complete example?

That is nothing like the example in the docs. Try

payload: root["switch"] = 0;

And if i am wrong {“properties”:{“switch”:0}} is not json anyway

Yes, the format is not the same as the documentation, so I wanted a detailed and complete example

What exactly are you trying to do? Why is mqtt needed if you have api enabled?

I only use MQTT, I want to control the esphome switch action by accepting mqtt in the specified json format, and the json format is {"properties":{"switch":1}} {"properties":{"switch":0}}

This should work based on what I read about ArduinoJson.

payload: |-
  root["properties"]["switch"] = 1;
1 Like

As i already said, not json

That is valid json. Valid json just requires double quotes for strings, and listed/mapped collections need to have a comma between each item, but no comma past the last item.

You may be right. However jsonlint.com does not agree.


Not sure what you’re pasting in there.

For example, I use the mqtt client to push a message, esphome receives the mqtt message and the json format matches the switch value of 1 to turn on the switch, and the value is 0 to turn off the switch


The format of your reply is correct, but I don’t know how to detect the usage of the MQTT JSON format in esphome, can you help me write an example?

mqtt:
  broker: "192.168.10.160"
  port: 1883
  client_id: "esphome888"
  username: "ism"
  password: "admin888"
  log_topic:
    topic: "${device_name}/logs"
    level: debug
  # 其他mqtt配置...



  on_message:
    - topic: "${device_name}/your_topic"  # 替换为你期望监听的 MQTT 主题
      then:
        - lambda: |-
            if (x["payload"]["properties"]["switch"] == 1) {
              id(your_switch_entity).turn_on();
            } else if (x["payload"]["properties"]["switch"] == 0) {
              id(your_switch_entity).turn_off();
            }

switch:
  - platform: gpio
    pin: GPIO12
    name: "Your Switch"
    id: your_switch_entity

This is my code, but it keeps getting an error when compiling

Yep, sorry, my mistake.

This is very confusing in the first post you have the esp publishing json to mqtt.

Now you seem to want to receive json via mqtt and have the esp react to that.

I think the answer to the latter is here