Is it possible to configure the Coronavirus integration to go down to US - State level?

The John Hopkins site has state data on the map so wondering if it would be possible to alter the configuration to get state level details. I am guessing that would require code changes to the integration but perhaps it can be hacked?

I have a NODE RED configuration which I have used to track down the country level confirmed cases of coronavirus COVID19 using the open APIs’ used by WHO. Goto node red and import the below code by pasting it from your clipboard.

You will need to change the country name node to "“UNITED STATES OF AMERICA”:

[{"id":"10cbe271.08c8ae","type":"www-request","z":"39d45ee1.7a82b2","name":"","method":"GET","ret":"txt","url":"https://services.arcgis.com/5T5nSi527N4F7luB/arcgis/rest/services/COVID_19_CasesByCountry(pt)_VIEW/FeatureServer/0/query?f=json&where=1%3D1&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=cum_conf%20desc&resultOffset=0&resultRecordCount=115&cacheHint=true","follow-redirects":true,"persistent-http":true,"tls":"","x":510,"y":200,"wires":[["4e4d99e0.686f28"]]},{"id":"4e4d99e0.686f28","type":"json","z":"39d45ee1.7a82b2","name":"Convert to object","property":"payload","action":"","pretty":false,"x":730,"y":200,"wires":[["cbb8b069.848d5"]]},{"id":"cbb8b069.848d5","type":"function","z":"39d45ee1.7a82b2","name":"calculate","func":"obj = msg.payload;\ncountry = flow.get(\"country\");\nvar errorProp = 'error';\nif(obj.hasOwnProperty(errorProp)){\n    msg.payload = {'total_cases': '0', 'total_cases_india': '0'};\n}\nelse\n{\n    total_cases = 0;\n    total_cases_india = 0;\n    \n    for (const key of obj.features) \n    {   total_cases = total_cases + key.attributes.cum_conf\n        if(key.attributes.ADM0_NAME == country)\n            total_cases_india = total_cases_india + key.attributes.cum_conf\n    }\n    msg.payload = {'total_cases': total_cases, 'total_cases_india': total_cases_india}\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":920,"y":200,"wires":[["5967ddab.b63364","93a9cbb8.aea688"]]},{"id":"6e45df48.472e5","type":"change","z":"39d45ee1.7a82b2","name":"Country Name","rules":[{"t":"set","p":"country","pt":"flow","to":"INDIA","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":200,"wires":[["10cbe271.08c8ae"]]},{"id":"5967ddab.b63364","type":"ha-entity","z":"39d45ee1.7a82b2","name":"Covid19 Wordwide","server":"82494656.b2ac98","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"covid19"},{"property":"device_class","value":"none"},{"property":"icon","value":"mdi:bacteria"},{"property":"unit_of_measurement","value":""}],"state":"payload.total_cases","stateType":"msg","attributes":[{"property":"name","value":"Covid19 Wordwide Cases","valueType":"str"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":1150,"y":140,"wires":[["e90a7547.a7e668"]]},{"id":"93a9cbb8.aea688","type":"ha-entity","z":"39d45ee1.7a82b2","name":"Covid19 Country Specific","server":"82494656.b2ac98","version":1,"debugenabled":false,"outputs":1,"entityType":"sensor","config":[{"property":"name","value":"covid19_india"},{"property":"device_class","value":"none"},{"property":"icon","value":"mdi:bacteria"},{"property":"unit_of_measurement","value":""}],"state":"payload.total_cases_india","stateType":"msg","attributes":[{"property":"name","value":"Covid19 India Cases","valueType":"str"}],"resend":true,"outputLocation":"","outputLocationType":"none","inputOverride":"allow","x":1150,"y":280,"wires":[[]]},{"id":"b15d752e.3c4af8","type":"inject","z":"39d45ee1.7a82b2","name":"","topic":"","payload":"","payloadType":"date","repeat":"1800","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":200,"wires":[["6e45df48.472e5"]]},{"id":"82494656.b2ac98","type":"server","z":"","name":"Home Assistant"}]

Don’t forget to change your change your entitiy names accordingly.

Hope this helps!

Unfortunately I don’t have Node Red installed so that’s a little too much effort for me right now :slight_smile:
But thanks for the info I may be able to find time in future to work on it.

You can get Node Red running in no time. If you’re running a HASSIO installation: Install node red from Supervisor and Node Red companion component using HACS. You are then good to go.

This is how my output looks like in HA:
image

Thanks, I am on an old school hassbian install. I plan to move my install from rpi to server so maybe then I will also convert to hassio if not too complicate to move it all over.

Also just realized this doesn’t get me down to the state level. I already have US level data using the existing integration. Was just wondering if I can get down to my state. Hawaii currently has only 2 confirmed cases so I would like to place an alert on when this data changes.

Comment Removed.

7 Likes

Hi,

Copied and pasted your code but I’m getting an error:
Invalid config for [sensor.rest]: [json_attributes_path] is an invalid option for [sensor.rest]. Check: sensor.rest->json_attributes_path. (See ?, line ?).

Thanks for this! I was struggling to find any documentation around the REST support for that data source. Your code worked as posted for me!

1 Like

Comment Removed.

Great, marked as solution. Do you know how frequently this platform updates? There doesn’t seem to be an option for setting how frequently to poll.

Comment Removed.

Thanks, that is interesting, I didn’t realize entities can have feature attributes not documented, that seems a little confusing. Maybe it would be good to link that page on entities that adhere to the options.
I did actually search for scan_interval and found the page you linked but I didn’t realize it would apply to the restful sensors. The note that the options are being phased out also made me think they perhaps didn’t apply.

I am trying to figure out how to handle “undefined” state. I think the site is probably overwhelmed at times so it fails then the sensor state becomes undefined. This triggers my change of state automation.

I could add conditions to my change of state automation to ignore from undefined or to undefined changes but then I could miss an update if it changes from 2 to undefined and then changes to 3 for example I wouldn’t get triggered.

So I would prefer to ignore state changes to undefined somehow. Perhaps this can be done in the value_template somehow I am just not sure how to get the current value to return that when undefined is found…doing more research to figure this out.

@ Silicon_Avatar: How did you figure out that url?
Tried with DE, but did not work. AND: Is it possible, to dig more into it i.e. on a City level?

At this url, the data is available for cities, but it seems not to be actual…

Comment Removed.

Does anyone know how to get the COUNTY level data in the US?

Anyone know how to get the active count for a state? I can get confirmed, recovered and deaths but I can’t seem to find current/active. The API used above seems to return 0 for the US and all states.

Did you figure out how to get the county-level data? I’ve been working on this and can’t figure it out.

Yes. The solution by Petro here worked for me: Help with Corona Scrape Sensor