Dynamic MQTT LWT birth, Close & Will Property

There is a lot of dynamic features now in mqtt node red. I noticed that the broker can be dynamically setting an object message as follow:

{"broker": 192.168.1.XX, "port":1883, "force":true}

I was wondering if I can set the LWT also dynamically in this Json object. I took a quick look in the mqtt.js file and found that this object coming from node.setOptions() and there is a function for LWT with same features called createLWT()

I tried many topics with the stated properties in the function but with no hope. Any ideas?

Yes it can,

Here is a big demo i used for debugging when developing the dynamic features...

[{"id":"a44858cb4f9a7624","type":"inject","z":"a73202046ef1075d","name":"sub [{test/test3},{test/test4},\"test/test5\"]","props":[{"p":"action","v":"subscribe","vt":"str"},{"p":"topic","v":"[{\"topic\":\"test/test3\",\"datatype\":\"json\",\"qos\":0},{\"topic\":\"test/test4\",\"datatype\":\"utf8\",\"qos\":1},\"test/test5\"]","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":650,"y":1960,"wires":[["22bc0452e33ae532"]]},{"id":"c93e455cf8a3300c","type":"inject","z":"a73202046ef1075d","name":"test/test1 (datatype auto)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test1","payload":"{\"key\":\"payload should be a string because subscription datatype was 'auto'\"}","payloadType":"json","x":1570,"y":2060,"wires":[["0cd070c3e7d76af3"]]},{"id":"6278eb39fc0977f9","type":"inject","z":"a73202046ef1075d","name":"test/test2 (datatype json)","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test2","payload":"{\"key\":\"payload should be an object because subscription datatype was 'json'\"}","payloadType":"json","x":1570,"y":2100,"wires":[["0cd070c3e7d76af3"]]},{"id":"0cd070c3e7d76af3","type":"mqtt out","z":"a73202046ef1075d","name":"","topic":"","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"d98ab0d0f2c84665","x":1910,"y":2020,"wires":[]},{"id":"dcfbaf334cc4c95a","type":"inject","z":"a73202046ef1075d","name":"sub single string \"test/test1\"","props":[{"p":"action","v":"subscribe","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test1","x":620,"y":1880,"wires":[["22bc0452e33ae532"]]},{"id":"06e4355aa8357e68","type":"inject","z":"a73202046ef1075d","name":"sub single object {test/test2}","props":[{"p":"action","v":"subscribe","vt":"str"},{"p":"topic","v":"{\"topic\":\"test/test2\",\"datatype\":\"json\",\"qos\":0}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":620,"y":1920,"wires":[["22bc0452e33ae532"]]},{"id":"12aa7c7be8d1b18f","type":"inject","z":"a73202046ef1075d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test3","payload":"{\"key\": \"value\"}","payloadType":"json","x":1570,"y":2140,"wires":[["0cd070c3e7d76af3"]]},{"id":"bac9e9ae6b9da2c5","type":"inject","z":"a73202046ef1075d","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test4","payload":"{\"key\": \"value\"}","payloadType":"json","x":1570,"y":2180,"wires":[["0cd070c3e7d76af3"]]},{"id":"df8dd5cba1197ac1","type":"inject","z":"a73202046ef1075d","name":"test/test5 (with v5 props)","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"userProperties","v":"{\"fred\":\"123\"}","vt":"json"},{"p":"responseTopic","v":"test/reply","vt":"str"},{"p":"contentType","v":"JSON","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test5","payload":"{\"key\": \"value\"}","payloadType":"json","x":1570,"y":2220,"wires":[["0cd070c3e7d76af3"]]},{"id":"f4afec0a0c146668","type":"comment","z":"a73202046ef1075d","name":"publish to regular mqtt out","info":"","x":1550,"y":2020,"wires":[]},{"id":"aa82e9cf3227066c","type":"comment","z":"a73202046ef1075d","name":"Subscribe topics","info":"","x":560,"y":1840,"wires":[]},{"id":"c8afed87a5818c8f","type":"comment","z":"a73202046ef1075d","name":"connect / disconnect / other","info":"","x":600,"y":1580,"wires":[]},{"id":"86d14a88aea7f2af","type":"inject","z":"a73202046ef1075d","name":"unsub single test/test1","props":[{"p":"action","v":"unsubscribe","vt":"str"},{"p":"topic","v":"{\"topic\":\"test/test1\"}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":600,"y":2060,"wires":[["22bc0452e33ae532"]]},{"id":"439475bbd6137052","type":"inject","z":"a73202046ef1075d","name":"unsub all","props":[{"p":"action","v":"unsubscribe","vt":"str"},{"p":"topic","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":560,"y":2100,"wires":[["22bc0452e33ae532"]]},{"id":"085faf61cce5c2ea","type":"comment","z":"a73202046ef1075d","name":"Unsubscribe topics","info":"","x":570,"y":2020,"wires":[]},{"id":"22bc0452e33ae532","type":"mqtt in","z":"a73202046ef1075d","name":"Dynamic 1","topic":"","qos":"2","datatype":"auto","broker":"d98ab0d0f2c84665","nl":false,"rap":true,"rh":0,"inputs":1,"x":930,"y":1880,"wires":[["16a5af7930d4bf85"]]},{"id":"70df6aaf14055b65","type":"complete","z":"a73202046ef1075d","name":"","scope":["5650e9b7e6f87d25"],"uncaught":false,"x":930,"y":2360,"wires":[["73f39c48f18cf5ea"]]},{"id":"73f39c48f18cf5ea","type":"debug","z":"a73202046ef1075d","name":"Complete messages from dyn 2","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":1170,"y":2360,"wires":[]},{"id":"a4ad40ea1646c2a2","type":"inject","z":"a73202046ef1075d","name":"sub single string \"test/test1\"","props":[{"p":"action","v":"subscribe","vt":"str"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"test/test1","x":620,"y":2240,"wires":[["5650e9b7e6f87d25"]]},{"id":"30fac3fb64e22589","type":"inject","z":"a73202046ef1075d","name":"sub single object {test/test2}","props":[{"p":"action","v":"subscribe","vt":"str"},{"p":"topic","v":"{\"topic\":\"test/test2\",\"datatype\":\"json\",\"qos\":0}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":620,"y":2280,"wires":[["5650e9b7e6f87d25"]]},{"id":"91d592021302d915","type":"comment","z":"a73202046ef1075d","name":"Subscribe topics","info":"","x":560,"y":2200,"wires":[]},{"id":"084f2b137449dbdf","type":"debug","z":"a73202046ef1075d","name":"MQTT messages dyn 2","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"topic & \" : \" & payload","statusType":"jsonata","x":1190,"y":2280,"wires":[]},{"id":"93ead290d3373322","type":"comment","z":"a73202046ef1075d","name":"Unsubscribe topics","info":"","x":570,"y":2340,"wires":[]},{"id":"5650e9b7e6f87d25","type":"mqtt in","z":"a73202046ef1075d","name":"Dynamic 2","topic":"","qos":"2","datatype":"auto","broker":"d98ab0d0f2c84665","nl":false,"rap":true,"rh":0,"inputs":1,"x":930,"y":2280,"wires":[["4bf75fc081d8c2ee"]]},{"id":"f59d806b296935e2","type":"inject","z":"a73202046ef1075d","name":"get subscriptions","props":[{"p":"action","v":"getSubscriptions","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":580,"y":2460,"wires":[["5650e9b7e6f87d25"]]},{"id":"aa6213febbe0bde0","type":"inject","z":"a73202046ef1075d","name":"get subscriptions","props":[{"p":"action","v":"getSubscriptions","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":580,"y":1780,"wires":[["22bc0452e33ae532"]]},{"id":"7812e66c6e25e290","type":"complete","z":"a73202046ef1075d","name":"","scope":["22bc0452e33ae532"],"uncaught":false,"x":930,"y":2000,"wires":[["a5ad4471bc9a9c1f"]]},{"id":"a5ad4471bc9a9c1f","type":"debug","z":"a73202046ef1075d","name":"Complete messages from dyn 1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":1170,"y":2000,"wires":[]},{"id":"00f833d0690a3a25","type":"inject","z":"a73202046ef1075d","name":"connect","props":[{"p":"action","v":"connect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1510,"y":1820,"wires":[["0cd070c3e7d76af3"]]},{"id":"2c98ce849c275c3b","type":"inject","z":"a73202046ef1075d","name":"disconnect","props":[{"p":"action","v":"disconnect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":1520,"y":1780,"wires":[["0cd070c3e7d76af3"]]},{"id":"482f5c6d02e57597","type":"inject","z":"a73202046ef1075d","name":"connect with opts V1","props":[{"p":"broker","v":"{\"url\":\"mqtt://broker.hivemq.com:1883\",\"protocolVersion\":3,\"birth\":{\"topic\":\"test/lwt/birth-V1\",\"qos\":0,\"retain\":false,\"payload\":\"birth-V1\",\"properties\":{\"correlationData\":\"hello birth-V1\",\"userProperties\":{\"prop1\":\"birth-V1\"},\"contentType\":\"application/text\"}},\"close\":{\"topic\":\"test/lwt/close-V1\",\"qos\":0,\"retain\":false,\"payload\":\"close-V1\",\"properties\":{\"correlationData\":\"hello close-V1\",\"userProperties\":{\"prop1\":\"close-V1\"},\"contentType\":\"application/text\"}},\"will\":{\"topic\":\"test/lwt/will-V1\",\"qos\":0,\"retain\":false,\"payload\":\"will-V1\",\"properties\":{\"correlationData\":\"hello will-V1\",\"userProperties\":{\"prop1\":\"will-V1\"},\"contentType\":\"application/text\"}}}","vt":"json"},{"p":"action","v":"connect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":1550,"y":1860,"wires":[["0cd070c3e7d76af3"]]},{"id":"9942e57661119659","type":"inject","z":"a73202046ef1075d","name":"connect with opts V2","props":[{"p":"broker","v":"{\"broker\":\"broker.emqx.io\",\"port\":1883,\"cleansession\":false,\"clientid\":\"smacsmac_test_test_gotta_unique\",\"protocolVersion\":5,\"birth\":{\"topic\":\"test/lwt/birth-V2\",\"qos\":0,\"retain\":false,\"payload\":\"birth-V2\",\"properties\":{\"correlationData\":\"hello birth-V2\",\"userProperties\":{\"prop1\":\"birth-V2\"},\"contentType\":\"application/text\"}},\"close\":{\"topic\":\"test/lwt/close-V2\",\"qos\":0,\"retain\":false,\"payload\":\"close-V2\",\"properties\":{\"correlationData\":\"hello close-V2\",\"userProperties\":{\"prop1\":\"close-V2\"},\"contentType\":\"application/text\"}},\"will\":{\"topic\":\"test/lwt/will-V2\",\"qos\":0,\"retain\":false,\"payload\":\"will-V2\",\"properties\":{\"correlationData\":\"hello will-V2\",\"userProperties\":{\"prop1\":\"will-V2\"},\"contentType\":\"application/text\"}}}","vt":"json"},{"p":"action","v":"connect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":1550,"y":1900,"wires":[["0cd070c3e7d76af3"]]},{"id":"6ebd8e1a153e3398","type":"complete","z":"a73202046ef1075d","name":"","scope":["0cd070c3e7d76af3"],"uncaught":false,"x":1910,"y":2120,"wires":[["68c12d86d6a7682e"]]},{"id":"afa8ca7ce723c740","type":"debug","z":"a73202046ef1075d","name":"Complete messages mqtt-out","active":false,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"msg","x":2000,"y":2200,"wires":[]},{"id":"2fa51c0994dfdf8f","type":"comment","z":"a73202046ef1075d","name":"Testing connect / disconnect actions with an mqtt-out","info":"","x":1630,"y":1740,"wires":[]},{"id":"68c12d86d6a7682e","type":"switch","z":"a73202046ef1075d","name":"has msg.action","property":"action","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":1940,"y":2160,"wires":[["afa8ca7ce723c740"]]},{"id":"42c8756ff2db3b9b","type":"inject","z":"a73202046ef1075d","name":"unsub all","props":[{"p":"action","v":"unsubscribe","vt":"str"},{"p":"topic","v":"true","vt":"bool"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":560,"y":2380,"wires":[["5650e9b7e6f87d25"]]},{"id":"fdca8a97b79e0a8c","type":"debug","z":"a73202046ef1075d","name":"action response","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1220,"y":1820,"wires":[]},{"id":"16a5af7930d4bf85","type":"switch","z":"a73202046ef1075d","name":"has msg.action","property":"action","propertyType":"msg","rules":[{"t":"eq","v":"getSubscriptions","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":980,"y":1820,"wires":[["fdca8a97b79e0a8c"],["49664da44719d200"]]},{"id":"49664da44719d200","type":"debug","z":"a73202046ef1075d","name":"MQTT messages dyn 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1190,"y":1880,"wires":[]},{"id":"4bf75fc081d8c2ee","type":"switch","z":"a73202046ef1075d","name":"has msg.action","property":"action","propertyType":"msg","rules":[{"t":"eq","v":"getSubscriptions","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":980,"y":2220,"wires":[["218ba9660b257012"],["084f2b137449dbdf"]]},{"id":"218ba9660b257012","type":"debug","z":"a73202046ef1075d","name":"action response","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1220,"y":2220,"wires":[]},{"id":"b02e334881c59f79","type":"mqtt in","z":"a73202046ef1075d","name":"birth/close/will V1 (broker.hivemq.com)","topic":"test/lwt/#","qos":"2","datatype":"auto","broker":"a56f8945.7b8388","nl":false,"rap":true,"rh":0,"inputs":0,"x":1050,"y":1620,"wires":[["a5477ecc1a6304a4"]]},{"id":"4ac0a5f5d913477d","type":"mqtt in","z":"a73202046ef1075d","name":"birth/close/will V2 (broker.emqx.io)","topic":"test/lwt/#","qos":"2","datatype":"auto","broker":"dc811dae.05f5","nl":false,"rap":true,"rh":0,"inputs":0,"x":1040,"y":1680,"wires":[["da1e21e4b879b8b7"]]},{"id":"da1e21e4b879b8b7","type":"debug","z":"a73202046ef1075d","name":"Check LWT V2","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"topic","statusType":"msg","x":1320,"y":1680,"wires":[]},{"id":"a5477ecc1a6304a4","type":"debug","z":"a73202046ef1075d","name":"Check LWT V1","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"topic","statusType":"msg","x":1320,"y":1620,"wires":[]},{"id":"3ed3df35b7583457","type":"comment","z":"a73202046ef1075d","name":"separate connections to V1 and V2 to monitor LWT","info":"","x":1090,"y":1580,"wires":[]},{"id":"34872679cc42ebe7","type":"inject","z":"a73202046ef1075d","name":"disconnect","props":[{"p":"action","v":"disconnect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":560,"y":1620,"wires":[["22bc0452e33ae532"]]},{"id":"100c8d90dc58a5e8","type":"inject","z":"a73202046ef1075d","name":"connect","props":[{"p":"action","v":"connect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":550,"y":1660,"wires":[["22bc0452e33ae532"]]},{"id":"943b22d0c93ba287","type":"inject","z":"a73202046ef1075d","name":"connect with opts V1","props":[{"p":"broker","v":"{\"url\":\"mqtt://broker.hivemq.com:1883\",\"protocolVersion\":3,\"birth\":{\"topic\":\"test/lwt/birth-V1\",\"qos\":0,\"retain\":false,\"payload\":\"birth-V1\",\"properties\":{\"correlationData\":\"hello birth-V1\",\"userProperties\":{\"prop1\":\"birth-V1\"},\"contentType\":\"application/text\"}},\"close\":{\"topic\":\"test/lwt/close-V1\",\"qos\":0,\"retain\":false,\"payload\":\"close-V1\",\"properties\":{\"correlationData\":\"hello close-V1\",\"userProperties\":{\"prop1\":\"close-V1\"},\"contentType\":\"application/text\"}},\"will\":{\"topic\":\"test/lwt/will-V1\",\"qos\":0,\"retain\":false,\"payload\":\"will-V1\",\"properties\":{\"correlationData\":\"hello will-V1\",\"userProperties\":{\"prop1\":\"will-V1\"},\"contentType\":\"application/text\"}}}","vt":"json"},{"p":"action","v":"connect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"str","x":590,"y":1700,"wires":[["22bc0452e33ae532"]]},{"id":"85061c93d2966f6a","type":"inject","z":"a73202046ef1075d","name":"connect with opts V2","props":[{"p":"broker","v":"{\"broker\":\"broker.emqx.io\",\"port\":1883,\"protocolVersion\":5,\"birth\":{\"topic\":\"test/lwt/birth-V2\",\"qos\":0,\"retain\":false,\"payload\":\"birth-V2\",\"properties\":{\"correlationData\":\"hello birth-V2\",\"userProperties\":{\"prop1\":\"birth-V2\"},\"contentType\":\"application/text\"}},\"close\":{\"topic\":\"test/lwt/close-V2\",\"qos\":0,\"retain\":false,\"payload\":\"close-V2\",\"properties\":{\"correlationData\":\"hello close-V2\",\"userProperties\":{\"prop1\":\"close-V2\"},\"contentType\":\"application/text\"}},\"will\":{\"topic\":\"test/lwt/will-V2\",\"qos\":0,\"retain\":false,\"payload\":\"will-V2\",\"properties\":{\"correlationData\":\"hello will-V2\",\"userProperties\":{\"prop1\":\"will-V2\"},\"contentType\":\"application/text\"}}}","vt":"json"},{"p":"action","v":"connect","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":590,"y":1740,"wires":[["22bc0452e33ae532"]]},{"id":"d98ab0d0f2c84665","type":"mqtt-broker","name":"broker_to_test_mqtt_control","broker":"localhost","port":"1883","clientid":"","autoConnect":false,"usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"a56f8945.7b8388","type":"mqtt-broker","name":"broker.hivemq.com","broker":"broker.hivemq.com","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"4","keepalive":"45","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"dc811dae.05f5","type":"mqtt-broker","name":"","broker":"broker.emqx.io","port":"1883","clientid":"","autoConnect":true,"usetls":false,"compatmode":false,"protocolVersion":"5","keepalive":"50","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""}]
2 Likes

stupid me, didn't add the birth object first. Thank you so much, this dynamic configurations are amazing! :clap: :clap:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.