Reading retained mqtt messages for use in function

In my use case I need to send two attributes to a receiver (a high and a low value). I get both values in two seperate (retained) MQTT messages in two topics. If either attribute changes, I want to send both to the receiver, which expects both values at a time.

Can I read retained MQTT messages/values for a combined use case, so I know the other value, if I'm sending the command to the receiver?

current flow
[
    {
        "id": "b2f37f21.9ab948",
        "type": "mqtt in",
        "z": "a351eaa.aed7b98",
        "name": "cmdSetChargingLimitAC",
        "topic": "openHAB/master/Kia/cmdSetChargingLimitAC",
        "qos": "2",
        "datatype": "auto",
        "broker": "4672ca10.309984",
        "x": 120,
        "y": 1040,
        "wires": [
            [
                "aec97b9c.d2374"
            ]
        ]
    },
    {
        "id": "c2674666.5d09",
        "type": "mqtt in",
        "z": "a351eaa.aed7b98",
        "name": "cmdSetChargingLimitDC",
        "topic": "openHAB/master/Kia/cmdSetChargingLimitDC",
        "qos": "2",
        "datatype": "auto",
        "broker": "4672ca10.309984",
        "x": 120,
        "y": 1080,
        "wires": [
            [
                "aec97b9c.d2374"
            ]
        ]
    },
    {
        "id": "aec97b9c.d2374",
        "type": "function",
        "z": "a351eaa.aed7b98",
        "name": "",
        "func": "myLimit = {};\nmyLimit.fast = 80;\nmyLimit.slow = 80;\n\nif (msg.topic == \"openHAB/master/Kia/cmdSetChargingLimitAC\") {\n    myLimit.slow = msg.payload;\n}\nif (msg.topic == \"openHAB/master/Kia/cmdSetChargingLimitDC\") {\n    myLimit.fast = msg.payload;\n}\n\nmsg.payload = myLimit;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "x": 320,
        "y": 1060,
        "wires": [
            [
                "f498274c.5833e",
                "1f2b80e4.92a73f"
            ]
        ]
    },
    {
        "id": "4672ca10.309984",
        "type": "mqtt-broker",
        "name": "Synology",
        "broker": "192.168.xx.yy",
        "port": "1883",
        "clientid": "nodered",
        "usetls": false,
        "compatmode": false,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "openHAB/master/nodered",
        "birthQos": "2",
        "birthRetain": "true",
        "birthPayload": "ON",
        "closeTopic": "openHAB/master/nodered",
        "closeQos": "2",
        "closeRetain": "true",
        "closePayload": "OFF",
        "willTopic": "openHAB/master/nodered",
        "willQos": "2",
        "willRetain": "true",
        "willPayload": "OFF"
    }
]

You can store/read values in global/flow/node contexts. See documentation.

Right, didn't think about that - (but this would only work, if the flow already got the other value, so after restarting the flow, I'd have to "guess" or intiatilize the other value... But at least it'll work afterwards...)

...no, scrub that: if it's retained, restarting the flow will bring the message in the flow regardless. Top!

This sounds like an ideal candidate for the Join node. See this article in the cookbook for an example of how to join messages into one object. If you set it to wait for two messages and then send on every subsequent message, using key/value mode, it should give you exactly what you want. Import the example from the cookbook and have a play with it to see how it works.

1 Like

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