Page 1 of 1
Graph question
Posted: Thursday 19 March 2020 13:39
by JimmyH1969
Hi,
I have a Custom Sensor in Domoticz that counts the infected corona people. In Domoticz this works fine.

- Domoticz counter.jpg (19.37 KiB) Viewed 1166 times
In the Dashticz i added it as a graph but the counter in the upper left corner is always 0,0 and is not updated.

- Dashticz counter.jpg (104.3 KiB) Viewed 1166 times
Any clue?
Re: Graph question
Posted: Saturday 21 March 2020 7:58
by Janco
Can't answer your question, but how did you set up this sensor in Domoticz?
Re: Graph question
Posted: Saturday 21 March 2020 11:39
by JimmyH1969
Getting the data from:
https://coronavirus-19-api.herokuapp.com/all
This will give you: {"cases":279338,"deaths":11587,"recovered":92906}
Ad some LUA code to parse the values and update a custom sensor with the values, like this:
Code: Select all
Debug = "On"
if Debug == "On" then
print "LUA-Corona Executed"
end
JSON = (loadfile "C:\\Program Files (x86)\\Domoticz\\scripts\\lua\\JSON.lua")()
function docorona(filename, ctype1, ctype2, ctype3)
file = io.open(filename, "r")
json_text = file:read()
file:close()
corona = JSON:decode(json_text)
commandArray[1]={['UpdateDevice']=ctype1..'|0|'..corona.cases}
commandArray[2]={['UpdateDevice']=ctype2..'|0|'..corona.deaths}
commandArray[3]={['UpdateDevice']=ctype3..'|0|'..corona.recovered}
if Debug == "On" then
print (tostring "Cases=" ..corona.cases.. " Deaths=" ..corona.deaths.. " recovered=" ..corona.recovered)
end
end
function docoronaNL(filename, cnltype1, cnltype2, cnltype3)
file = io.open(filename, "r")
json_text = file:read()
file:close()
coronanl = JSON:decode(json_text)
if Debug == "On" then
print (tostring "CasesNL=" ..coronanl.cases.. " DeathsNL=" ..coronanl.deaths.. " recoveredNL=" ..coronanl.recovered)
end
end
-- run the function and update the corresponding IDX
docorona("corona.txt", 927, 925, 926)
docoronaNL("coronaNL.txt", 1927, 1925, 1926)
return commandArray
I do this on Windows btw.
If you want the numbers from a specific country, you can do vor example:
https://coronavirus-19-api.herokuapp.co ... etherlands
I run this scheduled on a windows machine and output it to a .txt file in the domoticz folder.
Re: Graph question
Posted: Saturday 21 March 2020 17:54
by Janco
Thanks!
Re: Graph question
Posted: Sunday 22 March 2020 10:49
by laco
I have domoticz running on synology nas server.
How do I set up reading in the LUA script
https://coronavirus-19-api.herokuapp.co ... s/Slovakia
Re: Graph question
Posted: Sunday 22 March 2020 11:11
by JimmyH1969
No idea, i'm running on windows.
Re: Graph question
Posted: Sunday 22 March 2020 13:38
by Mike70
I made a bash script for world wide statistics.
With a litte editing you can use it for your own country.
I saved the file into /home/pi/domoticz/scripts/sh/corona_stats.sh
Code: Select all
#!/bin/bash
# /home/pi/domoticz/scripts/sh/corona_stats.sh
# Make 3 virtual sensors in Domoticz and name them WW_Cases, WW_Deaths and WW_Recovered
# Note the three IDX numbers of the counters and fill them into CASES_IDX, DEATHS_IDX, RECOVERED_IDX
# Make a SSH connection with your domoticz device and start crontab -e
# Put the rule below into crontab and save it
# * * * * * /home/pi/domoticz/scripts/sh/corona_stats.sh
# Now every minute there will be an update of the stats.
# Settings
CORONA_STATS_WEBSITE="https://coronavirus-19-api.herokuapp.com/all" # Corona stats website
DOMO_IP="192.168.1.100" # Domoticz IP (e.g. 192.168.1.100)
DOMO_PORT="8080" # Domoticz port (e.g. 8080)
CASES_IDX='855' # Domoticz IDX number of cases
DEATHS_IDX='856' # Domoticz IDX number of deaths
RECOVERED_IDX='857' # Domoticz IDX number of recovered
# Get data from Corona website
INPUT=$(curl "$CORONA_STATS_WEBSITE")
CASES=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $2//['}']/}')
DEATHS=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $4//['}']/}')
RECOVERED=$(echo "$INPUT" | awk -v FS="(:|,)" '{print $6//['}']/}')
echo
echo Number of infections.: $CASES
echo Number of deaths.....: $DEATHS
echo Number of recovered..: $RECOVERED
# Load data in domoticz
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=udevice&idx=$CASES_IDX&nvalue=0&svalue=$CASES"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=udevice&idx=$DEATHS_IDX&nvalue=0&svalue=$DEATHS"
curl -s -i -H "Accept: application/json" "http://$DOMO_IP:$DOMO_PORT/json.htm?type=command¶m=udevice&idx=$RECOVERED_IDX&nvalue=0&svalue=$RECOVERED"
Re: Graph question
Posted: Sunday 22 March 2020 14:54
by Janco
I use a DzVent script. It's simpler and more or less standalone (apart from the custom sensor).
- Spoiler: show
- return {
on = {
timer = { 'every hour' },
httpResponses = { 'coronaRetrieved' }
},
execute = function(domoticz, item)
if (item.isTimer) then
domoticz.openURL({
url = 'https://coronavirus-19-api.herokuapp.com/all',
method = 'GET',
callback = 'coronaRetrieved'
})
elseif (item.isHTTPResponse) then
if (item.ok) then -- statusCode == 2xx
local current = item.json.cases
domoticz.log(item.json.cases)
domoticz.devices('Corona Cases').updateCustomSensor(item.json.cases)
domoticz.log(item.json.deaths)
domoticz.devices('Corona Deaths').updateCustomSensor(item.json.deaths)
domoticz.log(item.json.recovered)
domoticz.devices('Corona Recovered').updateCustomSensor(item.json.recovered)
end
end
end
}
It retrieves the data for the whole word. It can be easily adapted for specific countries though.
Re: Graph question
Posted: Monday 23 March 2020 12:10
by JimmyH1969
Nice, now let's hope the "cases" and "deaths" counters stop counting real soon....