MQTT switch created with mosquitto_pub has 2 buttons instead of 1

When I create a MQTT switch using mosquitto_pub on the console, it gets kind of 2 buttons instead of a single toggle button.

The result looks like this:
grafik

instead of this:
grafik

What am I doing wrong?

Without knowing how you defined your switch, difficult to say.

One remedy could be

afbeelding

The command is like this:

mosquitto_pub -h $addr -p $port -u $username -P $password -t "homeassistant/switch/SW_test/config" -m "{"name": "SW test", "command_topic": "homeassistant/switch/SW_test", "unique_id": "some_SW_test", "device": {"identifiers": ["Some device"], "name": "MyDevice", "sw_version": "1.0"}}"

Adding “optimistic”: true like this

mosquitto_pub -h $addr -p $port -u $username -P $password -t "homeassistant/switch/SW_test/config" -m "{"name": "SW test", "optimistic": true, "command_topic": "homeassistant/switch/SW_test", "unique_id": "some_SW_test", "device": {"identifiers": ["Some device"], "name": "MyDevice", "sw_version": "1.0"}}"

doesn’t change anything nor does adding a state_topic.

There is a command topic, but no state topic. HA can’t know the state of the switch, hence the 2 buttons.

Thanks. Unfortunately adding optimistic or state_topic doesn’t help.

mosquitto_pub -h $addr -p $port -u $username -P $password -t "homeassistant/switch/SW_test/config" -m "{"name": "SW test", "optimistic": true, "command_topic": "homeassistant/switch/SW_test/set", "state_topic": "homeassistant/switch/SW_test/get", "unique_id": "some_SW_test", "device": {"identifiers": ["Some device"], "name": "MyDevice", "sw_version": "1.0"}}"

gives the same result.

Does the state topic effectively return the correct state ?

As long as I don’t set anything it’s “unknown”. But meanwhile I found the solution: setting the command_topic and state_topic to the same value did the trick. Thanks for your input and ideas!