I want to have my devices being detected via MQTT automatically. Therefore I’m doing the following:
1. Configuration via MQTT automatic discovery
MQTT Topic: homeassistant/light/myLight/config
{
"name":"myLight",
"unique_id":"myLight1",
"command_topic":"xyz/set/myLight",
"state_topic":"xyz/status/myLightState",
"value_template":"{{ value_json.value }}",
"schema":"json",
"brightness":false
}
Additionally I tried adding the folowing lines to define the values for
´´´
“payload_off”: false,
“payload_on”: true,
but my payloads are not recognized either.
2. Command Topic payload expectation in JSON by the device
{
"action":"toggle",
"value":true
}
3. State Topic payload being sent out in JSON
{
"value":false
}
**Questions: What do I have to set in the configuration in 1 to …**
**send the command topic in 2 and …**
**react correctly to the state topic in 3?**
I assume it can be done at all (I'm successfully doing this in the MQTTThing plugin of HomeBridge but I'm new to HomeAssistant)
However I've looked at the piece of code being responsible for this and it looks rather static to me (see here for the "State")
homeassistant/components/mqtt/light/schema_json.py, lines 312ff:
defstate_received(msg):
“”“Handle new MQTT messages.”""
values=json.loads(msg.payload)
ifvalues["state"] =="ON":
self._state=True
elifvalues["state"] =="OFF":
self._state=False
elifvalues["state"] isNone:
self._state=None
The following is received in the log for the state topic being evaluated:
2022-03-29 21:27:43 ERROR (MainThread) [homeassistant.util.logging] Exception in state_received when handling msg on ‘xyz/status/myLightState’: ‘{“value”:false}’
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/components/mqtt/debug_info.py”, line 47, in wrapper
msg_callback(msg)
File “/usr/src/homeassistant/homeassistant/components/mqtt/light/schema_json.py”, line 316, in state_received
if values[“state”] == “ON”:
KeyError: ‘state’