MQTT JSON Input with timestamp

I’m using Nodered to parse it. I enclose a couple of Nodes here. Bit of a mess as they currently have all sorts of test code in but I think they will work. if you want to just do things simply you can ignore some of the extra stuff I have in.

I had to set up a couple of flows to set the right timezone and adjust the update speed.

Obviously you need an MQTT node to send it out!

HTH

[edit] sorry first attempt was wrong
[edit2] sorry more haste less speed. let me do it properly…
[edit3] Third time lucky! - added in the ability to send time as a string or as seconds.

[
    {
        "id": "f993c14d.d68aa8",
        "type": "mqtt out",
        "z": "25471496.37cf34",
        "name": "",
        "topic": "",
        "qos": "",
        "retain": "",
        "broker": "a1885387.15bd3",
        "x": 450,
        "y": 660,
        "wires": []
    },
    {
        "id": "5fec2b92.1fb9e4",
        "type": "inject",
        "z": "25471496.37cf34",
        "name": "",
        "topic": "cmnd/SonoffPow1/teleperiod",
        "payload": "30",
        "payloadType": "num",
        "repeat": "",
        "crontab": "",
        "once": false,
        "x": 210,
        "y": 640,
        "wires": [
            [
                "f993c14d.d68aa8"
            ]
        ]
    },
    {
        "id": "9f9dbd1b.220c2",
        "type": "inject",
        "z": "25471496.37cf34",
        "name": "",
        "topic": "cmnd/SonoffPow1/Timezone",
        "payload": "0",
        "payloadType": "num",
        "repeat": "",
        "crontab": "",
        "once": true,
        "x": 210,
        "y": 700,
        "wires": [
            [
                "f993c14d.d68aa8"
            ]
        ]
    },
    {
        "id": "9d56c868.e2f568",
        "type": "debug",
        "z": "25471496.37cf34",
        "name": "",
        "active": true,
        "console": "false",
        "complete": "true",
        "x": 910,
        "y": 560,
        "wires": []
    },
    {
        "id": "71019180.7cf328",
        "type": "mqtt in",
        "z": "25471496.37cf34",
        "name": "SonoffPow1 tele",
        "topic": "tele/SonoffPow1/#",
        "qos": "2",
        "broker": "a1885387.15bd3",
        "x": 140,
        "y": 560,
        "wires": [
            [
                "948e95b6.8be568"
            ]
        ]
    },
    {
        "id": "948e95b6.8be568",
        "type": "function",
        "z": "25471496.37cf34",
        "name": "SonoffPow Message output",
        "func": "var topic = [];\nvar msg0 = null;//{};\nvar msg1 = null;//{};\n\ntry{\n    payload = JSON.parse(msg.payload);\n    topic = msg.topic.split(\"/\").pop();\n    if (topic == \"SENSOR\"){\n        msg1 = {};\n        msg1.topic = topic;\n        msg1.payload = payload;\n    }\n    else {\n        msg0 = {};\n        msg0.topic = topic;\n        msg0.payload = payload;\n    }\n}\n\ncatch(err){\n}\n\nfinally{\n    return [msg0, msg1];\n}\n",
        "outputs": "2",
        "noerr": 0,
        "x": 400,
        "y": 560,
        "wires": [
            [],
            [
                "119a72d9.2fc995"
            ]
        ],
        "outputLabels": [
            "Other",
            "Sensor"
        ]
    },
    {
        "id": "119a72d9.2fc995",
        "type": "function",
        "z": "25471496.37cf34",
        "name": "Test for Emoncms Node",
        "func": "var newmsg ={};\nvar newDateObj = new Date(msg.payload.Time);\n\nnewmsg.payload = msg.payload.ENERGY;\n//newmsg.payload.time = msg.payload.Time; // as string\nnewmsg.payload.time = newDateObj.getTime()/1000; // Seconds since Epoch\n\nreturn newmsg;",
        "outputs": 1,
        "noerr": 0,
        "x": 710,
        "y": 560,
        "wires": [
            [
                "9d56c868.e2f568"
            ]
        ]
    },
    {
        "id": "a1885387.15bd3",
        "type": "mqtt-broker",
        "z": "",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": ""
    }
]
1 Like