PiHole 5 (and 6) enable/disable groups - block internet

Hi,
if you just want to disable all of Pi-Hole you would be better off using the official integration that will give you a service to disable Pi-Hole for a duration of your choice.

The topic here is to enable/disable specific groups in pi-hole that are not exposed via an official API (yet).

Yeah, I tried that, including adding the apikey generated from within the pi-hole instance, but the lovelace card it generates doesn’t really toggle too well. It turns on, but the toggle off doesn’t really work.

@datamonkey
Short note to thank you for sharing this. It’s exactly what I was looking for. I’ve set up as described and it just works.
As for the command line switch I’ve taken a simpler, however somewhat less secure approach. However I can live with it.

I run HA in docker and I’ve given HA access to the host docker engine, adding below to volumes:

    - /var/run/docker.sock:/var/run/docker.sock
    - /usr/bin/docker:/usr/bin/docker

my command is then like this:

    block_youtube: 
      friendly_name: Block Youtube
      command_on: docker exec pihole bash /etc/pihole/setGroupStatus.sh 1 enable
      command_off: docker exec pihole bash /etc/pihole/setGroupStatus.sh 1 disable
      command_state: docker exec pihole bash /etc/pihole/setGroupStatus.sh 1 status
      value_template: '{{ value == "1" }}'

Thank you

1 Like

emphasized textThank you so much for this. It’s done the job well.

I have Home Assistant now enabling/disabling Pi-Hole groups. Both are in separate docker containers.

The only issue I had is that the Lovelace switch cards didn’t appear to stay at the on position, but cycled to off, then on every 1 min. 1 min on, one min off and repeat. The actual group status in Pi-Hole was unaffected, it was just an annoyance in my dashboard.

Adding ā€˜scan_interval: 300’ to my configuration.yaml in an attempt to resolve this has only lengthened the cycle to 5 minutes. I can’t understand why the status is flipping at every scan.
Help please!

  - platform: command_line
    scan_interval: 300
    switches:
      pihole_videos:
      ...

This is the shell script I’m using.

#!/bin/bash
# takes the group name and enable disable and status as input and
# updates the pihole gravity database accordingly
# then restarts the pihole dns and lists
# Data Monkey January 2021
case "$2" in
   "enable")
       sqlite3 /etc/pihole/gravity.db "update 'group' set 'enabled'=1 where name='$1'";
       # refresh PiHole
       /usr/local/bin/pihole restartdns reload-lists >/dev/null
       ;;
   "disable")
       sqlite3 /etc/pihole/gravity.db "update 'group' set 'enabled'=0 where name='$1'";
       # refresh PiHole
       /usr/local/bin/pihole restartdns reload-lists >/dev/null
       ;;
   "status")
       stat=`sqlite3 /etc/pihole/gravity.db " select enabled from 'group' where name = '$1';"`
       echo $stat
       exit 0
       ;;
   *)
   echo $"Usage: $0 {GroupName enable|disable|status}"
   exit 1
esac

UPDATE:

I’ve now sort of resolved the issue using the revisions offered above by the-bort-the. Thanks.

It address the strange unwanted toggle to off in HA, but I have lost the proper status checking unfortunately. So if I toggle a group on the Pi-Hole front end HA will not update.
But I’ll try to live with that for now.

FURTHER UPDATE:

I find that updating Home Assistant breaks the connection to Pi-hole.
Running the following command within the Home Assistant container (via Portainer) fixes it for me.

ssh -i /config/.ssh/pihole USERNAME@SERVER IP ADDRESS

Hope this helps someone else.

I’ve just spent ages struggling with this. Just switch to AdGuard. Their getting started guide documents the REST API.

Thank you for this! It has been the inspiration to build a small script that exposes PiHole groups via MQTT to HA with autoconf!
I can finally automate the site-blocking ā€œparental control styleā€ from Home Assistant!
https://github.com/Andrec83/pihole-ha-mqtt-service/tree/main

1 Like

This is a great idea.
But I am running pihole in docker, will need to see if I can make your script persistent in the docker container, that would be great!

I just checked the new PiHole v6 API and we might get an official API to enable/disable groups. Here is hoping …

