Help with Corona Scrape Sensor

I would like this too. Maybe the home assistant scrape cant handle the advanced website for displaying the stats. Maybe if we find a simpler site that has the same data?

No, i found nothing

Check https://rapidapi.com/astsiatsko/api/coronavirus-monitor

That site was so nice, I just created an iframe card with the link to the website, then set it to Panel mode in Lovelace to fill the screen in Home Assistant, and set the icon to mdi:biohazard.

type: iframe
url: >-
  https://gisanddata.maps.arcgis.com/apps/opsdashboard/index.html#/bda7594740fd40299423467b48e9ecf6
aspect_ratio: 0%

image

1 Like

have now done it via api for AUSTRIA
2020-03-01 09_42_21-Home Assistant

#######################################################################
### CORONA Virus ###
#################### 
  - platform: rest
    resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27Austria%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true"
    name: Corona Virus Erkrankt
    value_template: "{{ value_json.features[0].attributes.Confirmed }}"
    
  - platform: rest
    resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27Austria%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true"
    name: Corona Virus Tote
    value_template: "{{ value_json.features[0].attributes.Deaths }}"
  
  - platform: rest
    resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27Austria%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true"
    name: Corona Virus Geheilt
    value_template: "{{ value_json.features[0].attributes.Recovered }}"
      
  - platform: rest
    resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27Austria%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true"
    name: Corona Virus letztes Update
    value_template: "{{ value_json.features[0].attributes.Last_Update |replace ('000', '') | int | timestamp_custom('%H:%M %d.%m.%Y')}}"

automation:

- alias: Corona Virus 
  trigger:
  - entity_id: sensor.corona_virus_erkrankt
    platform: numeric_state
    above: 0
  action:
  - service: telegram_bot.send_message
    data_template:
      parse_mode: markdown
      target:
        - xxxxxxxxxxxxxxxxxxxxx
      title: "-🦠-😷 CORONA Virus 😷-🦠-"
      message: "Es sind {{ states('sensor.corona_virus_erkrankt') }} Personen in Österreich bestätigt! \n
                \n
                letztes Update: {{ states('sensor.corona_virus_letztes_update') }}\n
                Tote: {{ states('sensor.corona_virus_tote') }}\n
                Geheilt: {{ states('sensor.corona_virus_geheilt') }}\n
                \n
                Weitere Infos: \n
                https://www.arcgis.com/apps/opsdashboard/index.html#/85320e2ea5424dfaaa75ae62e5c06e61"

Lovelace:

- type: custom:vertical-stack-in-card
        cards:
        - type: entities
          entities:
            - type: custom:text-divider-row
              text: Corona - Virus
            - entity: sensor.corona_virus_letztes_update
              type: custom:multiple-entity-row
              name: Corona Virus Infos
              icon: mdi:bug-outline
              show_state: false
              #secondary_info: last-changed
              entities:
                - entity: sensor.corona_virus_erkrankt
                  name: Erkrankt
                - entity: sensor.corona_virus_geheilt
                  name: Geheilt
                - entity: sensor.corona_virus_tote
                  name: Tote
            - entity: sensor.corona_virus_letztes_update
              icon: mdi:update
4 Likes

Thanks for this!
One thing, your fetching the api 4 times on every update interval.
With json_attributes you can do all in one sensor. (Germany here)

  - platform: rest
    resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200)%20AND%20(Country_Region%3D%27Germany%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true"
    name: Corona Virus Germany
    value_template: "{{ value_json.features[0].attributes.Confirmed }}"
    json_attributes_path: "$.features[0].attributes"
    json_attributes:
      - Deaths
      - Recovered
      - Last_Update

Auswahl_296

4 Likes

yes this is better, thanks

Thanks for posting these API calls. For the US, the result set is a little different and returns results by each state and location. I created these calls for the US and one for North Carolina, US.

  #===================================
  # 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
      
  - 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%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

For other states, change the state code in the URL above:
… (Province_State%20LIKE%20%27%25NC%27) …

As of March 1, 2020, there are no cases in NC, so it returns null results. I am not sure how to convert those nulls to zero. I’ll play around with it.

4 Likes

It looks like Home Assistant handles the Null json array value fine, so no need to convert to zero. It shows the value as None.

This is great! Worked for me! Now I’m trying to figure out how to get the number for world total too. I haven’t been able to figure it out yet.

Use this line for world:

resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&&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"
  - 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 World
    value_template: "{{ value_json.features[0].attributes.Recovered }}"
    json_attributes_path: "$.features[0].attributes"
    json_attributes:
      - Confirmed
      - Deaths
      - Recovered
1 Like

Thank you all, Now how do I display the Confirmed and Deaths, right now I’m only getting Recovered to display?

I created template sensors for use in Lovelace:

  - 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'

Scroll 7 posts up. There’s an example by @Johann_Edelmuller with the Multiple Entity Row card.

Have you a solution to get the last updated attribute readable?

See my post up, i have it convertet

I don’t particularly like @Johann_Edelmuller’s method because it assumes that the updates happen on the exact second. There’s no guarentee that is true.

Using @Johann_Edelmuller’s method, the sensor should be

    value_template: "{{ value_json.features[0].attributes.Last_Update | int / 1000 | timestamp_custom('%H:%M %d.%m.%Y') }}"

and @VDRainer’s for the same format on a template sensor:

    value_template: >
     {% set sensor = 'sensor.corona_virus_germany' %}
     {{ state_attr(sensor, 'Last_Update') | int / 1000 | timestamp_custom('%H:%M %d.%m.%Y') }}"

As i didn’t want another template sensor i decided to put this value to the state of the sensor.

sensor:
  - platform: rest
    resource: "https://services1.arcgis.com..."
    name: Corona Virus Germany
    value_template: "{{ (value_json.features[0].attributes.Last_Update / 1000) | int | timestamp_custom('%d.%m.%Y %H:%M') }}"
    json_attributes_path: "$.features[0].attributes"
    json_attributes:
      - Confirmed
      - Deaths
      - Recovered

So now there’s everything in one sensor. :mask:
20200302_17:34:56_001

  - type: entities
    entities:
      - type: 'custom:multiple-entity-row'
        entity: sensor.corona_virus_germany
        show_state: false
        icon: 'mdi:biohazard'
        name: Corona Germany
        style: |
          :host {
            --paper-item-icon-color: cyan;
          }
        entities:
          - attribute: Confirmed
            name: Confirmed
          - attribute: Deaths
            name: Deaths
          - attribute: Recovered
            name: Recovered
        secondary_info:
          entity: sensor.corona_virus_germany
          name: false
4 Likes

FYI, if you go that route and use this template with device_class: timestamp, the UI will perform all the fancy math to make it read x minutes ago.

sensor:
  - platform: rest
    resource: "https://services1.arcgis.com..."
    name: Corona Virus Germany
    value_template: "{{ (value_json.features[0].attributes.Last_Update / 1000) | int | timestamp_custom('%Y-%m-%dT%H:%M:%S.%f+00:00') }}"
    device_class: timestamp
    json_attributes_path: "$.features[0].attributes"
    json_attributes:
      - Confirmed
      - Deaths
      - Recovered
2 Likes

Yeah! I tried that yesterday with relative_time and templating, but failed and gave up.
Looks like it doesn’t work.