Help to read JSON msg via Node-RED

I have a device that can POST data in the following format:

{"esp8266id": "13956060", "software_version": "BETA-1.0.9.3", "sensordatavalues":[{"value_type":"SDS_P1","value":"2.09"},{"value_type":"SDS_P2","value":"1.11"},{"value_type":"BME280_temperature","value":"36.30"},{"value_type":"BME280_humidity","value":"23.98"},{"value_type":"BME280_pressure","value":"99872.79"},{"value_type":"NH3","value":"0.026864"},{"value_type":"CO","value":"0.295"},{"value_type":"NO2","value":"0.021016"},{"value_type":"NH3-raw","value":"403.68"},{"value_type":"CO-raw","value":"305.88"},{"value_type":"NO2-raw","value":"20.95"},{"value_type":"MICS-calibration-data","value":"5625 3034 14384"},{"value_type":"Radiation","value":"7"},{"value_type":"samples","value":"288427"},{"value_type":"min_micro","value":"117"},{"value_type":"max_micro","value":"2303908"},{"value_type":"signal","value":"-83"},{"value_type":"quality","value":"34"}]}

I need to read POST message via HA (Node-RED can do it) and then display measured values in HA interface as sensors.
How can I create Node-RED flow properly? I tried HTTP-in, but what then? Nothing is working :frowning:

Can you post the flow you have. Are you telling the mqtt-in node that the output should be parsed JSON (it’s one of the options)? What problem are you seeing?

@michaelblight
Here is my node. My device sends HTTP POST msg in the format above, I used HTTP-in. Maybe node construction is wrong. Please correct. Thank you!


image

Messages are readable in free HTTP online server, but I don’t understand how to read it in HA.
Thank you!

Actually I gave you a bum steer - you mentioned HTTP-in but I answered as if you’re using HTTP-request. HTTP-in is used to respond to an inbound request to your Node Red server (and needs to be paired with an HTTP-response). If you want to POST from Node Red and read the response, you need to use HTTP-request.

I actually POST to Node-RED a message in the following format:

{
    "esp8266id": "13956060",
    "software_version": "BETA-1.0.9.3",
    "sensordatavalues": [
        {
            "value_type": "SDS_P1",
            "value": "3.27"
        },
        {
            "value_type": "SDS_P2",
            "value": "0.93"
        },
        {
            "value_type": "BME280_temperature",
            "value": "17.08"
        },
        {
            "value_type": "BME280_humidity",
            "value": "46.43"
        },
        {
            "value_type": "BME280_pressure",
            "value": "99248.06"
        },
        {
            "value_type": "NH3",
            "value": "0.012802"
        },
        {
            "value_type": "CO",
            "value": "0.234"
        },
        {
            "value_type": "NO2",
            "value": "0.035237"
        },
        {
            "value_type": "NH3-raw",
            "value": "605.22"
        },
        {
            "value_type": "CO-raw",
            "value": "363.54"
        },
        {
            "value_type": "NO2-raw",
            "value": "34.69"
        },
        {
            "value_type": "MICS-calibration-data",
            "value": "5625 3034 14384"
        },
        {
            "value_type": "Radiation",
            "value": "6"
        },
        {
            "value_type": "samples",
            "value": "280037"
        },
        {
            "value_type": "min_micro",
            "value": "131"
        },
        {
            "value_type": "max_micro",
            "value": "1961105"
        },
        {
            "value_type": "signal",
            "value": "-84"
        },
        {
            "value_type": "quality",
            "value": "32"
        }
    ]
}

And I transfer it via Node RED in MQTT Server based on HA.
MQTT server receives a message in above mentioned format, and in order to pass figures to corresponding Sensors I need to parse that kind of message.
My question is - how to parse this message in Node RED and create multiple Sensors in HA via Node RED? Or, maybe it’s going to be easier to parse MQTT message like in above via templates through HA? I have no idea how to solve both, I need an advice from community.

Thank you!

Ah, in that case you don’t need NR to push it to HA. HA can get it using the MQTT sensor. However, the current format might fit too well with how the sensor works. It could be handled more simply if you reformat the message to look like:

{
    "esp8266id": "13956060",
    "software_version": "BETA-1.0.9.3",
    "SDS_P1": 3.27,
    "SDS_P2": 0.93,
    (etc)
}

Then HA will read the value of “SDS_P1” (etc) as an attribute on the MQTT sensor. Note also that the values should be formatted as numbers (no quotes).

The format change could either be done in the ESP8266 (assuming it’s your code) or in NR using MQTT-in and MQTT-out nodes with a template node between. In the latter case, HA would be connected to the topic from the MQTT-out.

If you’re using the NR custom component.

