Unable to publish MQTT switch

I am able to publish auto discovered lights over MQTT but when I attempt to publish a switch it does not get discovered. I am publishing the following payload:

{“name”:“iCue Control”,“unique_id”:“icue_control”,“platform”:“mqtt”,“state_topic”:“homeassistant/switch/icue2mtt/icue_control/state”,“command_topic”:“homeassistant/switch/icue2mtt/icue_control/set”,“payload_on”:“ON”,“payload_off”:“OFF”,“state_on”:“ON”,“state_off”:“OFF”,“schema”:“json”,“optimistic”:true,“qos”:1,“retain”:false}

With the topic: homeassistant/switch/icue2mtt/icue_control/config

On state message I am returning the following payload:

{“state”:“ON”}

Any ideas what I am doing wrong?

Not without seeing your automation or script.

To recap, you’re using MQTT Discovery to create an MQTT Switch. You’re sending the following payload to this topic: homeassistant/switch/icue2mtt/icue_control/config

{"name":"iCue Control",
"unique_id":"icue_control",
"platform":"mqtt",
"state_topic":"homeassistant/switch/icue2mtt/icue_control/state",
"command_topic":"homeassistant/switch/icue2mtt/icue_control/set",
"payload_on":"ON",
"payload_off":"OFF",
"state_on":"ON",
"state_off":"OFF",
"schema":"json",
"optimistic":true,
"qos":1,
"retain":false}

You’ve specified that it should the JSON schema. Where in the documentation for MQTT Switch does it indicate that it supports the schema option?

Also is there a 255 character limit to payloads?

I don’t think so because a discovery message supports such a broad range of options that it can easily exceed 255 characters. Option names can be abbreviated but that’s a courtesy extended to devices that might have limited storage capacity:

Configuration variable names in the discovery payload may be abbreviated to conserve memory when sending a discovery message from memory constrained devices.

It even supports an abbreviation of the base topic (tilde is used like a variable):

A base topic ~ may be defined in the payload to conserve memory when the same topic base is used multiple times.

However, nowhere in the docs does it say the message must be <= 255 in length.

In this particular example, even if MQTT Switch supported JSON schema (like MQTT Light does), it lacks a state_value_template so it has no clue how to extract ON from {"state:"ON"}.

1 Like

Thanks for the input. This is not a automation script but a C#.net MQTT client for exposing some functionality on a PC. I did realise I had made some assumptions and used a payload designed for a light as the model. I simplified my discovery payload to be:

{
"name":"iCue  Control",
"unique_id":"icue_control",
"state_topic":"homeassistant/switch/icue2mtt/icue_control/state",
"command_topic":"homeassistant/switch/icue2mtt/icue_control/set"
}

It then discovered it. I changed the publish message sent in response to the state topic to simply return the string "ON" or "OFF" depending on the sate of the switch. It seems to work with some quircks:

*  After toggling the switch from Home Assistant it jumps back to the original state after a few seconds and then after a bit longer it reverts back to the correct value
* The client seems to slow down home assistant. I am not sure if this could be addressed with the retain flags of QOS or some such

Sorry, I don’t understand what that sentence means.

If your MQTT client publishes ON to:

homeassistant/switch/icue2mtt/icue_control/state

then Home Assistant will indicate switch.icue_control is on. The state_topic is used to represent the switch’s current state.

If you change the state of switch.icue_control from off to on, it will publish ON to:

homeassistant/switch/icue2mtt/icue_control/set

It will then expect to receive an immediate response consisting of the message ON via:

homeassistant/switch/icue2mtt/icue_control/state

If it fails to promptly receive this response message, it will immediately set its state back to off.

I suspect there may be a problem of latency (i.e. delay) between Home Assistant, the MQTT broker, and your MQTT client.