Help with Corona Scrape Sensor

you could try changing it to

secondary_info: last-changed

and that should give you a better result in the meantime

1 Like

Thanks, but that doesn’t show the ‘Last_Update’ from the Rest API, most of the time it shows the last restart of HA. :slightly_smiling_face:

I’m embarrassed to say that it took me quite a bit to find the URL for the USA, here it is:

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%27US%27)&returnGeometry=false&spatialRel=esriSpatialRelIntersects&outFields=*&orderByFields=Confirmed%20desc%2CCountry_Region%20asc%2CProvince_State%20asc&outSR=102100&resultOffset=0&resultRecordCount=250&cacheHint=true

I tried United%20States, USA, America… Ended up just being US. :man_facepalming:

Maybe you guys already know but check out 0.106.3 :wink:

Why is that? EDIT Never mind, new integration.

Well I’m impatient and haven’t updated to 106. Also @VDRainer’s sensor didn’t work for the USA because theres 9 million state provinces in the us. Anyways, if anyone is interested in a USA sensors…

rest

  - 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%27US%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 Rest
    value_template: "{{ value_json.features | length }}"
    json_attributes:
      - features

template

  - platform: template
    sensors:
      corona_virus_usa:
        friendly_name: Corona Virus USA
        value_template: >
          {%- set features = state_attr('sensor.corona_virus_rest', 'features') %}
          {%- set last_updated = features | map(attribute='attributes.Last_Update') | list | max / 1000 %}
          {{ last_updated | timestamp_custom('%Y-%m-%dT%H:%M:%S.%f+00:00') }}
        attribute_templates:
          confirmed: >
            {%- set features = state_attr('sensor.corona_virus_rest', 'features') %}
            {{ features | map(attribute='attributes.Confirmed') | list | sum }}
          deaths: >
            {%- set features = state_attr('sensor.corona_virus_rest', 'features') %}
            {{ features | map(attribute='attributes.Deaths') | list | sum }}
          recovered: >
            {%- set features = state_attr('sensor.corona_virus_rest', 'features') %}
            {{ features | map(attribute='attributes.Recovered') | list | sum }}
          closest: >
            {%- set features = state_attr('sensor.corona_virus_rest', 'features') %}
            {%- set features = features | map(attribute='attributes') | list %}
            {%- set loc = namespace(dist=[]) %}
            {%- for feature in features %}
            {%- set loc.dist = loc.dist + [ distance(feature.Lat, feature.Long_, 'zone.home') ] %}
            {%- endfor %}
            {{ features[loc.dist.index(loc.dist | min)].Province_State }}
          miles_away: >
            {%- set features = state_attr('sensor.corona_virus_rest', 'features') %}
            {%- set features = features | map(attribute='attributes') | list %}
            {%- set loc = namespace(dist=[]) %}
            {%- for feature in features %}
            {%- set loc.dist = loc.dist + [ distance(feature.Lat, feature.Long_, 'zone.home') ] %}
            {%- endfor %}
            {{ loc.dist | min | round }}

You end up with a sensor that tells you the total deaths, confirmed, recovered, closest province with the virus, and how many miles it is away.

image

And here’s a custom:button-card configuration.

type: custom:button-card
show_state: false
show_label: true
label: |
  [[[
    var empty = ' '
    if (entity === undefined)
      return empty;

    function getEntityIcon(text, icon, color){
      return `<ha-icon
        icon="${icon}"
        style="size: 10%; color: ${color};">
        </ha-icon><span> ${text}</span>`
    }

    var attrs = [{
      text: entity.attributes.confirmed,
      icon: 'mdi:emoticon-neutral-outline',
      color: 'var(--paper-item-icon-active-color)'
    }, {
      text: entity.attributes.recovered,
      icon: 'mdi:emoticon-excited-outline',
      color: 'rgba(94, 228, 101)'
    }, {
      text: entity.attributes.deaths,
      icon: 'mdi:emoticon-dead-outline',
      color: 'rgba(228, 94, 101)'
    }]

    var i;
    var line1 = []
    for (i = 0; i < attrs.length; i++){
      var attr = attrs[i];
      line1.push(getEntityIcon(attr.text, attr.icon, attr.color));
    }

    var attrs = [{
      text: entity.attributes.closest,
      icon: 'mdi:map-marker',
      color: 'var(--paper-item-icon-active-color)'
    }, {
      text: entity.attributes.miles_away,
      icon: 'mdi:map-marker-distance',
      color: 'var(--paper-item-icon-active-color)'
    }]

    var line2 = []
    for (i = 0; i < attrs.length; i++){
      var attr = attrs[i];
      line2.push(getEntityIcon(attr.text, attr.icon, attr.color));
    }

    return line1.join(" ") + "<br>" + line2.join(" ");
  ]]]
