REST api errors and avoiding storing 'bad' history

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
      User-Agent: Home Assistant
      Content-Type: application/json
  device_class: timestamp
  timeout: 60
  json_attributes_path: "$.data[0]"
  resource_template: '{"date":"date","new":"newCasesByPublishDate","total":"cumCasesByPublishDate"}'
  scan_interval: 7200
  value_template: '{{ as_timestamp([0].date) | timestamp_custom(''%Y-%m-%d'') }}'
      - new
      - total
- platform: template
      friendly_name: "New Covid Cases Peterborough"
      icon_template: mdi:virus
      value_template: "{{ '{0:,.0f}'.format(state_attr('sensor.coronaPeterborough', 'new') | int) }}"
      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 :slight_smile: