Adding a Pi-Hole switch in Home Assistant7 min read
I’ve been using Pi-Hole for 6 months on my Raspberry PI 4, and I’m loving it. What I like most is that I no longer have to deal with ads on devices where I can’t install AdBlock. For example, my TV would play YouTube ads very often, but now I barely see them. There are some cases where it might be a bit irritating though. One of them is when I go to some website and it doesn’t work, and I have to check if it’s Pi-Hole that’s blocking it or if the website is really down. In most cases, the website actually works and it’s just blacklisted. If I use it frequently then I’ll whitelist it, otherwise, I just disable Pi-Hole for a few minutes. To do this, I have to open Home Assistant, navigate to Pi-Hole, and then disable it. This takes only 10 seconds, but when it happens every day it becomes annoying, so I wanted to make it easier because whitelisting everything is clearly not an option.
Option 1: Adding a switch in Home Assistant
The idea is simple, Pi-Hole can be enabled/disabled if you make a GET request to a certain endpoint. We can do this by creating a shell_command that uses curl for making that request. Here’s how it looks like:
This might work for you if you haven’t changed the port and Pi-Hole is on the same machine as your Home Assistant instance. If not, then use the IP for your Pi-Hole and also remember to check the port, it might be 80 instead of 4865 or something else. The next thing you’ll notice on the off command is that we send a value of 60 in the request. That’s the time in seconds that we want to switch off our Pi-Hole. If you won’t specify a value, it will be disabled until you switch it back on.
One other thing you have to be aware of is authentication. You noticed at the end of the URL there’s the query param “&auth=”. In my case, I don’t need it because Home Assistant handles authentication for me. If this is not the case for you, then you should use the API Key provided by Pi-Hole. Just navigate to Pi-Hole settings page, then API/Web Interface, and click on Show API Token. If you’re redirected to another page that only has the text “No password set”, then you’ll have to grab it from the disk. This means you’ll have to access the files on your Pi-Hole and find “setupVars.conf”. For me, this file was located in “/share/pihole/etc-pihole”. In this file, look for a key named WEBPASSWORD and copy the value, that’s what you’ll use in the request after “auth=”.
I spent a lot of time with this because authentication wasn’t working for me in the beginning. I tried different combinations with the WEBPASSWORD and different ports without any luck. In the end, I found out that instead of using the IP of my raspberry I should’ve used localhost with no authentication. I hope you won’t have to waste much time with this.
To summarize, this should give you a basic switch that you can put wherever you want in your Home Assistant dashboard.
Disabling for a specified time
This wasn’t enough for me because in some cases I might want to disable it for 5 minutes or an hour. To solve this, I found an excellent guide on how to create an input that allowed me to select the time I want to disable. Here’s what I got based on that guide:
The only change that I did to my switch was to use minutes instead of seconds, buy you have it all in the gist below:
The last step is to display this in your dashboard, so go to your lovelace-ui.yaml file(or wherever you want to put this) and paste this snippet:
Here’s a small demo of the card:
Again, this is useful, but can we improve this?
Option 2: Disabling Pi-Hole with Alexa
Instead of opening the app, it would be easier to ask Alexa to disable Pi-Hole. For this, we just need a script that disables Pi-Hole for a certain amount of time and an Alexa routine. The script is very simple, it just uses the one we created in the beginning and turns it off, thus disabling Pi-Hole for 5 minutes:
Then open your Alexa app and create a new routine, but before you do this, make sure you sync your entities from the Alexa integration. After you set the name, you should set the voice command, in my case, it’s “Alexa, turn off adblock”. I tried “Alexa, turn off Pi Hole” but Alexa always understood “piehole” or “pie home” and so on, so this one works better for me. In the next step you have to add an action, and this is where we’ll turn on our script. Just click on “Add action” -> “Smart Home” -> “Control scene” -> Tap on “Disable PiHole” -> Click on “Add”. After that, I also added a response from Alexa so I can have confirmation that she understood the command. I just made her say “Ok, disabling Pi Hole” on the device I interacted with, either my phone or an Echo Dot. In the end, your routine should look like this:
That’s it! Now you can either control Pi-Hole from your Home Assistant dashboard, or you can use Alexa to turn it off temporarily. Let me know if you need help setting this up, I’d be happy to help.
PS: If you want to see it in action, check the video below: