XKCD in Home Assistant

I like to do some funny things from time to time, and I learned that adding XKCD to my home assistant dashboard is pretty simple. So what am I going to do, of course I’m going to add the current days XKCD to my home assistant dashboard!

To do this, we’ll be using the rest platform in home assistant. This can call a website’s API and parse the results. We will call the XKCD API using the YAML below to produce a resultant sensor we can get images from.

rest:
- scan_interval: 43200 #12h
resource: "https://xkcd.com/info.0.json"
sensor:
- name: "xkcd"
value_template: "{{ value_json.safe_title }}"
json_attributes:
- "img"
- "alt"
- "num"
XKCD rest platform entry in VS Code

To get the images out of our sensor, we will add a camera integration. This will use the below code for the URL in the integration which will tell it to pull from the local home assistant sensor rather than an external device.

{{ state_attr('sensor.xkcd', 'img') }}
Creating the XKCD ‘camera’

Now that our camera is created and ready to go. We can add it to a dashboard by searching for the xkcd entity. Select the camera and add it in to your favorite dashboard.

Adding the XKCD ‘camera’ entity

Now we can read XKCD and get our current weather all in one easy pane.

XKCD on the dashboard

Resources