hasspi88
(Hasspi88)
December 1, 2023, 9:25am
1
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
Sir_Goodenough
((SG) WhatAreWeFixing.Today)
December 1, 2023, 9:56am
2
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.
This category is for everything about <a href="https://esphome.io/">ESPHome</a>. Topics do not have to be Home Assistant related.
zoogara
(Daryl)
December 1, 2023, 10:29am
3
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:
hasspi88
(Hasspi88)
December 1, 2023, 11:25am
4
Can you help me write a full example to complement my config file
hasspi88
(Hasspi88)
December 1, 2023, 11:10am
6
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?
nickrout
(Nick Rout)
December 1, 2023, 9:49pm
7
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
hasspi88
(Hasspi88)
December 2, 2023, 9:31am
8
Yes, the format is not the same as the documentation, so I wanted a detailed and complete example
nickrout
(Nick Rout)
December 2, 2023, 9:55am
9
What exactly are you trying to do? Why is mqtt needed if you have api enabled?
hasspi88
(Hasspi88)
December 2, 2023, 10:26am
10
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}}
petro
(Petro)
December 2, 2023, 10:32am
11
This should work based on what I read about ArduinoJson .
payload: |-
root["properties"]["switch"] = 1;
1 Like
nickrout
(Nick Rout)
December 2, 2023, 11:05am
12
As i already said, not json
petro
(Petro)
December 2, 2023, 11:11am
13
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.
nickrout
(Nick Rout)
December 2, 2023, 11:24am
14
You may be right. However jsonlint.com does not agree.
petro
(Petro)
December 2, 2023, 11:25am
15
Not sure what you’re pasting in there.
hasspi88
(Hasspi88)
December 2, 2023, 11:50am
16
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
hasspi88
(Hasspi88)
December 2, 2023, 11:53am
17
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?
hasspi88
(Hasspi88)
December 2, 2023, 12:54pm
18
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
nickrout
(Nick Rout)
December 2, 2023, 7:45pm
20
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