aspect_ratio: 4/1
entity: sensor.corona_virus_usa
icon: mdi:biohazard
color: rgb(94, 228, 101)
size: 100%
styles:
  card:
  - border-radius: 15px
  grid:
  - grid-template-areas: '"i n" "i l"'
  - grid-template-columns: 3fr 14fr
  - grid-template-rows: 1fr 2fr
  name:
  - justify-self: start
  - align-self: end
  - padding-left: 10px
  - font-weight: bold
  - font-family: Helvetica 
  - font-size: 16px
  label:
  - align-self: start
  - padding-left: 10px
  - font-size: 12px
  - justify-self: start
  - color: gray
  - text-align: start
  custom_fields:
    updated:
    - position: absolute
    - top: 50%
    - right: 5%
    - transform: translateY(-50%)
    - font-size: 13px
    - justify-self: start
    - text-align: start
custom_fields:
  updated: |
    [[[
      if (entity === undefined)
        return 'Invalid Entity';
  
      let now = new Date();
      let date = new Date(entity.state);
  
      var tdelta = Math.floor((now - date)/1000);
  
      function plural(descriptor, divisor){
        var ret = Math.floor(tdelta/divisor);
        return (ret == 1) ? `${ret} ${descriptor} ago` : `${ret} ${descriptor}s ago`;
      }
  
      var tstring;
      if (tdelta < 60)
        tstring = plural('second', 1);
      else if (tdelta < 60 * 60)
        tstring = plural('minute', 60);
      else if (tdelta < 60 * 60 * 24)
        tstring = plural('hour', 60 * 60);
      else
        tstring = plural('day', 60 * 60 * 24);
      return tstring;
    ]]]
2 Likes

Four sensors! Pff! :face_with_hand_over_mouth:

nice!

trying you code the distance = None

couldn’t spot the error just yet… do you see it? (apparently they dont show provinces in Netherlands, but dont think that’s the issue here, it must be the template?

Does anyone have any issues with the rest sensor not updating? I have am using this code in my setup but it doesn’t appear to be updating on its own. I use the homeassistant.update_entity service on the rest sensor and it updates right away. Is there something that I am missing?

rest sensor updates fine, the template sensor needs the entity_id for the rest sensor.

Is it possible to retrieve data from this site to create Scrape sensor about Coronavirus. Coronavirus integration is not updated.

Tried to make Scrape sensor , but it show only text not digit. Can any bpdy help ?

 - platform: scrape
   resource: https://www.vg.no/spesial/2020/corona-viruset/
   name: Corona1
   select: 'body > div:nth-child(5) > div'
   
   
 - platform: scrape
   resource: https://www.vg.no/spesial/2020/corona-viruset/
   name: Corona2
   select: 'body > div:nth-child(7)'

Screen Shot 2020-03-08 at 02.30.29 Screen Shot 2020-03-08 at 02.30.46

I want to help integrate it for everybody in Italy, since we are in this bad situation right now.
in /config/configuration.yaml put

sensor:

  - platform: rest
    resource: "https://services1.arcgis.com/0MSEUqKaxRlEPj5g/arcgis/rest/services/ncov_cases/FeatureServer/1/query?f=json&where=(Confirmed%20%3E%200%20AND%20Country_Region=%27Italy%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 Italia
    device_class: timestamp
    value_template: "{{ (value_json.features[0].attributes.Last_Update / 1000) | int | timestamp_custom('%Y-%m-%dT%H:%M:%S') }}"
    json_attributes_path: "$.features[0].attributes"
    json_attributes:
      - Confirmed
      - Deaths
      - Recovered

reboot, and install Multiple Entity Row in HACS then add

- type: module
  url: /community_plugin/lovelace-multiple-entity-row/multiple-entity-row.js

in raw lovelace editing.
Now add the card

type: entities
entities:
  - entities:
      - attribute: Confirmed
        name: Confirmed
      - attribute: Deaths
        name: Deaths
      - attribute: Recovered
        name: Recovered
    entity: sensor.corona_virus_italia
    show_state: false
    icon: 'mdi:biohazard'
    name: Corona Virus Italia
    secondary_info: last-changed
    type: 'custom:multiple-entity-row'

And you are good to see the stats.

How can I filter to a specific state (like NY)?

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…