Extract msg.payload for triggering mytimeout

Hi, I trigger a "mytimeout" node with a Thingboard dashboard and a certain timeout value. The syntax is straight forward and I get it via MQTT exaclty like it has to be.

{
    "timeout": number,
}

The start mqtt message:
timeout

I managed to save the msg.payload in order to trigger the mytimeout node in dependence of other events.

The problem:
Every now and then my NodeRed server needs a reboot, which causes the variables to get lost. Via a status request to Thingsboard I get the shared attributes as a response. But Im simply not able to extract the shared attribute "timeout" and save it in the same JSON syntax as when it comes from the Dashboard itselfe, if that makes sense.

Here the request response.
shared-attributes

And a overview about the schematic:

The first timer runs 100s.
The lower timer uses the command as a trigger, but runs the fixed default value.

Here the lower trigger flow:

[
    {
        "id": "acae7fb96ddba2b7",
        "type": "mytimeout",
        "z": "256fc4e0c15f22be",
        "name": "T_on",
        "outtopic": "",
        "outsafe": "",
        "outwarning": "Warning",
        "outunsafe": "off",
        "warning": "5",
        "timer": "",
        "debug": false,
        "ndebug": false,
        "ignoreCase": false,
        "repeat": false,
        "again": false,
        "x": 1390,
        "y": 500,
        "wires": [
            [],
            []
        ]
    },
    {
        "id": "7383571347fef23c",
        "type": "function",
        "z": "256fc4e0c15f22be",
        "name": "extract attribute timeout",
        "func": "return {payload: msg.payload.shared.timeout};",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 1120,
        "y": 500,
        "wires": [
            [
                "acae7fb96ddba2b7",
                "30e1334bb4796bcb"
            ]
        ]
    },
    {
        "id": "29327df981980ce6",
        "type": "json",
        "z": "256fc4e0c15f22be",
        "name": "",
        "property": "payload",
        "action": "",
        "pretty": false,
        "x": 730,
        "y": 500,
        "wires": [
            [
                "7383571347fef23c",
                "4ee7e054d1afcd15"
            ]
        ]
    }
]

It looks to me as though msg.payload.shared.timeout is the value you want, but that's what your function is using.
Can you show us the value of msg.payload that is passed in to the function and the value that leaves the function?

This is passed into the function:
shared-attributes

This leaves the function:
leaves

This is, what I would need:
timeout

So actually I need to implement an Number in a JSON string... kind of? I guess...

Take a change node:

image

First is: SET
Second is: MOVE

Und wie immer ein toller Tip von einem unbekannten hellen Kopf irgendwo aus der Welt!

Und falls Du es unbedingt mit einer function Node machen möchtest, warst Du ja nahe dran:

[
    {
        "id": "79bb09783c053ade",
        "type": "function",
        "z": "f87aa05e3297dcd1",
        "name": "",
        "func": "return {payload: {timeout: msg.payload.shared.timeout}};\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 680,
        "y": 480,
        "wires": [
            [
                "a5d46e4ef4c2e176"
            ]
        ]
    },
    {
        "id": "6529ad3591da8d08",
        "type": "inject",
        "z": "f87aa05e3297dcd1",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"shared\":{\"timeout\":100}}",
        "payloadType": "json",
        "x": 510,
        "y": 480,
        "wires": [
            [
                "79bb09783c053ade"
            ]
        ]
    },
    {
        "id": "a5d46e4ef4c2e176",
        "type": "debug",
        "z": "f87aa05e3297dcd1",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 850,
        "y": 480,
        "wires": []
    }
]

der Einzeiler:

return {payload: {timeout: msg.payload.shared.timeout}};

Kommt wie gerufen, das hatte ich noch versucht :smile:

jetzt geht es doch weiter...I write once again in English :slight_smile:

I have to multiply the contained number. How do I extract this, send it to the calculator and then reassemble the msg.payload so that timeout: number comes out?

Funny, i searched and tried for hours and 5min after posting, I got it...

[
    {
        "id": "b06d9d0d0c1dc00e",
        "type": "calculator",
        "z": "256fc4e0c15f22be",
        "name": "",
        "inputMsgField": "payload.timeout",
        "outputMsgField": "payload.timeout",
        "operation": "mult",
        "constant": "3",
        "round": false,
        "decimals": 0,
        "x": 980,
        "y": 540,
        "wires": [
            [
                "e85823bf4c01bdc9"
            ]
        ]
    }
]
1 Like

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