Node-red: Covert data types from number to content

Hi, I'm trying to change from data types of PLC to content. Currently, I haven't PLC module to practice, I just have the inject node to replace PLC. My problem is changing msg.payload to msg.payload.content to send data, but It's not working to send to Telegram. Do you know how to overcome it?





Hi, in your second image you move msg.payload to msg.payload.content.
Now payload is only the property content.
So your search on msg.payload.topic is searching payload.topic, that wont work as payload.topic is not defined.
In your function you set text to msg.payload.topic, which is undefined , so your if statement is false as text is now undefined. So your function only returns the input msg.

[edit] your inject is also not right as setting payload to 300 will overwrite msg.payload.topic, deleting it.

Actually, When I set the inject node value as msg.payload.content, and direct connection between The inject node and function node, It's working to send data to telegram.



your new image is not the same as the original i commented on.
But does msg.payload.topic exist? using your original inject image, your function relies on msg.payload.topic for the if statement.

I know what you mean.
But, if data input is msg.payload, what should I do first to covert data to send message of Telegram?

Then sort your change node out so when you move msg.payload to msg.payload.content, you do not delete msg.payload.topic. maybe do not put topic in payload, why not use msg.topic. Then when you move payload to content msg.topic is not affected. Then in your function const text = msg.toipc;.

Oh, thanks for your suggest, I coverted msg.payload to msg.contentType.
But, I want to send data to Telegram chatbot, It's not working.



image

Now you have no msg.payload as you are not using it. So in your function you are trying to set msg.payload.type, but msg.payload does not exist.
before your if statement add msg.payload = {}; to initialise payload as an object.

I added it buu, it's not still working

add node.warn(text); before your if statement, what do you see in debug panel. does it equal the string you are comparing in the if statement?
Also show us what is coming out of the change node, add a debug node.

p.s. it best to alter 1 thing at a time when debugging, as every time you change something you may add a new error.

Hi, after I added node.warm(text). It show this


please export your flow, highlight all nodes you wish to export, press ctrl and click on nodes. then press ctrl E and copy to clipboard. Then paste here using the </> button in the post editor, and paste the clipboard between the `triple backticks.

Hi, this is my file

[
    {
        "id": "8894007d182a28a9",
        "type": "tab",
        "label": "Flow 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "b969ef34dae40c14",
        "type": "chatbot-telegram-send",
        "z": "8894007d182a28a9",
        "bot": "fa23d69fac6ed462",
        "botProduction": "",
        "track": true,
        "passThrough": false,
        "errorOutput": false,
        "outputs": 1,
        "x": 570,
        "y": 420,
        "wires": [
            [
                "463c8dca34be2804"
            ]
        ]
    },
    {
        "id": "d8d4528ab432ba8e",
        "type": "inject",
        "z": "8894007d182a28a9",
        "name": "Nhiệt độ",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "Nhiệt độ",
        "payload": "300",
        "payloadType": "num",
        "x": 120,
        "y": 120,
        "wires": [
            [
                "88299cce5dbaa5e3"
            ]
        ]
    },
    {
        "id": "463c8dca34be2804",
        "type": "debug",
        "z": "8894007d182a28a9",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 710,
        "y": 340,
        "wires": []
    },
    {
        "id": "51a22cf5b313e4d6",
        "type": "change",
        "z": "8894007d182a28a9",
        "name": "chuyển đổi",
        "rules": [
            {
                "t": "move",
                "p": "payload",
                "pt": "msg",
                "to": "contentType",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 430,
        "y": 160,
        "wires": [
            [
                "2295ba1a47ad09c8",
                "4270412c8bb8812a"
            ]
        ]
    },
    {
        "id": "2295ba1a47ad09c8",
        "type": "debug",
        "z": "8894007d182a28a9",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "contentType",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 670,
        "y": 140,
        "wires": []
    },
    {
        "id": "4270412c8bb8812a",
        "type": "function",
        "z": "8894007d182a28a9",
        "name": "",
        "func": "//var tam = dem;\nconst data=msg.payload.content;\nvar text = msg.payload.topic;\n if(text == \"Nhiệt độ\"){\n     msg.payload.type = 'message';\n     msg.payload.content = 'Nhiệt độ ổn định\\r\\n'+ data;\n }\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 420,
        "y": 280,
        "wires": [
            [
                "b969ef34dae40c14"
            ]
        ]
    },
    {
        "id": "88299cce5dbaa5e3",
        "type": "switch",
        "z": "8894007d182a28a9",
        "name": "",
        "property": "topic",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "Nhiệt độ",
                "vt": "str"
            },
            {
                "t": "eq",
                "v": "Độ ẩm",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 270,
        "y": 160,
        "wires": [
            [
                "51a22cf5b313e4d6"
            ],
            [
                "51a22cf5b313e4d6"
            ]
        ]
    },
    {
        "id": "7c667d51083abf2e",
        "type": "inject",
        "z": "8894007d182a28a9",
        "name": "Độ ẩm",
        "props": [
            {
                "p": "topic",
                "vt": "str"
            },
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "Độ ẩm",
        "payload": "400",
        "payloadType": "num",
        "x": 110,
        "y": 200,
        "wires": [
            [
                "88299cce5dbaa5e3"
            ]
        ]
    },
    {
        "id": "fa23d69fac6ed462",
        "type": "chatbot-telegram-node",
        "botname": "Hoang Sky",
        "usernames": "",
        "polling": "1000",
        "store": "",
        "log": "",
        "debug": false,
        "skipMediaFiles": false,
        "webHook": "",
        "connectMode": "polling",
        "storeMessages": true,
        "enableMissionControl": false,
        "inspectMessages": true,
        "chatbotId": ""
    }
]

try this i removed unneeded nodes, please add your sender as i do not have it installed

[{"id":"d8d4528ab432ba8e","type":"inject","z":"8894007d182a28a9","name":"Nhiệt độ","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Nhiệt độ","payload":"300","payloadType":"num","x":120,"y":120,"wires":[["4270412c8bb8812a"]]},{"id":"4270412c8bb8812a","type":"function","z":"8894007d182a28a9","name":"","func":"//var tam = dem;\nconst data=msg.payload;\nvar text = msg.topic;\nmsg.payload={\n    \"type\": \"message\", \n    \"chat_id\": 123456\n};\n if(text === \"Nhiệt độ\"){\n    msg.payload.content = 'Nhiệt độ ổn định\\r\\n'+ data;\n }else if(text === \"Độ ẩm\"){\n    msg.payload.content = 'Độ ẩm ổn định\\r\\n'+ data;\n }else{\n     msg = null;\n }\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":200,"wires":[["463c8dca34be2804"]]},{"id":"7c667d51083abf2e","type":"inject","z":"8894007d182a28a9","name":"Độ ẩm","props":[{"p":"topic","vt":"str"},{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"Độ ẩm","payload":"400","payloadType":"num","x":110,"y":200,"wires":[["4270412c8bb8812a"]]},{"id":"463c8dca34be2804","type":"debug","z":"8894007d182a28a9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":710,"y":340,"wires":[]}]

Thanks for your suggest.
But, If i want to send 3 values of PLC. For example, when I typing Temparature, it send value of temperature. Typing speed, it send values of speed, Typing humidity, it send values of humidity. I think it should be definitive, but actually that how should I do?