DEV Community

Cover image for Home Assistant dev.to views sensor
Gustav Ehrenborg
Gustav Ehrenborg

Posted on

Home Assistant dev.to views sensor

Home Assistant (HA) is a popular home automation software. Is has tons of built-in features, for example something called RESTful Sensor. It let's you configure an API call and how to process the data to show the data you want on your Home Assistant dashboard.

This guide will show you how to set it up to show your dev.to total views count.

Getting started

Dev.to has an API that can be used to fetch user and article information. However, total post view count is not available in the API, but by adding the view count of each article together will produce this number.

Go to https://dev.to/settings/account and generate a new API key. It is used for authorization, since the view count is not publicly available.

The API endpoint that we're gonna use is documented here. It returns the published articles, including the attribute page_views_count.

curl 'https://dev.to/api/articles/me/published' \
-H 'api_key: <your API key>' \
-H 'per_page: 100'
Enter fullscreen mode Exit fullscreen mode

will return

[
  {
    ...
    "page_views_count": 103,
    ...
  },
  {
    ...
    "page_views_count": 3245,
    ...
  },
  ...
]
Enter fullscreen mode Exit fullscreen mode

Make sure to add the per_page: 100 (or more, up to 1000), since 30 is the default. Once you go over 30 posts, all views will not be counted.

Configure the sensor in HA

In the secrets.yml, add dev_to_api_key: <your API key>. Separating the secrets from the configuration.yml is a good practice. !secret dev_to_api_key will be substituted for the secret value.

In configuration.yml, add the following sensor code.

sensor:
  - platform: rest
    name: Dev to views
    resource: https://dev.to/api/articles/me/published
    headers:
      api_key: !secret dev_to_api_key
      per_page: 100
    scan_interval: 3600
    value_template: "{{ value_json | sum(attribute='page_views_count') }}"
    unit_of_measurement: views
Enter fullscreen mode Exit fullscreen mode

The sensor attributes are pretty self-explanatory. Scan interval takes in the amount of seconds between each call (3600 seconds are one hour). The value template needs some explanation though.

The data received looks like in the snippet above. It is automatically parsed as json and put in the value_json variable. That is piped through the sum method that sums the specific attribute.

Stop and start HA to reload the configuration.

Add to the dashboard in HA

Add a new entity card and select your new Dev.to entity and a suitable icon.

Image description

This will be the result:

Image description

Good luck!

Top comments (1)

Collapse
 
jansche profile image
Jan Schenk (he/him)

Nice explainer article, @gutsav. Definitely want to try this out.

I like the concept of RESTful sensors. It's somewhat blurring the lines between the digital and the material world. I avoided saying real world, but I was tempted. There's no reason why a digital world should be less real.

I'd love to read more about Home Assistant and APIs. I'm into both myself and always looking for inspiration.