image

[{"id":"5e66f0fc.b10eb","type":"inject","z":"ffbd7f06.4a014","name":"data received","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{     \"esp8266id\": \"13956060\",     \"software_version\": \"BETA-1.0.9.3\",     \"sensordatavalues\": [         {             \"value_type\": \"SDS_P1\",             \"value\": \"3.27\"         },         {             \"value_type\": \"SDS_P2\",             \"value\": \"0.93\"         },         {             \"value_type\": \"BME280_temperature\",             \"value\": \"17.08\"         },         {             \"value_type\": \"BME280_humidity\",             \"value\": \"46.43\"         },         {             \"value_type\": \"BME280_pressure\",             \"value\": \"99248.06\"         },         {             \"value_type\": \"NH3\",             \"value\": \"0.012802\"         },         {             \"value_type\": \"CO\",             \"value\": \"0.234\"         },         {             \"value_type\": \"NO2\",             \"value\": \"0.035237\"         },         {             \"value_type\": \"NH3-raw\",             \"value\": \"605.22\"         },         {             \"value_type\": \"CO-raw\",             \"value\": \"363.54\"         },         {             \"value_type\": \"NO2-raw\",             \"value\": \"34.69\"         },         {             \"value_type\": \"MICS-calibration-data\",             \"value\": \"5625 3034 14384\"         },         {             \"value_type\": \"Radiation\",             \"value\": \"6\"         },         {             \"value_type\": \"samples\",             \"value\": \"280037\"         },         {             \"value_type\": \"min_micro\",             \"value\": \"131\"         },         {             \"value_type\": \"max_micro\",             \"value\": \"1961105\"         },         {             \"value_type\": \"signal\",             \"value\": \"-84\"         },         {             \"value_type\": \"quality\",             \"value\": \"32\"         }     ] }","payloadType":"json","x":230,"y":1120,"wires":[["ec9c143f.f28da8"]]},{"id":"f30a4628.379848","type":"split","z":"ffbd7f06.4a014","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":383,"y":1120,"wires":[["e3e156ae.5bf828"]],"l":false},{"id":"ec9c143f.f28da8","type":"change","z":"ffbd7f06.4a014","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.sensordatavalues","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":335,"y":1120,"wires":[["f30a4628.379848"]],"l":false},{"id":"e3e156ae.5bf828","type":"switch","z":"ffbd7f06.4a014","name":"","property":"payload.value_type","propertyType":"msg","rules":[{"t":"eq","v":"SDS_P1","vt":"str"},{"t":"eq","v":"SDS_P2","vt":"str"},{"t":"eq","v":"BME280_temperature","vt":"str"},{"t":"eq","v":"BME280_humidity","vt":"str"},{"t":"eq","v":"BME280_pressure","vt":"str"}],"checkall":"true","repair":false,"outputs":5,"x":466,"y":1120,"wires":[["ffb44edc.3b676"],["fd8ad245.7cc9f"],["f7d1fea9.3c26a"],["83653f12.f8c89"],["7e786e7.9f12b9"]]},{"id":"ffb44edc.3b676","type":"ha-entity","z":"ffbd7f06.4a014","name":"SDS_P1","server":"2dad33ee.42bf5c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"SDS_P1"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload.value","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":652,"y":1024,"wires":[[]]},{"id":"fd8ad245.7cc9f","type":"ha-entity","z":"ffbd7f06.4a014","name":"SDS_P2","server":"2dad33ee.42bf5c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"SDS_P2"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload.value","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":652,"y":1072,"wires":[[]]},{"id":"f7d1fea9.3c26a","type":"ha-entity","z":"ffbd7f06.4a014","name":"BME280_temperature","server":"2dad33ee.42bf5c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"BME280_temperature"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload.value","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":692,"y":1120,"wires":[[]]},{"id":"83653f12.f8c89","type":"ha-entity","z":"ffbd7f06.4a014","name":"BME280_humidity","server":"2dad33ee.42bf5c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"BME280_humidity"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload.value","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":682,"y":1168,"wires":[[]]},{"id":"7e786e7.9f12b9","type":"ha-entity","z":"ffbd7f06.4a014","name":"BME280_pressure","server":"2dad33ee.42bf5c","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"BME280_pressure"},{"property":"device_class","value":""},{"property":"icon","value":""},{"property":"unit_of_measurement","value":""}],"state":"payload.value","stateType":"msg","attributes":[],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":682,"y":1216,"wires":[[]]}]
1 Like

Thank you a lot! This is a working solution!

One more problem - how can I output graph-type (not bar-type) visualization in case of Node-RED sensor?
I assigned ‘unit_of_measurement’ to each value, but still there is no graph.