February 8, 2018

PiHole and OpenHAB

Recently I installed pihole on my network because I was sick of ads, and wanted an added layer of security on my network against nasty malware and the like

There are plenty of install guides out there for pihole (see official website) so thats not what I am covering today. I installed this on a raspberry pi 1 (Model B) and it handles everything just fine!

Becauase I am a fan of home automation, I wanted all the data that is available in the piHole web interface, avaialable to me at my openHAB interface. You know, so everything is all in the one place. Thankfully this was super easy because piHole has a simple API that you can extract this data from. This also allows you to temporailty disable piHole. Which is useful when your wife wants to clicks ads anyway, or if you know piHole is causing mischef with a particular website


  • We need to first make sure that the webpassword is set in the piHole config. This will be used to authenticate against the API (I beleive you can turn this off if you desire)

$ cat /etc/pihole/setupVars.conf

Look for the line WEBPASSWORD

If its not set, then set it by adding a line in the config file, making up a random string


Now lets make some items in openhab to capture the data we want, and an entry in the sitemap to display it - $ sudo vim /etc/openhab2/services/http.cfg Set up a regular http call as a service, so it will go ahead a poll the API every so often. This is also useful because you can use this service against as many items as you want, reducing the amount of API calls you do to only as many as this service does.


  • $ sudo vim /etc/openhab2/items/pihole.items Add some items for your sitemaps to use
Number piholePercent    "Percent blocked [%.1f %%]" <pihole>    { http="<[pihole:303000:JSONPATH($.ads_percentage_today)]"}
Number piholeBlockedAds "Blocked ads [%d]" <pihole>     (pihole)        {http="<[pihole:303000:JSONPATH($.ads_blocked_today)]"}
Number  piholeQueries   "DNS Queries [%d]" <pihole>     (pihole)        {http="<[pihole:303000:JSONPATH($.dns_queries_today)]"}
Switch piholeDisable "Toggle Pi Hole" (pihole) {http=">[OFF:GET:http://pi.hole/admin/api.php?disable=600&auth=<WEBPASSWORD>] >[ON:GET:http://pi.hole/admin/api.php?enable&auth=<WEBPASSWORD>]"}

The first 3 items use the item set in the http.cfg. you can see is uses JSONPATH to do some transformation to only get the data we want for the particular item. In the case of piholePercent we are grabbing the ads_percentage_today value out of the JSON output from the API. I’ve also created a switch to disable and enable piHole as needed. This API function requires you to authenticate yourself so we needed to add that in there too, this is where the WEBPASSWORD value in the piHole setting comes into play

  • Sitemap. Add the items to your sitemap as you see fit (Here is my example from my sitemap)
    Frame label="Pi Hole"{
                    Text item=piholeQueries
                    Text item=piholeBlockedAds
                    Text item=piholePercent
                    Switch item=piholeDisable
    pihole sitemap

Optional If you’d like to add an icon for the items in the sitemap further, download the graphics from piHole (png and svg) and upload them into your openhab install at /etc/openhab2/icons/classic. Then set the icon in the item using <pihole> (Refer to the openhab item config above). Now you will get a nice icon for the item too

More info on the API can be found here