Help with Corona Scrape Sensor

Hi Salty,
I copied and pasted your code, but I’m getting an error.

Tue Mar 10 2020 21:36:33 GMT-0700 (Pacific Daylight Time)
Invalid config for [sensor.rest]: [json_attributes_path] is an invalid option for [sensor.rest]. Check: sensor.rest->json_attributes_path. (See ?, line ?).
Invalid config for [sensor.rest]: [json_attributes_path] is an invalid option for [sensor.rest]. Check: sensor.rest->json_attributes_path. (See ?, line ?).

They now have an official Coronavirus integration for HA 0.106+ . However, I am still using my original sensors, but the John Hopkins website changed how they were reporting. It was previously on a county basis in the USA and was being reported as County, ST (ex. Wake, NC). And I was performing a query with LIKE “NC”, but within the last couple of days it is now on a state level only, and they switched to full state names, so that LIKE query now must use the whole state name (ex. North Carolina).

In my sensors.yaml file, I am monitoring USA, North Carolina, and South Carolina:

  #===================================
  # Corona Virus Statistics
  #===================================
  - platform: rest
    resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Country_Region%3D%27US%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"
    name: Corona Virus US
    value_template: "{{ value_json.features[0].attributes.Recovered }}"
    json_attributes_path: "$.features[0].attributes"
    json_attributes:
      - Confirmed
      - Deaths
      - Recovered
    scan_interval: 3600  #seconds
      
  - platform: rest
    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%20%27%25North%20Carolina%27)%20%20&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"
    # 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%20%27%25NC%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"
    name: Corona Virus North Carolina
    value_template: "{{ value_json.features[0].attributes.Recovered }}"
    json_attributes_path: "$.features[0].attributes"
    json_attributes:
      - Confirmed
      - Deaths
      - Recovered
    scan_interval: 3600  #seconds
      
      
  - platform: rest
    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%20%27%25South%20Carolina%27)%20%20&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"
    # 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%20%27%25SC%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"
    name: Corona Virus South Carolina
    value_template: "{{ value_json.features[0].attributes.Recovered }}"
    json_attributes_path: "$.features[0].attributes"
    json_attributes:
      - Confirmed
      - Deaths
      - Recovered
    scan_interval: 3600  #seconds      
      
      
  - platform: template
    sensors: 
      corona_virus_us_confirmed:
        value_template: "{{ state_attr('sensor.corona_virus_us', 'Confirmed') }}"
        friendly_name: 'Corona Virus US Confirmed Cases'

  - platform: template
    sensors: 
      corona_virus_us_deaths:
        value_template: "{{ state_attr('sensor.corona_virus_us', 'Deaths') }}"
        friendly_name: 'Corona Virus US Deaths'

  - platform: template
    sensors: 
      corona_virus_us_recovered:
        value_template: "{{ state_attr('sensor.corona_virus_us', 'Recovered') }}"
        friendly_name: 'Corona Virus US Recovered'

  - platform: template
    sensors: 
      corona_virus_north_carolina_confirmed:
        value_template: "{{ state_attr('sensor.corona_virus_north_carolina', 'Confirmed') }}"
        friendly_name: 'Corona Virus NC Confirmed Cases'

  - platform: template
    sensors: 
      corona_virus_north_carolina_deaths:
        value_template: "{{ state_attr('sensor.corona_virus_north_carolina', 'Deaths') }}"
        friendly_name: 'Corona Virus NC Deaths'

  - platform: template
    sensors: 
      corona_virus_north_carolina_recovered:
        value_template: "{{ state_attr('sensor.corona_virus_north_carolina', 'Recovered') }}"
        friendly_name: 'Corona Virus NC Recovered'

  - platform: template
    sensors: 
      corona_virus_south_carolina_confirmed:
        value_template: "{{ state_attr('sensor.corona_virus_south_carolina', 'Confirmed') }}"
        friendly_name: 'Corona Virus SC Confirmed Cases'

  - platform: template
    sensors: 
      corona_virus_south_carolina_deaths:
        value_template: "{{ state_attr('sensor.corona_virus_south_carolina', 'Deaths') }}"
        friendly_name: 'Corona Virus SC Deaths'

  - platform: template
    sensors: 
      corona_virus_south_carolina_recovered:
        value_template: "{{ state_attr('sensor.corona_virus_south_carolina', 'Recovered') }}"
        friendly_name: 'Corona Virus SC Recovered'

And in lovelace I have:

