@_mirjka posted a new topic, subject "Extract msg.payload for triggering mytimeout". It read:

   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.
    [code] 
        {
            "timeout": number,
        }
        
    [/code]
    
    The start mqtt message:  
    
    
    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.  
    
    
    And a overview about the schematic:  
    
    
    overview1131×454 25.2 KB
    
    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:
    [code] 
        [
            {
                "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"
                    ]
                ]
            }
        ]
        
    [/code]

Then @jbudd said

    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?

So @_mirjka replied

    This is passed into the function:  
    
    
    This leaves the function:  
    
    
    This is, what I would need:  
    
    
    So actually I need to implement an Number in a JSON string... kind of? I guess...

So @mickym2 (mickym) replied

    Take a change node:
    
    First is: SET  
    Second is: MOVE

To which @_mirjka replied

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

To which @mickym2 (mickym) replied

    _mirjka:
    
    > `"return {payload: msg.payload.shared.timeout};`
    
    Und falls Du es unbedingt mit einer function Node machen möchtest, warst Du ja nahe dran:
    
    image1359×160 9.31 KB
    [code] 
        [
            {
                "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": []
            }
        ]
        
    [/code]
    
    der Einzeiler:
    [code] 
        return {payload: {timeout: msg.payload.shared.timeout}};
        
    [/code]

To which @_mirjka replied

    Kommt wie gerufen, das hatte ich noch versucht

So @_mirjka replied

    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?

So @_mirjka replied

    Funny, i searched and tried for hours and 5min after posting, I got it...
    [code] 
        [
            {
                "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"
                    ]
                ]
            }
        ]
        
    [/code]

Then @system (system) said

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


{'source': 'discourse', 'url': 'https://discourse.nodered.org/t/extract-msg-payload-for-triggering-mytimeout/60135', 'title': 'Extract msg.payload for triggering mytimeout', 'views': 207, 'has_accepted_answer': False, 'word_count': 612, 'like_count': 1, 'reply_count': 6, 'participant_count': 3, 'posts': 10, 'tags': '', 'author': '@_mirjka', 'participants': '@_mirjka, @jbudd, @mickym2 (mickym), @system (system)', 'image_url': ['https://global.discourse-cdn.com/business6/uploads/nodered/original/3X/8/f/8fa7668ef62aeb4994cb33c4b91a749ec9ef0087.png'], 'created_at': '2022-03-21T10:25:45.319Z', 'last_posted_at': '2022-05-21T19:30:21.220Z'}