Help with Corona Scrape Sensor

Thanks for sharing this. I got it working on my setup. How do I get it to stay up to date?

Thanks

add ‘entity_id: sensor.whatever_your_rest_sensor’ is to the template sensor.

1 Like

Can someone show what I am doing wrong here please? This rest sensor only updates if I reboot Home Assistant. I have searched and tried every solution I could find and still cannot get it to update on its own. Does the scan_interval: have to be in a different location?

Thanks

- platform: rest
  name: Kansas Corona
  resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Country_Region%3D%27US%27)%20AND%20(Province_State%20LIKE%27Kansas%27)&outStatistics=%5B%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Confirmed%22%2C%22outStatisticFieldName%22%3A%22Confirmed%22%7D%2C%20%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Deaths%22%2C%22outStatisticFieldName%22%3A%22Deaths%22%7D%2C%20%7B%22statisticType%22%3A%22sum%22%2C%22onStatisticField%22%3A%22Recovered%22%2C%22outStatisticFieldName%22%3A%22Recovered%22%7D%5D"
  json_attributes_path: "$.features[0].attributes"
  scan_interval: 3600  #seconds
  value_template: "{{ value_json.features[0].attributes.Confirmed }}"
  unit_of_measurement: "Confirmed"
  json_attributes:
    - Confirmed
    - Deaths
    - Recovered
  
- platform: template
  sensors: 
    kansas_corona_deaths:
      value_template: "{{ state_attr('sensor.kansas_corona', 'Deaths') }}"
      friendly_name: 'Kansas Corona Deaths'
      entity_id: 'sensor.kansas_corona'

- platform: template
  sensors: 
    kansas_corona_recovered:
      value_template: "{{ state_attr('sensor.kansas_corona', 'Recovered') }}"
      friendly_name: 'Kansas Corona Recovered'
      entity_id: 'sensor.kansas_corona'

- platform: template
  sensors: 
    kansas_corona_confirmed:
      value_template: "{{ state_attr('sensor.kansas_corona', 'Confirmed') }}"
      friendly_name: 'Kansas Corona Confirmed Cases'
      entity_id: 'sensor.kansas_corona'

the rest sensor only updates every few hours…

The Johns Hopkins website now contains COUNTY level data in the US. Does anyone know how to get the data?

I don’t know enough about the API to answer that question, maybe @Johann_Edelmuller can share how he derived the resource link.

EDIT: It looks like the field “Admin2” is County but it’s always null in my queries. Not sure how to populate that field.

The calls by platform: rest. Which yaml file are these placed into?

sensor.yaml, the sensor section of your configuration.yaml.

Would this help? I found this and was able to pull up the State of Kansas and it list all the Counties and current stats. The card you shared above that shows distance is not working for me anymore now that more data is being collected. Mine shows 119 miles away and looking at the John Hopkins map, It’s a lot closer than that. I just don’t know anything about json queries really. Not sure if this would help with anything or not.

https://services9.arcgis.com/N9p5hsImWXAccRNI/arcgis/rest/services/Nc2JKvYFoAEOFCG5JSI6/FeatureServer/1/query?f=json&where=(Country_Region%3D'US')%20AND%20(Province_State%3D'Kansas')&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=200&cacheHint=true

Yes, it looks like it will. It has ‘Admin2’ being returned. What doesn’t make sense to me is WHY you’re getting ‘Admin2’ populated when your query doesn’t appear to request it. I’ll have to compare some things tonight when I get home.

EDIT: As for the queries they are pretty simple. There’s just a syntax to them. All the %xx’s in the URL are escaped characters because URL’s only accept specific characters. So %20 is space, and %3D is =.

1 Like

so after comparing our urls, the only difference is the starting portion of the URL and nothing about the query or data sorting is different. It seems the URL you are using is returning the extra information. What’s odd is that I cannot change the URL to another state. All other states but Kansas return invalid requests.

@iamhueman

OK last reply, Figured it out. It’s a different server. Took me a while to find it:

SO:

Basically add these together into 1 URL to make your URL

First part, this should not change for anyone, and this is only data in the US.

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases_US/FeatureServer/0/query?f=json&where=

Next add this for the query to find any confirmed cases

(Confirmed%20%3E%200)

This translates to (Confirmed > 0)

Next add this to the query to find your state:

%20AND%20(Province_State%3D%27New%20York%27)

this translates to AND (Province_State='New York'). So just replace your state between the two %27's. If you have a space in your state name, replace your space with %20

Next add this to the query to find your county:

%20AND%20(Admin2%3D%27XXXXXXXX%27)

this translates to AND (Admin2='XXXXXXXX'). So just replace your state between the two %27's. If you have a space in your state name, replace your space with %20

Lasty, just add this bit to finish. I’m sure this can be shortened because this should only get a single county but I don’t feel like reverse engineering all the calls and proper variables.

&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true

The resulting URL in this example is:

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases_US/FeatureServer/0/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Province_State%3D%27New%20York%27)%20AND%20(Admin2%3D%27XXXXXXXX%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true
1 Like

Hmmm, returns…

{“objectIdFieldName”:“OBJECTID”,“uniqueIdField”:{“name”:“OBJECTID”,“isSystemMaintained”:true},“globalIdFieldName”:"",“features”:[]}

From this…

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases_US/FeatureServer/0/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Province_State%3D%27Kansas%27)%20AND%20(Admin2%3D%27Kansas%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true

It didn’t find one of your attributes. So either your state or county name is incorrect.

EDIT: Looking at the url and removing the county bit, it’s clear that your county name is incorrect. Now remember, this is filtering out counties with zero confirmed.

EDIT2: There is no Kansas county in their database. So that’s ultimately your problem.

Admin2 field is where County goes?

Yes see this portion of my last post

1 Like

Thanks,
I’ll play around with it this evening, There is definitely no cases in my county and must be why it doesn’t return anything for me. I’ll try to add it to the button card you shared that shows closest cases and distance away and see if I can get it to work. I know there are cases within 40-50 miles but it still shows 119 miles.

Thanks for working on this.

If you omit the query on the county, then it should work:

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases_US/FeatureServer/0/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Province_State%3D%27Kansas%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true

Also gotta remember that the site might be missing data.

1 Like

That worked, Thanks

Trying this now to see if it works on the Button Card you shared above for closest distance.

https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases_US/FeatureServer/0/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27US%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true

edit: It worked! I think, it did change from 119 miles in Kansas to 66 miles in Oklahoma. Now I just got to figure out how to get this rest sensor to update. I don’t know why but this sensor does not update automatically. I have to reboot my RPI to get this to update.

image

- platform: rest
  name: Corona Virus Rest
  resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases_US/FeatureServer/0/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27US%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true"
  scan_interval: 3600  #seconds
  value_template: "{{ value_json.features | length }}"
  json_attributes:
    - features