entities:
  - entities:
      - entity: sensor.installed_version
        name: Installed
      - entity: sensor.latest_available_version
        name: Available
    entity: sensor.installed_version
    icon: 'mdi:home-assistant'
    name: HA Version
    show_state: false
    type: 'custom:multiple-entity-row'
  - entity: sensor.hacs
    name: HACS
    style: |
      :host {
        {% if states(config.entity) != '0' %}
          # color: blue;
          --paper-item-icon-color: red;
        {% endif %};
      }
    tap_action:
      action: navigate
      navigation_path: /hacs
  - entities:
      - entity: sensor.corona_virus_us_confirmed
        name: Confirmed
      - entity: sensor.corona_virus_us_deaths
        name: Deaths
      - entity: sensor.corona_virus_us_recovered
        name: Recovered
    entity: sensor.corona_virus_us
    icon: 'mdi:biohazard'
    name: USA Corona
    show_state: false
    type: 'custom:multiple-entity-row'
  - entities:
      - entity: sensor.corona_virus_north_carolina_confirmed
        name: Confirmed
      - entity: sensor.corona_virus_north_carolina_deaths
        name: Deaths
      - entity: sensor.corona_virus_north_carolina_recovered
        name: Recovered
    entity: sensor.corona_virus_north_carolina
    icon: 'mdi:biohazard'
    name: NC Corona
    show_state: false
    type: 'custom:multiple-entity-row'
  - entities:
      - entity: sensor.corona_virus_south_carolina_confirmed
        name: Confirmed
      - entity: sensor.corona_virus_south_carolina_deaths
        name: Deaths
      - entity: sensor.corona_virus_south_carolina_recovered
        name: Recovered
    entity: sensor.corona_virus_south_carolina
    icon: 'mdi:biohazard'
    name: SC Corona
    show_state: false
    type: 'custom:multiple-entity-row'
type: entities

The lovelace code uses the custom multiple entity row plugin. I just added it to my version monitoring card:

image

1 Like

And for your own US state, you would change the rest resource URL to have your state name by replacing this portion of the URL:
(Province_State%20LIKE%20%27%25North%20Carolina%27)

So for New York:
(Province_State%20LIKE%20%27%25New%20York%27)

Or Montana:
(Province_State%20LIKE%20%27%25Montana%27)

Edit: I see now that I inadvertently left in the %25 “ends with” from the prior County, State query, but it doesn’t hurt anything. If you want to remove it, just remove %25 from the above examples.

Just FYI, these are HTML URL encodings. This can be thought of as a SQL query embedded in an API call. SELECT * WHERE (Province_State LIKE ‘North Carolina’)

Edit: And now that I think about it, it could now be a simple equals instead of LIKE, but the hell with it. :slight_smile:

I can’t figure out how to go to a specific attribute using your code is it:

confirmed: >
            {%- set features = state_attr('sensor.corona_virus_rest', 'features') %}
            {{ features | map(attribute='attributes[1].Confirmed')  }}

or

confirmed: >
            {%- set features = state_attr('sensor.corona_virus_rest', 'features[1]') %}
            {{ features | map(attribute='attributes.Confirmed')  }}

or something else…

All you have to do is copy and paste the whole setup… no altering. Why are you trying to modify it?

Cuz I want both country and a specific state using only one rest. I can do it with 2 rest…

you’d have to use a different method. Mine dynamically finds the closest state to your home location with the value and only outputs the whole country. You can’t modify the code to get a specific state without performing a filtered look up based on the name. I.E. heavy changes, not a simple ‘change the item in the list’ change.

but under attributes of your rest sensor I see my state…can’t I just point to these attributes directly in lovelace?

I don’t expose the values for that. The exposed values are a summation of all provinces/states. It’s not selecting a state and displaying those.

You see how closest looks like alot of code? You’d need all that code and change this line to output Confirmed.

{{ features[loc.dist.index(loc.dist | min)].Province_State }}

So you’d need to add 3 new attributes: closest_recovered, closest_deaths, etc. using the modified code.

OK I’ll play around and see what I can find, I’m just curious to see if I can pull this through.

Thanks!

How do you get the sensor.latest_available_version and sensor.installed_version ?

Since the last update of multiple entity row, the secondary_info shows relative time.
Auswahl_301
Config from my post above.

  • Change in the rest sensor
value_template: "{{ (value_json.features[0].attributes.Last_Update / 1000) | int | timestamp_local }}"
  • Change in multiple-entity-row
                secondary_info:
                  entity: sensor.corona_virus_germany
                  name: false
                  format: relative

Ok I did what you said and managed to get it to work.

1 Like

I cannot get the style part to work. Everything looks fine. Copy pasted, retyped, spaces are fine. Icon color does not chage.

So your closest algorithm isn’t working all the time, it thinks I’m located closer to another province and messed all my numbers. Will try to make a simple for loop, something like:
For x through all features
{
If province_state == myprovince
{
Features[x].province_state
}
x++
}
Need to find how to code this in Hassio…

You need to install card-mod.

use selectattr, don’t loop it.

{%- set features = state_attr('sensor.corona_virus_rest', 'features') %}
{%- set features = features | map(attribute='attributes') | list %}
{%- set province = features | selectattr('Province_State', 'eq', 'blah') | first %}
{{ province.Province_State }}
1 Like

By the way, when does this code update the data? Every hour?

Hello, I don’t think there’s data on the Johns Hopkins site on the County Level. Can someone please help me figure out how to scrape the TOTAL and DEATHS on the Santa Clara County website? Here’s the page:

https://www.sccgov.org/sites/phd/DiseaseInformation/novel-coronavirus/Pages/home.aspx

Thanks in advance for your help!

whenever the rest sensor updates. You need to add it in the entity_id field for the updates to occur because the features attribute does not get monitored for changes.