https://ftl.pi-hole.net/development-v6/docs/#put-/groups/-name-

I see, thank you for this.

the install script for docker integration was next in line, I added yesterday a couple of features that were missing (and did some code clean-up, although still very ugly) and was planning to work on a set-up script in the next few days. I will hold on for now since the API route is of course preferred.

This is exactly what I was looking for. I use domains and groups to define what sites are accessible and then set my child’s device to the appropriate group to set the access.

Unfortunately, my HA server is accessible from the Internet via Nabu Casa cloud, so enabling ssh to root isn’t an option.

I looked at the MQTT approach but that doesn’t seem feasible for my situation.

I guess I just have to wait for an API.,…

PiHole 6Beta is out. I have been using it for a few months.
It has an API but I have not tried to implement this yet.

I managed to transfer my script to the PiHole6 (beta) API version.
No more ssh and root privileges required!

Hello. I am using this script on pihole v5 atm. Can you provide a guide on how you upgraded to v6 and ported all your v5 settings to 6? I am assuming you are running pihole in a container environment. Thank you.

hI, I believe there is not config change on pihole required.
You need to change the command line switch as described in the code.


# command_line:
#   - switch:
#       name: PiHoleSwitchName
#       command_on: ./piholeGroupStatus.sh http://pi.hole:80/api/ API_PWD GroupName enable
#       command_off: ./piholeGroupStatus.sh http://pi.hole:80/api/ API_PWD GroupName disable
#       command_state: ./piholeGroupStatus.sh http://pi.hole:80/api/ API_PWD GroupName status

that should be all that is required.

Hello. ok. I lost at how to obtain the api on v6. I can’t seem to find it in the gui.

Update: ok. I figured it Was multiple of missed config on my end. The api isn’t really an api but an app password you need to generate. This is my command line switch that is working.

How are you hiding the app password using the secret file?

command_line:
  - switch:
      name: iPad
      command_on: bash /config/scripts/sh/piholeGroupStatus.sh http://192.168.1.55:8010/api APP_PASS Shion-NoAccess enable
      command_off: bash /config/scripts/sh/piholeGroupStatus.sh http://192.168.1.55:8010/api APP_PASS Shion-NoAccess disable
      command_state: bash /config/scripts/sh/piholeGroupStatus.sh http://192.168.1.55:8010/api APP_PASS Shion-NoAccess status

Hi, I did not hide my password. Yes I know, bad practice, I was just using it internally …

you might be able to do it by putting the password at the end of the command and using the secret file.

Change the order of the command and the variables in the script

# command_line:
#   - switch:
#       name: PiHoleSwitchName
#       command_on: ./piholeGroupStatus.sh http://pi.hole:80/api GroupName enable API_PWD
#       command_off: ./piholeGroupStatus.sh http://pi.hole:80/api GroupName disable API_PWD
#       command_state: ./piholeGroupStatus.sh http://pi.hole:80/api GroupName status API_PWD



PiHoleAPI=$1 
GroupName=$2
Action=$3
PiHolePWD=$4

I have not tried this.

I try to add this as a script to Home Assistant but get an error:

Many thanks for this script Roland. Way beyond my skills.

I’ve just migrated to PiHole V6.0.5 and this works perfectly.

1 Like

Hi Roland,

Your script continues to work well, but seems to be triggering rate limiting warnings in the FTL.log. 6x warnings every 30 seconds >

Not sure how much of an issue it is, but is there any way to reduce this please?

I have a total of 9 command line switches currently set up in the config.yaml.
If I comment these out of the file the log warnings stop.

Thanks

One of the devs over at the Pi-Hole forums had a recommendation, but maybe this is beyound the scope of what’s possible within the script.

Hi,
the script is supposed to close the session. Can you please check the Pihole UI to see if there are lots of open sessions hanging around.
pihole-ip/admin/settings/api (advanced Settings)

you could also try to add a scan_interval to the configuration and increase the interval. That will delay HA from updating the entity in case the setting gets changed in PiHole directly. If you only use HA to change the switch it should not have much of an impact. (I have not tried that yet)