Filter to get only some data from msg.payload: string

Hello, I have this data via mqtt and I want to take it to an influxdb to graph it in a grafana, the values ​​that arrive in the graph are this whole chain, and I just need to get temperature and humidity data in number:

{"Time":"2020-06-11T13:36:11","SI7021":{"Temperature":15.9,"Humidity":74.5,"DewPoint":11.4},"TempUnit":"C"}

I am a novice in node-red I would appreciate that you explain with what specific nodes I could make these filters.

Have a read in the node red docs at the section Working with Messages. That should help a lot.
[Edit] also, if the example you posted is a string then set the MQTT node to output Parsed JSON. It will convert the JSON string to a javascript object which will make it much easier.

1 Like

The data is a JSON Object and you can extract the temperature and humidity values using a change node. See the example flow. It is possible that the message from the MQTT node is a JSON string and you will need to add a JSON node to convert the output of the MQTT node to a JSON Object

[
    {
        "id": "6ff4951.acf306c",
        "type": "tab",
        "label": "Flow 7",
        "disabled": false,
        "info": ""
    },
    {
        "id": "fc5901ca.92592",
        "type": "inject",
        "z": "6ff4951.acf306c",
        "name": "",
        "topic": "",
        "payload": "{\"Time\":\"2020-06-11T13:36:11\",\"SI7021\":{\"Temperature\":15.9,\"Humidity\":74.5,\"DewPoint\":11.4},\"TempUnit\":\"C\"}",
        "payloadType": "json",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 410,
        "y": 140,
        "wires": [
            [
                "b0ba994e.3fe428",
                "1d4f00f9.b6b25f",
                "d316e8b9.a21aa8"
            ]
        ]
    },
    {
        "id": "b0ba994e.3fe428",
        "type": "debug",
        "z": "6ff4951.acf306c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 740,
        "y": 160,
        "wires": []
    },
    {
        "id": "1d4f00f9.b6b25f",
        "type": "change",
        "z": "6ff4951.acf306c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.SI7021.Temperature",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 660,
        "y": 320,
        "wires": [
            [
                "f7bfa24c.9fdeb"
            ]
        ]
    },
    {
        "id": "f7bfa24c.9fdeb",
        "type": "debug",
        "z": "6ff4951.acf306c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 960,
        "y": 340,
        "wires": []
    },
    {
        "id": "d316e8b9.a21aa8",
        "type": "change",
        "z": "6ff4951.acf306c",
        "name": "",
        "rules": [
            {
                "t": "set",
                "p": "payload",
                "pt": "msg",
                "to": "payload.SI7021.Humidity",
                "tot": "msg"
            }
        ],
        "action": "",
        "property": "",
        "from": "",
        "to": "",
        "reg": false,
        "x": 680,
        "y": 400,
        "wires": [
            [
                "2bf5fc11.4dba04"
            ]
        ]
    },
    {
        "id": "2bf5fc11.4dba04",
        "type": "debug",
        "z": "6ff4951.acf306c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 920,
        "y": 400,
        "wires": []
    }
]

The MQTT node has this capability built in, just tell it to parse the JSON string and convert it to a javascript object (note, a javascript object, it is not correct to call it a JSON object, JSON is always a string).

1 Like

Thanks Colin, always learning! :blush:

I passed it to json I have it as an object and I could remove some data with the change node, I think I have to improve the filter so as not to display and have the pure data I need such as temperature and humidity

Thank you all very much, it worked that way

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