cflurin / homebridge-mqtt

Homebridge-mqtt is a Plugin for Homebridge.
Apache License 2.0
229 stars 39 forks source link

Set the retained flag #22

Closed cobungra closed 7 years ago

cobungra commented 7 years ago

Hello and thanks for this work, I am having great success with it. I wonder if there is a way to set the publish "retained" flag to true with homebridge-mqtt ? I am building an esp8266 pushbutton that goes to sleep, but needs to be aware of the last status of the controlled device when it wakes up. Many thanks.

cflurin commented 7 years ago

Hi, Thanks for your feedback. This will probably be possible. I'm working on an other project just now but I'll have a look at the code in the next days.

cflurin commented 7 years ago

@cobungra: are you using only one accessory? If yes, retain make sense, otherwise if you are using more than one accessory, the mqtt broker will retain the last payload of the topic homebridge/from/set

topic: homebridge/from/set
payload: {"name": "flex_lamp", "characteristic": "On", "value": true}
cobungra commented 7 years ago

@cflurin Yes only one I hope this isn't a distraction, but when I publish client.publish("homebridge/to/set", "{\"name\": \"fooby\", \"characteristic\": \"Off\", \"value\": true }"); it is pubishing as expected. I subscribe to homebridge/to/set and can see the updates in real time. However I was expecting that subscribing after a recent publish... that I should get the last published status (that had "value": true") . I thought that "true" might be the retain flag.... I fear that I am missing something. (My MQTT server is Mosquitto)

Regards and thanks again

cflurin commented 7 years ago

Try setting retain=true in your esp8266 code. Something like this:

options.retain = true;
client.publish(topic, payload, options);

BTW: there isn't a characteristic "Off".

if you implement topic: homebridge/from/response in your esp8266 code, you will get this:

{"ack":false,"message":"characteristic 'Off' undefined."}
cobungra commented 7 years ago

Brilliant! That works. (If a little embarrassing.... you were right about the error message.) Thanks @cflurin. Greetings from Australia. (hehe assuming that you are elsewhere.)

cflurin commented 7 years ago

Glad it works. Indeed I'm in Switzerland. I visited the South of Australia a long time ago, great country.