Pi-Hole switch

I’m using Pi-Hole for ad-blocking in our LAN. With the Pi-Hole component I added the sensors in Home Assistant. But my wife sometimes want to disable the ad-blocking features (i.e. for Google shopping). So I have to open the Pi-Hole admin page to (temporary) disable ad-blocking. So I came up with a solution to enable/disable Pi-Hole within Home Assistant.

The Pi-Hole API listed the needed commands for a command_line switch.

To add a Pi-Hole switch to Home Assistant add the following to your configuration:

# Pi-Hole component
sensor:
  - platform: pi_hole
    host: 10.254.10.25

# Pi-Hole switch
switch:
  - platform: command_line
    switches:
      pihole_switch:
        command_on: "curl -X GET 'http://10.254.10.25/admin/api.php?enable&auth='"
        command_off: "curl -X GET 'http://10.254.10.25/admin/api.php?disable=3600&auth='"
        command_state: "curl -X GET 'http://10.254.10.25/admin/api.php?status'"
        value_template: "{{ value_json.status == 'enabled' }}"

When the switch is turned off it will disable Pi-Hole ad-blocking for 3600 seconds (1 hour). After 1 hour ad-blocking is automatically enabled.

Notes:

  • In the above example my Pi-Hole instance is running at ip 10.254.10.25, you need to replace that with your own Pi-Hole ip-address.
  • I dont use a password to access the Pi-Hole admin page and API. If you are using a password you need to include the authentication token to the api calls (see WEBPASSWORD in /etc/pihole/setupVars.conf. For example:
    command_on: "curl -X GET 'http://10.254.10.25/admin/api.php?enable&auth=183c1b634da0078fcf5b0af84bdcbb3e817708c3f22b329be84165f4bad1ae58'"
28 Likes

I didn’t know Pi-Hole could be disabled this easily. Don’t you have DNS cache issue? I mean, if you try to open a site, notice it’s blocked, then disable Pi-Hole temporarily, your browser probably cached the pi-holed IP address already, didn’t it?

Yes you have to clear the local DNS cache after you disable pi-hole (if it was blocked before, otherwise the record wont be in the cache).

Great work! Added to my todo list. :wink:

Okay. That’s still pretty useful, thanks! :slight_smile:

Hey,

Thanks for this. I’ve popped it in but can’t get it to work.

If I paste the http in to a browser I can successfully enable, disable and see the status just fine. However in HASS is does nothing.

    switches:
      pihole_switch:
        command_on: "curl -X GET 'http://192.168.1.234/admin/api.php?enable&auth=WEBBPASS"
        command_off: "curl -X GET 'http://192.168.1.234/admin/api.php?disable=3600&auth=WEBBPASS'"
        command_state: "curl -X GET 'http://192.168.1.234/admin/api.php?status'"
        value_template: "{{ value_json.status == 'enabled' }}"```

Am I missing something obvious?

Probably stupid but you never know: do you have curl installed?

I’m pretty sure, I have another switch using curl for my foscam camera which works fine. No errors in the log either.

I’ve added it into my YAML package if you want to see actual yaml code.

1 Like

Try the curl commando from the shell to test them. Also I haven’t tried the commands with an authentication token so maybe it’s worth a try to disable the password in the pi-hole configuration.

Weird, as soon as I removed the password from pihole it started working.

my guess is you used the password you type, which is incorrect. I believe pihole hashes it and stores it in the file mentioned in the OP. If you dig that password/key out and use it instead it should work (did for me).

I followed the original instructions and pulled the password out .conf file. Also when I pasted it in to a browser with the password and authentication enabled on the pihole end it worked.

1 Like

I am using a password on mine and I couldn’t get the status to work without also providing the auth param in the URL. You can also get the key you need from the pi.Hole admin interface.

It will generate a QR code you can scan to get the token. I just used my phone to grab the value of the token then.

switch:
  - platform: command_line
    switches:
      pihole_switch:
        command_on: "curl -X GET 'http://10.254.10.25/admin/api.php?enable&auth='"
        command_off: "curl -X GET 'http://10.254.10.25/admin/api.php?disable=3600&auth='"
        command_state: "curl -X GET 'http://10.254.10.25/admin/api.php?status&auth='"
        value_template: "{{ value_json.status == 'enabled' }}"
3 Likes

Brilliant. Seems to wok just fine. Thanks.

I think I must be missing something. I cannot get it to turn off or on. Th switch shows in the front end as on, but if I switch it off, it just goes back on after a couple of seconds. am I missing something obvious?

Here is the config I use:

  pihole_switch:
    command_on: "/usr/bin/curl -X GET 'http://x.x.x.x/admin/api.php?enable&auth=<myAPIKey>'"
    command_off: "/usr/bin/curl -X GET 'http://x.x.x.x/admin/api.php?disable=1800&auth=<myAPIKey>'"
    command_state: "/usr/bin/curl -X GET 'http://x.x.x.x/admin/api.php?enable&auth=<myAPIKey>'"
    value_template: "{{ value_json.status == 'enabled' }}"

Running the curl commands from the HA box as the HA user works fine. I have another Command Line switch that uses curl and it needs the /usr/bin/bash to run as I run HA in VirtualEnv. This has me baffled!

Arrh. I am a dozo.

I had the state running the enable :slight_smile: Needed to change the word ‘enable’ to 'status"

command_state: "/usr/bin/curl -X GET 'http://x.x.x.x/admin/api.php?enable&auth=<myAPIKey>'"
command_state: "/usr/bin/curl -X GET 'http://x.x.x.x/admin/api.php?status&auth=<myAPIKey>'"

then what, where you put the token?

HI there ,

you put it after the auth=

http://x.x.x.x/admin/api.php?enable&auth=PUT_KEY_HERE

I’m having trouble getting the state of pi-hole to show (and being accurate). I’ve tried the code from this thread, but get “Invalid config for [switch.template]: [command_on] is an invalid option for [switch.template]”

When i have a look at https://home-assistant.io/components/switch.template/ i don’t see the command_on-option. Has it been removed? I see a “value_template” option, but am currently most of the time struggling with indention, parameter syntax etc of HA :-(. Can anybody confirm that in the latest HA the above example code to show status and switch on/off pi-hole doesn’t work anymore, and maybe share sample code that does work?