Improving Covid Data Integration in Home Assistant

Hey everyone, I thought I’d share my journey in trying to get Covid statistics into Home Assistant. It’s been quite the learning experience! :face_with_monocle:

I’ve been trying to pull data from the UK government’s Covid API and integrate it into my HA setup. The goal was to have sensors for new cases and total cases in my area. I managed to get it working initially, but lately, I’ve been running into some issues with bad data and timeouts. It’s been a bit frustrating, but I’ve learned a lot along the way! :rocket:

Here’s what I’ve done so far:

  1. REST Sensor Setup: I set up a REST sensor to pull the data from the API. I included headers and a timeout to handle any delays.
  2. Template Sensors: I created template sensors to parse the JSON data and display the numbers in a user-friendly way.
  3. Error Handling: I’ve been experimenting with retry logic to avoid setting my numbers to zero when there’s a bad request. It’s still a work in progress, but I’m hopeful!

I’d love to hear if anyone else has tackled similar challenges or has tips on how to make this more robust. Maybe there’s a better way to handle the data parsing or improve the reliability of the API calls? :thinking:

Here’s a snippet of my current setup for reference:
yaml

  • platform: rest
    name: CovidPeterborough
    headers:
    User-Agent: Home Assistant
    Content-Type: application/json
    device_class: timestamp
    timeout: 60
    json_attributes_path: “$…data”
    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.covidPeterborough’, ‘new’) | int) }}”
    covid_peterborough_total:
    friendly_name: “Total Covid Cases Peterborough”
    icon_template: mdi:virus-outline
    value_template: “{{ ‘{0:,.0f}’.format(state_attr(‘sensor.covidPeterborough’, ‘total’) | int) }}”

If anyone has suggestions on how to improve this setup, I’d be super grateful! Maybe there’s a way to implement better error handling or cache the last good state more effectively. Let me know your thoughts! :handshake:

Cheers, [Your Name]