# Extract msg.payload for triggering mytimeout

**URL:** <https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135>\
**Category:** General\
**Created:** [21 March 2022 10:25 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135 "2022-03-21T10:25:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_mirjka](https://avatars.discourse-cdn.com/v4/letter/_/f19dbf/32.png) [@\_mirjka](https://discourse.nodered.org/u/_mirjka)\
**Post date:** [21 March 2022 10:25 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/1 "2022-03-21T10:25:45Z")

</div>

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.

```auto
{
    "timeout": number,
}

```

The start mqtt message:  
 ![timeout](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8fa7668ef62aeb4994cb33c4b91a749ec9ef0087.png)

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b9c5c0bf5543e6c59940487c5f0b1e1518acaef.png)

And a overview about the schematic:

 ![overview](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/9/69f7996c0c0a7e28ecf3f2ed85e312468a688395.png)

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:

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [21 March 2022 11:47 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/2 "2022-03-21T11:47:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![\_mirjka](https://avatars.discourse-cdn.com/v4/letter/_/f19dbf/32.png) [@\_mirjka](https://discourse.nodered.org/u/_mirjka)\
**Post date:** [21 March 2022 13:31 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/3 "2022-03-21T13:31:35Z")

</div>

This is passed into the function:  
 ![shared-attributes](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/b/8b9c5c0bf5543e6c59940487c5f0b1e1518acaef.png)

This leaves the function:  
 ![leaves](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/4/f4ff21982c61b1464960bc827edb6b4ead9e1baf.png)

This is, what I would need:  
 ![timeout](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/f/8fa7668ef62aeb4994cb33c4b91a749ec9ef0087.png)

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

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [21 March 2022 13:36 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/4 "2022-03-21T13:36:16Z")

</div>

Take a change node:

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da9cd1c8122235c59932b5d198d2e0279c35710d.png)

First is: SET  
Second is: MOVE

---

<div class="post-metadata">

**Author:** ![\_mirjka](https://avatars.discourse-cdn.com/v4/letter/_/f19dbf/32.png) [@\_mirjka](https://discourse.nodered.org/u/_mirjka)\
**Post date:** [21 March 2022 13:48 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/5 "2022-03-21T13:48:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [21 March 2022 14:02 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/6 "2022-03-21T14:02:23Z")

</div>

> [@\_mirjka](#):
>
> `"return {payload: msg.payload.shared.timeout};`

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/5/05578be772e4c457899f22df7bcfada7fa50503e.png)

```auto
[
    {
        "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:

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

```

---

<div class="post-metadata">

**Author:** ![\_mirjka](https://avatars.discourse-cdn.com/v4/letter/_/f19dbf/32.png) [@\_mirjka](https://discourse.nodered.org/u/_mirjka)\
**Post date:** [21 March 2022 15:09 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/7 "2022-03-21T15:09:24Z")

</div>

Kommt wie gerufen, das hatte ich noch versucht 😄

---

<div class="post-metadata">

**Author:** ![\_mirjka](https://avatars.discourse-cdn.com/v4/letter/_/f19dbf/32.png) [@\_mirjka](https://discourse.nodered.org/u/_mirjka)\
**Post date:** [22 March 2022 19:26 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/8 "2022-03-22T19:26:48Z")

</div>

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

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?

---

<div class="post-metadata">

**Author:** ![\_mirjka](https://avatars.discourse-cdn.com/v4/letter/_/f19dbf/32.png) [@\_mirjka](https://discourse.nodered.org/u/_mirjka)\
**Post date:** [22 March 2022 19:29 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/9 "2022-03-22T19:29:37Z")

</div>

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

```auto
[
    {
        "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"
            ]
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [21 May 2022 19:30 UTC](https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135/10 "2022-05-21T19:30:21Z")

</div>

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