MQTT Discovery - manual adding - after ha restart entities greyed out

Hi All,

I was trying to add all my switches using MQTT discovery. So they come nicely into integration with their own section:

Below 2 examples of 38 in total. When I add them they working great!!!
But, when I restart HA the switches are still there but they greyed out. I can’t use them

Only when I do same thing as first execute the command below they become working again.
How can I prevent that, so they will always work when restart HA?

mosquitto_pub -h 192.168.100.23 -u myusername -P mypassword -p 1883 -t "homeassistant/switch/meek_balcony/config" -m '
{"name": "balcony",
"state_topic": "homeassistant/ha_switches/balcony",
"command_topic": "homeassistant/ha_switches/balcony",
"unique_id": "14685093-ad68-4e03-a978-6fb858e34390",
"device": {"identifiers": "meek_switches","name": "Meek switches","manufacturer": "Peter Oudenes"},
"optimistic": false,
"retain": true,
"qos": 1,
"payload_on": 1,
"payload_off": 0,
"state_on": 1,
"state_off": 0,
"icon": "mdi:nintendo-switch"}'

mosquitto_pub -h 192.168.100.23 -u myusername -P mypassword -p 1883 -t "homeassistant/switch/meek_bathroom/config" -m '
{"name": "bathroom",
"state_topic": "homeassistant/ha_switches/bathroom",
"command_topic": "homeassistant/ha_switches/bathroom",
"unique_id": "d009b4c9-37ed-4d74-a5ef-a2a239213bd5",
"device": {"identifiers": "meek_switches","name": "Meek switches","manufacturer": "Peter Oudenes"},
"optimistic": false,
"retain": true,
"qos": 1,
"payload_on": 1,
"payload_off": 0,
"state_on": 1,
"state_off": 0,
"icon": "mdi:nintendo-switch"}

Don’t use that. That option retains the command, not the state, and can lead to unexpected results.

Mosquitto option -r sets retain flag

mosquitto_pub -r  -h 192.168.100.23 -u myusername -P mypassword -p 1883 -t "homeassistant/switch/meek_balcony/config" -m '
{"name": "balcony",
"state_topic": "homeassistant/ha_switches/balcony",
"command_topic": "homeassistant/ha_switches/balcony",
"unique_id": "14685093-ad68-4e03-a978-6fb858e34390",
"device": {"identifiers": "meek_switches","name": "Meek switches","manufacturer": "Peter Oudenes"},
"optimistic": false,
"retain": false,
"qos": 1,
"payload_on": 1,
"payload_off": 0,
"state_on": 1,
"state_off": 0,
"icon": "mdi:nintendo-switch"}'

So if I understand you ok remove the “retain”: false and leave the -r?
Or remove both?

Leave the -r, that makes the auto-discovery messages retained, so HA finds it when reconnected to the mqtt broker.

You want the discovery messages retained, not the command.

1 Like

Let my try that. The Retain flag for state will be given by the device that use the topic?

Retain flag for state must come from the device.

When I check with MQTT Explorer all lines are gone after HA disovered it.
So the retain is not working?

Try publishing with mqtt explorer. There is a checkbox for retain.

When I do the -r and retain: true then everything is working ok. After restart the entities are there and working

mosquitto_pub -r  -h 192.168.100.23 -u myusername -P mypassword -p 1883 -t "homeassistant/switch/meek_balcony/config" -m '
{"name": "balcony",
"state_topic": "homeassistant/ha_switches/balcony",
"command_topic": "homeassistant/ha_switches/balcony",
"unique_id": "14685093-ad68-4e03-a978-6fb858e34390",
"device": {"identifiers": "meek_switches","name": "Meek switches","manufacturer": "Peter Oudenes"},
"optimistic": false,
"retain": true,
"qos": 1,
"payload_on": 1,
"payload_off": 0,
"state_on": 1,
"state_off": 0,
"icon": "mdi:nintendo-switch"}'