Let's consider the following:
[{"id":"f484ccd9.7480e","type":"mqtt in","z":"ced90757.b4e1e8","name":"","topic":"vDevice/State","qos":"2","datatype":"auto","broker":"d0a0bfc5.b4176","x":110,"y":220,"wires":[["7d4cd0fc.00e96"]]},{"id":"c14e8beb.ee34e8","type":"mqtt in","z":"ced90757.b4e1e8","name":"","topic":"vDevice/Telemetrics","qos":"2","datatype":"auto","broker":"d0a0bfc5.b4176","x":130,"y":280,"wires":[["3c727614.33e56a"]]},{"id":"3c727614.33e56a","type":"switch","z":"ced90757.b4e1e8","name":"","property":"retain","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":280,"wires":[["b19f805b.83a39"],["3c81bd84.3dfc12"]]},{"id":"3c81bd84.3dfc12","type":"change","z":"ced90757.b4e1e8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"(\" & msg.payload & \")\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":300,"wires":[["b19f805b.83a39"]]},{"id":"7d4cd0fc.00e96","type":"switch","z":"ced90757.b4e1e8","name":"","property":"retain","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":220,"wires":[["753bff47.3299f"],["526cdae4.842964"]]},{"id":"526cdae4.842964","type":"change","z":"ced90757.b4e1e8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"(\" & msg.payload & \")\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":240,"wires":[["753bff47.3299f"]]},{"id":"b24f40e8.e7898","type":"mqtt in","z":"ced90757.b4e1e8","name":"","topic":"vDevice/NetworkState","qos":"2","datatype":"auto","broker":"d0a0bfc5.b4176","x":140,"y":120,"wires":[["92b368e5.8e5238"]]},{"id":"92b368e5.8e5238","type":"switch","z":"ced90757.b4e1e8","name":"","property":"retain","propertyType":"msg","rules":[{"t":"false"},{"t":"true"}],"checkall":"true","repair":false,"outputs":2,"x":330,"y":120,"wires":[["e93e9089.cd42c"],["3f56ee40.1655e2"]]},{"id":"3f56ee40.1655e2","type":"change","z":"ced90757.b4e1e8","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"(\" & msg.payload & \")\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":520,"y":140,"wires":[["e93e9089.cd42c"]]},{"id":"e93e9089.cd42c","type":"debug","z":"ced90757.b4e1e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":120,"wires":[]},{"id":"753bff47.3299f","type":"debug","z":"ced90757.b4e1e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":220,"wires":[]},{"id":"b19f805b.83a39","type":"debug","z":"ced90757.b4e1e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":730,"y":280,"wires":[]},{"id":"d0a0bfc5.b4176","type":"mqtt-broker","z":"","name":"localhost MQTT","broker":"localhosta","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Each flow reads a certain value from MQTT and passes it forward either:
- intact,
- or if the message had
msg.retain
set, it marks the payload as last-known-state (in the example: by enclosing it in parentheses).
Now, I would like to add logic that: if the device goes offline (the value of vDevice/NetworkState
changes to offline
), then the remaining values are "degraded" to last-known-state.
If I could force the MQTT-in nodes to re-subscribe, then they would get the values from MQTT with msg.retain
flag set (automatically, by design) and pass the message according to existing logic.
Is it doable?
Or should I store the value of vDevice/NetworkState
in context and modify each flow to consider messages matching (retained: true
or flow.networkstate: offline
) as last-known-state?