Page 1 of 2

Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Sunday 15 March 2020 21:44
by xury
I've done a Node red flow to get Coronavirus statistic from https://www.worldometers.info/coronavirus/ and push them to custom dummy sensor in Domoticz.
If you want get statistics for your country just type it in "select country" switch node.
Moreover it need to change idx'es in function nodes as Cases ,Deaths etc. and you have to create your own custom sensors in Domoticz too.
covidv2.png
covidv2.png (36.31 KiB) Viewed 2698 times

Code: Select all

[
    {
        "id": "b4ab87d5.800568",
        "type": "mqtt out",
        "z": "ebda401.e7556c",
        "name": "",
        "topic": "domoticz/in",
        "qos": "0",
        "retain": "false",
        "broker": "9748a36f.44bdd",
        "x": 1230,
        "y": 500,
        "wires": []
    },
    {
        "id": "362cf5cf.d65ada",
        "type": "debug",
        "z": "ebda401.e7556c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 990,
        "y": 380,
        "wires": []
    },
    {
        "id": "685e4122.5f4f9",
        "type": "debug",
        "z": "ebda401.e7556c",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "x": 990,
        "y": 660,
        "wires": []
    },
    {
        "id": "4e6e64f8.d3a23c",
        "type": "http request",
        "z": "ebda401.e7556c",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "paytoqs": false,
        "url": "https://www.worldometers.info/coronavirus/",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 290,
        "y": 480,
        "wires": [
            [
                "782c7966.acea78"
            ]
        ]
    },
    {
        "id": "782c7966.acea78",
        "type": "html",
        "z": "ebda401.e7556c",
        "name": "Table select",
        "property": "payload",
        "outproperty": "payload",
        "tag": "tr",
        "ret": "text",
        "as": "multi",
        "x": 450,
        "y": 480,
        "wires": [
            [
                "54b1003a.e4e13"
            ]
        ]
    },
    {
        "id": "cc6dcd38.ffa83",
        "type": "inject",
        "z": "ebda401.e7556c",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "3600",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 130,
        "y": 480,
        "wires": [
            [
                "4e6e64f8.d3a23c"
            ]
        ]
    },
    {
        "id": "54b1003a.e4e13",
        "type": "switch",
        "z": "ebda401.e7556c",
        "name": "Select country",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "Poland",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "Italy",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "Total:",
                "vt": "str"
            }
        ],
        "checkall": "false",
        "repair": false,
        "outputs": 3,
        "x": 620,
        "y": 480,
        "wires": [
            [
                "48d960e2.51c88"
            ],
            [
                "58431ef1.e4577"
            ],
            [
                "50a91f56.b04fa"
            ]
        ]
    },
    {
        "id": "48d960e2.51c88",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "parse",
        "func": "array = msg.payload.split(\"  \");\nfor (var i = 0; i < array.length; i++) {\n     array[i] = array[i].trim()\n }\nmsg.topic = array[1];  \nmsg.payload = {};\nmsg.payload.confirmed = array[2].replace(\",\", \"\");\nmsg.payload.newcases = array[3].replace(\"+\", \"\").replace(\",\", \"\");\nmsg.payload.deaths = array[4].replace(\",\", \"\");\nmsg.payload.newdeaths = array[5].replace(\"+\", \"\").replace(\",\", \"\");\nmsg.payload.recovered = array[6].replace(\",\", \"\");\nmsg.payload.active = array[8].replace(\",\", \"\");\nmsg.payload.serious = array[9].replace(\",\", \"\");\n//msg.payload.array = array;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 460,
        "wires": [
            [
                "c6560917.528468",
                "30e38032.19474",
                "f323260b.3a85c8",
                "362cf5cf.d65ada",
                "fc353f4b.bd07c",
                "2cebd7ec.6325b8"
            ]
        ]
    },
    {
        "id": "c6560917.528468",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Cases",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 196, \"nvalue\": 0, \"svalue\" :msg.payload.confirmed};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 970,
        "y": 440,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "30e38032.19474",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Deaths",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 197, \"nvalue\": 0, \"svalue\" :msg.payload.deaths};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 980,
        "y": 480,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "f323260b.3a85c8",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Recovered",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 198, \"nvalue\": 0, \"svalue\" :msg.payload.recovered};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 990,
        "y": 520,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "50a91f56.b04fa",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Total",
        "func": "array = msg.payload.split(\" \");\nfor (var i = 0; i < array.length; i++) {\n     array[i] = array[i].trim()\n }\nmsg.topic = array[1]; \nmsg.payload = {};\nmsg.payload.total = array[2].replace(\",\", \"\");\nmsg.payload.totalnewcases = array[4].replace(\",\", \"\");\nmsg.payload.totaldeaths = array[5].replace(\",\", \"\");\nmsg.payload.totalnewdeaths = array[6].replace(\",\", \"\");\nmsg.payload.totalrecovered = array[7].replace(\",\", \"\");\nmsg.payload.totalactivecases = array[9].replace(\",\", \"\");\nmsg.payload.totalcritical = array[10].replace(\",\", \"\");\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 540,
        "wires": [
            [
                "685e4122.5f4f9"
            ]
        ]
    },
    {
        "id": "fc353f4b.bd07c",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Active cases",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 199, \"nvalue\": 0, \"svalue\" :msg.payload.active};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 990,
        "y": 560,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "2cebd7ec.6325b8",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Serious Critical",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 200, \"nvalue\": 0, \"svalue\" :msg.payload.serious};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1000,
        "y": 600,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "58431ef1.e4577",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "parse",
        "func": "array = msg.payload.split(\"  \");\nfor (var i = 0; i < array.length; i++) {\n     array[i] = array[i].trim()\n }\nmsg.topic = array[1]; \nmsg.payload = {};\nmsg.payload.confirmed = array[2].replace(\",\", \"\");\nmsg.payload.newcases = array[3].replace(\"+\", \"\").replace(\",\", \"\");\nmsg.payload.deaths = array[4].replace(\",\", \"\");\nmsg.payload.newdeaths = array[5].replace(\"+\", \"\").replace(\",\", \"\");\nmsg.payload.recovered = array[6].replace(\",\", \"\");\nmsg.payload.active = array[8].replace(\",\", \"\");\nmsg.payload.serious = array[9].replace(\",\", \"\");\n//msg.payload.array = array;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 790,
        "y": 500,
        "wires": [
            [
                "362cf5cf.d65ada"
            ]
        ]
    },
    {
        "id": "9748a36f.44bdd",
        "type": "mqtt-broker",
        "z": "",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Tuesday 24 March 2020 15:16
by gzdzacz81
Hi
Unfortunately, it receives empty values. As if there was a problem with the parse function?

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Tuesday 24 March 2020 16:19
by LouiS22
+1, created custom sensors (i suppose it should be counter), but everything stays 0.

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Tuesday 24 March 2020 17:30
by alanlsmith
You need to look through the msg.payload.array by sending it to the debugging panel and then match the elements with the figures shown in https://www.worldometers.info/coronavirus/ for a given country. the array elements appear to be different to those that the OP has used.

Below is the code from the parsing node that I have used to retrieve data for the United Kingdom.
The 'commented out' last element is the array that I mentioned earllier.

Code: Select all

array = msg.payload.split("  ");
for (var i = 0; i < array.length; i++) {
     array[i] = array[i].trim()
 }
//msg.topic = array[1];  
msg.payload = {};
msg.payload.country = array[16].replace(",", "");
msg.payload.confirmed = array[32].replace(",", "");
msg.payload.newcases = array[64].replace("+", "").replace(",", "");
msg.payload.deaths = array[80].replace(",", "");
msg.payload.newdeaths = array[128].replace("+", "").replace(",", "");
msg.payload.recovered = array[144].replace(",", "");
msg.payload.active = array[192].replace(",", "");
msg.payload.serious = array[208].replace(",", "");
//msg.payload.array = array;

return msg;

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Tuesday 24 March 2020 18:19
by gzdzacz81
ok, I understand now, but after the changes the function parse returns: "function : (error)
"TypeError: Cannot read property 'replace' of undefined""

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Tuesday 24 March 2020 20:00
by alanlsmith
gzdzacz81 wrote: Tuesday 24 March 2020 18:19 ok, I understand now, but after the changes the function parse returns: "function : (error)
"TypeError: Cannot read property 'replace' of undefined""
Check the array index numbers used in the 'replace' function. It sounds like one (or more) might be out of range. ie: not in the array. As the arrays do appear to be of different lengths, although I have not properly checked this.

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Tuesday 24 March 2020 20:35
by xury
Website where is scraped data was modified. This is a updated flow:

Code: Select all

[
    {
        "id": "b4ab87d5.800568",
        "type": "mqtt out",
        "z": "ebda401.e7556c",
        "name": "",
        "topic": "domoticz/in",
        "qos": "2",
        "retain": "false",
        "broker": "9748a36f.44bdd",
        "x": 1350,
        "y": 500,
        "wires": []
    },
    {
        "id": "4e6e64f8.d3a23c",
        "type": "http request",
        "z": "ebda401.e7556c",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "paytoqs": false,
        "url": "https://www.worldometers.info/coronavirus/",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "x": 270,
        "y": 480,
        "wires": [
            [
                "782c7966.acea78"
            ]
        ]
    },
    {
        "id": "782c7966.acea78",
        "type": "html",
        "z": "ebda401.e7556c",
        "name": "Table select",
        "property": "payload",
        "outproperty": "payload",
        "tag": "tr",
        "ret": "text",
        "as": "multi",
        "x": 450,
        "y": 480,
        "wires": [
            [
                "54b1003a.e4e13"
            ]
        ]
    },
    {
        "id": "cc6dcd38.ffa83",
        "type": "inject",
        "z": "ebda401.e7556c",
        "name": "",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "3600",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 110,
        "y": 480,
        "wires": [
            [
                "4e6e64f8.d3a23c"
            ]
        ]
    },
    {
        "id": "54b1003a.e4e13",
        "type": "switch",
        "z": "ebda401.e7556c",
        "name": "Select country",
        "property": "payload",
        "propertyType": "msg",
        "rules": [
            {
                "t": "cont",
                "v": "Poland",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "Italy",
                "vt": "str"
            },
            {
                "t": "cont",
                "v": "Total:",
                "vt": "str"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 3,
        "x": 620,
        "y": 480,
        "wires": [
            [
                "d22369e8.5663e8"
            ],
            [
                "7821f413.21604c"
            ],
            [
                "50a91f56.b04fa"
            ]
        ]
    },
    {
        "id": "48d960e2.51c88",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "parse",
        "func": "array = msg.payload.split(\" \");\narray = array.filter(Boolean)\nfor (var i = 0; i < array.length; i++) {\n     array[i] = array[i].trim()\n }\nmsg.topic = array[1];  \nmsg.payload = {};\nmsg.payload.confirmed = array[2].replace(\",\", \"\");\nmsg.payload.newcases = array[3].replace(\"+\", \"\").replace(\",\", \"\");\nmsg.payload.deaths = array[4].replace(\",\", \"\");\nmsg.payload.newdeaths = array[5].replace(\"+\", \"\").replace(\",\", \"\");\nmsg.payload.recovered = array[7].replace(\",\", \"\");\nmsg.payload.active = array[8].replace(\",\", \"\");\nmsg.payload.serious = array[9].replace(\",\", \"\");\n//msg.payload.array = array;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 930,
        "y": 420,
        "wires": [
            [
                "978ed5e8.5bf618",
                "c6560917.528468",
                "30e38032.19474",
                "f323260b.3a85c8",
                "fc353f4b.bd07c",
                "2cebd7ec.6325b8"
            ]
        ]
    },
    {
        "id": "c6560917.528468",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Cases",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 196, \"nvalue\": 0, \"svalue\" :msg.payload.confirmed};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1090,
        "y": 440,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "30e38032.19474",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Deaths",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 197, \"nvalue\": 0, \"svalue\" :msg.payload.deaths};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1080,
        "y": 480,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "f323260b.3a85c8",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Recovered",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 198, \"nvalue\": 0, \"svalue\" :msg.payload.recovered};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1090,
        "y": 520,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "50a91f56.b04fa",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Total",
        "func": "array = msg.payload.split(\" \");\nfor (var i = 0; i < array.length; i++) {\n     array[i] = array[i].trim()\n }\nmsg.topic = array[1]; \nmsg.payload = {};\nmsg.payload.total = array[2].replace(\",\", \"\");\nmsg.payload.totalnewcases = array[4].replace(\",\", \"\");\nmsg.payload.totaldeaths = array[5].replace(\",\", \"\");\nmsg.payload.totalnewdeaths = array[6].replace(\",\", \"\");\nmsg.payload.totalrecovered = array[7].replace(\",\", \"\");\nmsg.payload.totalactivecases = array[9].replace(\",\", \"\");\nmsg.payload.totalcritical = array[10].replace(\",\", \"\");\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 810,
        "y": 520,
        "wires": [
            []
        ]
    },
    {
        "id": "fc353f4b.bd07c",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Active cases",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 199, \"nvalue\": 0, \"svalue\" :msg.payload.active};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1090,
        "y": 560,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "2cebd7ec.6325b8",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "Serious Critical",
        "func": "msg.payload = { \"command\": \"udevice\", \"idx\": 200, \"nvalue\": 0, \"svalue\" :msg.payload.serious};\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 1100,
        "y": 600,
        "wires": [
            [
                "b4ab87d5.800568"
            ]
        ]
    },
    {
        "id": "d22369e8.5663e8",
        "type": "throttle",
        "z": "ebda401.e7556c",
        "name": "",
        "throttleType": "time",
        "timeLimit": "1",
        "timeLimitType": "seconds",
        "countLimit": 0,
        "blockSize": 0,
        "locked": false,
        "x": 810,
        "y": 420,
        "wires": [
            [
                "48d960e2.51c88"
            ]
        ]
    },
    {
        "id": "7821f413.21604c",
        "type": "function",
        "z": "ebda401.e7556c",
        "name": "parse",
        "func": "array = msg.payload.split(\" \");\narray = array.filter(Boolean)\nfor (var i = 0; i < array.length; i++) {\n     array[i] = array[i].trim()\n }\nmsg.topic = array[1];  \nmsg.payload = {};\nmsg.payload.confirmed = array[2].replace(\",\", \"\");\nmsg.payload.newcases = array[3].replace(\"+\", \"\").replace(\",\", \"\");\nmsg.payload.deaths = array[4].replace(\",\", \"\");\nmsg.payload.newdeaths = array[5].replace(\"+\", \"\").replace(\",\", \"\");\nmsg.payload.recovered = array[8].replace(\",\", \"\");\nmsg.payload.active = array[9].replace(\",\", \"\");\nmsg.payload.serious = array[10].replace(\",\", \"\");\nmsg.payload.array = array;\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "x": 810,
        "y": 480,
        "wires": [
            []
        ]
    },
    {
        "id": "9748a36f.44bdd",
        "type": "mqtt-broker",
        "z": "",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "usetls": false,
        "compatmode": true,
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "willTopic": "",
        "willQos": "0",
        "willPayload": ""
    }
]

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Tuesday 24 March 2020 22:36
by Mooms
Very nice initiative !

When I import this script then Receive an error that Throttle node can't be found.

Do you know how I can resolve this?

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Wednesday 25 March 2020 8:23
by freijn
Mooms wrote: Tuesday 24 March 2020 22:36 Very nice initiative !

When I import this script then Receive an error that Throttle node can't be found.

Do you know how I can resolve this?
install palette :
node-red-contrib-throttle

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Wednesday 25 March 2020 11:10
by alanlsmith
Just a small point.

Whilst the 'Throttle' node limits the data to the first set the query comes across, which in the case of countries is today's data. It might be better to explicitly limit the data table selected by specifying a 'Switch' node between the 'Select country' and 'Parse' nodes to select the correct "msg.parts.index".

In the case of the total data (for the world) selecting the first set doesn't appear to return the correct data whereas selecting the "msg.parts.index" as 196 gives the correct numbers for today.

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Wednesday 25 March 2020 15:49
by freijn
alanlsmith wrote: Wednesday 25 March 2020 11:10 Just a small point.

Whilst the 'Throttle' node limits the data to the first set the query comes across, which in the case of countries is today's data. It might be better to explicitly limit the data table selected by specifying a 'Switch' node between the 'Select country' and 'Parse' nodes to select the correct "msg.parts.index".

In the case of the total data (for the world) selecting the first set doesn't appear to return the correct data whereas selecting the "msg.parts.index" as 196 gives the correct numbers for today.
Hi Alan,

As I am learning Node Red from examples. Would you mind sharing your suggested changes to us?
I do understand filtering by country does reduce the amount of data sent, but msg.part.index I have lost you :-(

Many thanks in advantage

Frank

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Wednesday 25 March 2020 17:36
by alanlsmith
If you set the debug node to show the "Complete msg object and attatch it to the parse node then in the debug window you will see the elements of the data object, if you then expand this by clicking on it you will see at the bottom a section called "parts", expand that and you will see the "index" that is the number you can use in a switch node.

One caveat: I think that index number may change for the world totals, according to the day of the data but I'm not sure.

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Wednesday 25 March 2020 19:39
by Mooms
freijn wrote: Wednesday 25 March 2020 8:23
Mooms wrote: Tuesday 24 March 2020 22:36 Very nice initiative !

When I import this script then Receive an error that Throttle node can't be found.

Do you know how I can resolve this?
install palette :
node-red-contrib-throttle
Everything now works perfectly! Thank you very much.

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Friday 27 March 2020 22:02
by freijn
I assume the site has changed again.. its not working anymore


27-3-2020 22:01:23node: parsefunction : (error)
"TypeError: Cannot read property 'replace' of undefined"

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Friday 27 March 2020 22:53
by FireWizard
Hi all,

As scraping data from a website seems not always reliable, it is better to use a JSON API.
The flow is much simpler.

Screenshot_corona 1.png
Screenshot_corona 1.png (10.76 KiB) Viewed 2290 times
Normally the data is updated once a day. So you can inject a timestamp once a day.

For the http request you can insert the country, you are interested in.
In my example, I have inserted for the Netherlands, but see the various possibilities here: https://covid19.mathdro.id/api
I used the following: https://covid19.mathdro.id/api/countries/Netherlands

This will give you the following data:

Screenshot_corona 2.png
Screenshot_corona 2.png (22.29 KiB) Viewed 2290 times
The function node gives you the following:

Screenshot_corona 3.png
Screenshot_corona 3.png (19.63 KiB) Viewed 2290 times
Do not forget the idx numbers and replace the debug node by a MQTT Out node.

Regards

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Friday 27 March 2020 22:54
by FireWizard
And here is the flow:

Code: Select all

[{"id":"50c0bfe7.9b2c7","type":"tab","label":"Corona","disabled":false,"info":""},{"id":"97919453.074a3","type":"inject","z":"50c0bfe7.9b2c7","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":220,"wires":[["8cee1b17.bc1ed"]]},{"id":"8cee1b17.bc1ed","type":"http request","z":"50c0bfe7.9b2c7","name":"","method":"GET","ret":"txt","paytoqs":false,"url":"https://covid19.mathdro.id/api/countries/Netherlands","tls":"","persist":false,"proxy":"","authType":"","x":340,"y":220,"wires":[["f0e866f7.3730a"]]},{"id":"bda954fa.7ba61","type":"debug","z":"50c0bfe7.9b2c7","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":890,"y":220,"wires":[]},{"id":"f0e866f7.3730a","type":"json","z":"50c0bfe7.9b2c7","name":"","property":"payload","action":"","pretty":false,"x":530,"y":220,"wires":[["f6646b4f.44c008"]]},{"id":"f6646b4f.44c008","type":"function","z":"50c0bfe7.9b2c7","name":"","func":"var msg1 ={};\nvar msg2 ={};\nvar msg3 ={};\nmsg1.payload = {\"command\":\"udevice\",\"idx\":123,\"nvalue\":0,\"svalue\":(parseFloat(msg.payload.confirmed.value.toString()))};\nmsg2.payload = {\"command\":\"udevice\",\"idx\":124,\"nvalue\":0,\"svalue\":(parseFloat(msg.payload.recovered.value.toString()))};\nmsg3.payload = {\"command\":\"udevice\",\"idx\":125,\"nvalue\":0,\"svalue\":(parseFloat(msg.payload.deaths.value.toString()))};\nreturn [[msg1,msg2,msg3]];","outputs":1,"noerr":0,"x":710,"y":220,"wires":[["bda954fa.7ba61"]]}]
Regards

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Saturday 28 March 2020 7:59
by Mooms
Hi FireWizard,

Thank you very much for the Flow you sent.

For some reason i’m receiving error messages in domoticz that the data is invalid.

Output from Node-Red:
{"command":"udevice","idx":937,"nvalue":0,"svalue":547}

Domoticz log:
2020-03-28 07:53:04.006 MQTT: Topic: domoticz/in, Message: {"command":"udevice","idx":941,"nvalue":0,"svalue":8647}
2020-03-28 07:53:04.007 Error: MQTT: Invalid data received!

Do you have any idea how I can fix this?

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Saturday 28 March 2020 9:49
by FireWizard
Ho,

Sorry, this was a copy paste error from my side.
As you can see in the last screenshot the "svalue" is a number (blue) and should be a string, so "8641" instead of 8641.

Change the function node as follows:

Code: Select all

var msg1 ={};
var msg2 ={};
var msg3 ={};
msg1.payload = {"command":"udevice","idx":123,"nvalue":0,"svalue":msg.payload.confirmed.value.toString()};
msg2.payload = {"command":"udevice","idx":124,"nvalue":0,"svalue":msg.payload.recovered.value.toString()};
msg3.payload = {"command":"udevice","idx":125,"nvalue":0,"svalue":msg.payload.deaths.value.toString()};
return [[msg1,msg2,msg3]]
Change your "idx" to your values.

Regards

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Saturday 28 March 2020 10:01
by Mooms
FireWizard wrote: Saturday 28 March 2020 9:49 Ho,

Sorry, this was a copy paste error from my side.
As you can see in the last screenshot the "svalue" is a number (blue) and should be a string, so "8641" instead of 8641.

Change the function node as follows:

Code: Select all

var msg1 ={};
var msg2 ={};
var msg3 ={};
msg1.payload = {"command":"udevice","idx":123,"nvalue":0,"svalue":msg.payload.confirmed.value.toString()};
msg2.payload = {"command":"udevice","idx":124,"nvalue":0,"svalue":msg.payload.recovered.value.toString()};
msg3.payload = {"command":"udevice","idx":125,"nvalue":0,"svalue":msg.payload.deaths.value.toString()};
return [[msg1,msg2,msg3]]
Change your "idx" to your values.

Regards
Excellent now it works properly ! Thank you for your quick response and spending the time to put this together.

Re: Coronavirus Covid-19 statistics to Domoticz custom sensor using Node Red

Posted: Saturday 28 March 2020 16:14
by FireWizard
Hi All,

As the Node Red flow, made yesterday evening, was a "quick and dirty" exercise, I decided to look once more to it.
As the previous flow requires a virtual "Text" sensor for each message, I thought that it would be more convenient to include all information in only one sensor. As I do not know, when the data is updated, I decided to poll the URL every hour. In order to avoid that the log for the sensors is extended every hour with an identical log entry, I have included a RBE node, so that the Domoticz sensor is only updated if the data has been updated.

Screenshot_corona 4.png
Screenshot_corona 4.png (30.08 KiB) Viewed 2223 times
In addition I have included also the "World" data. If you don't want it, you can delete the lower flow.
So users can select their own country in the upper flow and have the "World" data in the lower flow.
Users can change the texts in the function nodes to match their local language and do not forget to use your own idx's.

Screenshot_corona 5.png
Screenshot_corona 5.png (132.98 KiB) Viewed 2223 times

Letś hope that the values are not increasing too much and too quick.

Here is the complete flow.

Code: Select all

[{"id":"50c0bfe7.9b2c7","type":"tab","label":"Corona","disabled":false,"info":""},{"id":"97919453.074a3","type":"inject","z":"50c0bfe7.9b2c7","name":"1 Hour Tick","topic":"","payload":"","payloadType":"date","repeat":"3600","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":120,"wires":[["8cee1b17.bc1ed","69fb3c8f.03301c"]]},{"id":"8cee1b17.bc1ed","type":"http request","z":"50c0bfe7.9b2c7","name":"Request NL","method":"GET","ret":"txt","paytoqs":false,"url":"https://covid19.mathdro.id/api/countries/Netherlands","tls":"","persist":false,"proxy":"","authType":"","x":330,"y":100,"wires":[["f0e866f7.3730a"]]},{"id":"f0e866f7.3730a","type":"json","z":"50c0bfe7.9b2c7","name":"","property":"payload","action":"","pretty":false,"x":510,"y":100,"wires":[["f6646b4f.44c008"]]},{"id":"f6646b4f.44c008","type":"function","z":"50c0bfe7.9b2c7","name":"Prepare for Domoticz","func":"msg.payload = {\"command\":\"udevice\",\"idx\":391,\"nvalue\":0,\"svalue\":\"Confirmed: \"+ msg.payload.confirmed.value.toString() + \"\\n\" + \"Recovered: \" + msg.payload.recovered.value.toString() + \"\\n\" + \"Deaths: \"+ msg.payload.deaths.value.toString()};\nreturn msg\n","outputs":1,"noerr":0,"x":720,"y":100,"wires":[["e7e9e71.bc1c318"]]},{"id":"e7e9e71.bc1c318","type":"rbe","z":"50c0bfe7.9b2c7","name":"Send only, if changed","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":980,"y":100,"wires":[["a00c2ada.eefb98"]]},{"id":"a00c2ada.eefb98","type":"mqtt out","z":"50c0bfe7.9b2c7","name":"Domoticz In","topic":"domoticz/in","qos":"2","retain":"false","broker":"f9f13036.e28b58","x":1210,"y":120,"wires":[]},{"id":"69fb3c8f.03301c","type":"http request","z":"50c0bfe7.9b2c7","name":"Request World","method":"GET","ret":"txt","paytoqs":false,"url":"https://covid19.mathdro.id/api","tls":"","persist":false,"proxy":"","authType":"","x":340,"y":140,"wires":[["829c54f7.62b468"]]},{"id":"829c54f7.62b468","type":"json","z":"50c0bfe7.9b2c7","name":"","property":"payload","action":"","pretty":false,"x":510,"y":140,"wires":[["c27506ed.57bd7"]]},{"id":"39dea0.f416816","type":"rbe","z":"50c0bfe7.9b2c7","name":"Send only, if changed","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":980,"y":140,"wires":[["a00c2ada.eefb98"]]},{"id":"c27506ed.57bd7","type":"function","z":"50c0bfe7.9b2c7","name":"Prepare for Domoticz","func":"msg.payload = {\"command\":\"udevice\",\"idx\":392,\"nvalue\":0,\"svalue\":\"Confirmed: \"+ msg.payload.confirmed.value.toString() + \"\\n\" + \"Recovered: \" + msg.payload.recovered.value.toString() + \"\\n\" + \"Deaths: \"+ msg.payload.deaths.value.toString()};\nreturn msg\n","outputs":1,"noerr":0,"x":720,"y":140,"wires":[["39dea0.f416816"]]},{"id":"f9f13036.e28b58","type":"mqtt-broker","z":null,"name":"RPI1_ MQTT_Broker","broker":"192.168.10.51","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
Regards