Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 calledRESTful 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 tohttps://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 documentedhere. It returns the published articles, including the attributepage_views_count.

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

will return

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

Make sure to add theper_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, adddev_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:restname:Dev to viewsresource:https://dev.to/api/articles/me/publishedheaders:api_key:!secretdev_to_api_keyper_page:100scan_interval:3600value_template:"{{value_json|sum(attribute='page_views_count')}}"unit_of_measurement:views
Enter fullscreen modeExit 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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
jansche profile image
Jan Schenk (he/him)
Hi! I'm Jan. I'm an Engineering Community Manager. My <3 is with dev communities, diversity & inclusion and making the Tech Industry a safer place for everyone. Pronouns: he/they
  • Location
    Munich, Germany
  • Education
    College Drop-Out since 2002
  • Pronouns
    He / They
  • Work
    Engineering Community Manager @ E.ON
  • Joined

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.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Sweden
  • Education
    Computer Science and Engineering, MSc
  • Work
    Freelance backend developer
  • Joined

More fromGustav Ehrenborg

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp