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.
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'"
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?
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.
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.
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.
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!
I had the state running the enable 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>'"
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?