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!