Help with Corona Scrape Sensor

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.

It might be that custom card of yours. The state (in the devtools -> states page) will always be that jibberish, but when you put it in lovelace it should have the correct time.

1 Like

As always, you’re right. :+1:
If i add as a normal entity, it works.
20200302_17:54:40_001

That’s a bug for the custom card!

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)?