poudenes
(Poudenes)
November 18, 2021, 8:35am
1
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"}
francisp
(Francis)
November 18, 2021, 8:46am
2
poudenes:
"retain": true,
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"}'
poudenes
(Poudenes)
November 18, 2021, 8:47am
3
So if I understand you ok remove the “retain”: false and leave the -r?
Or remove both?
francisp
(Francis)
November 18, 2021, 8:48am
4
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
poudenes
(Poudenes)
November 18, 2021, 8:51am
5
Let my try that. The Retain flag for state will be given by the device that use the topic?
francisp
(Francis)
November 18, 2021, 8:54am
6
Retain flag for state must come from the device.
poudenes
(Poudenes)
November 18, 2021, 9:01am
7
When I check with MQTT Explorer all lines are gone after HA disovered it.
So the retain is not working?
francisp
(Francis)
November 18, 2021, 9:02am
8
Try publishing with mqtt explorer. There is a checkbox for retain.
poudenes
(Poudenes)
November 18, 2021, 9:15am
9
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"}'