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 ?).
Salty
(John)
March 11, 2020, 3:10pm
44
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:
1 Like
Salty
(John)
March 11, 2020, 3:20pm
45
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.
ThaNerd
(Turbo Tronix)
March 11, 2020, 5:38pm
46
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…
petro
(Petro)
March 11, 2020, 5:40pm
47
All you have to do is copy and paste the whole setup… no altering. Why are you trying to modify it?
ThaNerd
(Turbo Tronix)
March 11, 2020, 5:41pm
48
Cuz I want both country and a specific state using only one rest. I can do it with 2 rest…
petro
(Petro)
March 11, 2020, 5:44pm
49
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.
ThaNerd
(Turbo Tronix)
March 11, 2020, 5:45pm
50
but under attributes of your rest sensor I see my state…can’t I just point to these attributes directly in lovelace?
petro
(Petro)
March 11, 2020, 5:49pm
51
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.
ThaNerd
(Turbo Tronix)
March 11, 2020, 5:50pm
52
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.
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
ThaNerd
(Turbo Tronix)
March 11, 2020, 9:22pm
55
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.
ThaNerd
(Turbo Tronix)
March 12, 2020, 1:53am
57
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 .
petro
(Petro)
March 12, 2020, 12:01pm
59
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
ThaNerd
(Turbo Tronix)
March 13, 2020, 11:22pm
60
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!
petro
(Petro)
March 14, 2020, 9:45am
62
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.