I’ve been trying to get working Covid stats from the UK government website and had ‘some’ success, but lately (either due to issues with their website or Home Assistant) I’ve been getting a lot of bad data on the sensor (either due to timeouts/dropped request/etc). Ideally I’d like it so that at worst case, these bad requests don’t default set my numbers to 0 and keep the last good state, or ideally retry so that I can have accurate data.
My REST sensor and the template to split the information into New cases and Total cases are below:
- platform: rest
name: CoronaPeterborough
headers:
User-Agent: Home Assistant
Content-Type: application/json
device_class: timestamp
timeout: 60
json_attributes_path: "$.data[0]"
resource_template: 'https://api.coronavirus.data.gov.uk/v1/data?filters=areaName=peterborough&structure={"date":"date","new":"newCasesByPublishDate","total":"cumCasesByPublishDate"}'
scan_interval: 7200
value_template: '{{ as_timestamp(value_json.data[0].date) | timestamp_custom(''%Y-%m-%d'') }}'
json_attributes:
- new
- total
- platform: template
sensors:
covid_peterborough_new:
friendly_name: "New Covid Cases Peterborough"
icon_template: mdi:virus
value_template: "{{ '{0:,.0f}'.format(state_attr('sensor.coronaPeterborough', 'new') | int) }}"
covid_peterborough_total:
friendly_name: "Total Covid Cases Peterborough"
icon_template: mdi:virus-outline
value_template: "{{ '{0:,.0f}'.format(state_attr('sensor.coronaPeterborough', 'total') | int) }}"
Any help on how to make this better is very much appreciated