PiHole Switch

Pi-hole Switch and Sensor for Home Assistant

This guide will describe how to create a switch for Pi-hole in Home Assistant. It will be usable as any general switch in the Lovelace UI

Configuration File

The following sections need to be added to your Home Assistant configuration file (configuration.yaml)

Pi-hole integration

pi_hole:
  host: '192.168.x.x'
  ssl: false
  verify_ssl: false
  api_key: !secret Pi-hole_key

Adjust to your setup:

  • host: Change it to the IP address of your Pi-hole.
  • ssl/verify_ssl: If you run your Pi-hole with ssl, you can enable it here.
  • api_key: The API key from Pi-hole. Found on your Pi-hole dashboard under Settings -> API/Web interface -> Show API token. Best is to keep secrets and keys into a separate secrets.yaml file.

Pi-hole sensor

The current Pi-hole integration does not reflect wether the Pi-hole is enabled or not. Weā€™ll make a command line sensor that will reflect the status of the Pi-hole. Put this block in the sensor part of your configuration as shown:

sensor:
  - platform: command_line
    name: Pi-hole
    scan_interval: 10
    command: curl -X GET 'http://192.168.x.x/admin/api.php?status'
    value_template: >
      {{ value_json.status }}

Donā€™t forget to alter your IP and change the scan_interval in seconds to your likings

Pi-hole switch

Create the switch that will be usable in the Lovelace frontend, put this in the switch part of your configuration:

switch:
  - platform: template
    switches:
      pi_hole_switch:
        friendly_name: "Pi-hole Enabled"
        value_template: "{{ is_state('sensor.pihole', 'enabled') }}"
        turn_on:
          service: pi_hole.enable
        turn_off:
          service: pi_hole.disable
          data:
            duration: '12:00:00'

Adjust to your setup:

  • friendly_name: The name that will show up in the frontend.
  • value_template: Retrieves the the state of the sensor as enabled/disabled value of the switch. Note that the name from our sensor Pi-hole is translated in the backend to pihole.
  • turn_on: the service pi_hole.enable will be used to enable the Pi-hole when turning on the switch
  • turn_off: the service pi_hole.disable will be used to disable the Pi-hole when turning off the switch. In duration you can set how long the Pi-hole needs to remain disabled. Currently itā€™s set to 12h.

Front-end setup

Use any card that has switch support to reflect the switch in your frontend. With above settings your switch is known as entity switch.pi_hole_switch.

There is an icon available as mdi:pi-hole.

Remark: Depending on your sensor scan_interval it is possible that your switch will flick back after changing. Just let it be, after the max period of scan_interval, the switch will set itself to the real value.

Entity button example:

5 Likes

Another example as I was only allowed to upload one image:

Cool. Iā€™ll have to try this out. Thanks for the write up.

Ehmā€¦ not working for meā€¦ ? Did exactly what you wroteā€¦ I can also see the status of my pihole with the http command (enabled/disbled) but nothing happens on the button and the piā€¦

image
(I have NO API key (and for antother button I can disbale pihole succesfully with that.

Top config is a aonther secntion under ā€œsensor:ā€ but not copy pasted:
image

Where is the name ā€œsensor.piholeā€ coming from?

The name sensor.pihole comes from:

sensor:
  - platform: command_line
    name: Pi-hole
    ...

Which translates to name pihole. Home Assistant removes capitalization and dashes I assume.

I noted the following, but maybe I should make it more prominent:

value_template: Retrieves the the state of the sensor as enabled/disabled value of the switch. Note that the name from our sensor Pi-hole is translated in the backend to pihole .

In your case:
Try changing the value template in the switch from sensor.pihole to sensor.pihole_100 or sensor.pihole_101. To verify the correct name, you can check your entities in ā€œDeveloper Toolsā€ ā†’ ā€œStatesā€.

Also check if you have the service pi_hole available.

And of course, after a config edit, restart HA :slight_smile:

Yes, works:

image

image

Only thing is now (lovelace thing, I want to have both in 1 tileā€¦ the button and the entitiesā€¦

This is my config for both:

Entities Card Configuration

entities:
  - entity: sensor.pihole2_100_ads_blocked_today
  - entity: sensor.pihole1_101_ads_blocked_today
  - entity: sensor.pihole2_100_ads_percentage_blocked_today
  - entity: sensor.pihole1_101_ads_percentage_blocked_today
  - entity: sensor.pihole2_100_dns_queries_today
  - entity: sensor.pihole1_101_dns_queries_today
show_header_toggle: false
title: PiHole Metrics
type: entities

Entity Button Card Configuration

type: entity-button
tap_action:
  action: toggle
hold_action:
  action: more-info
show_icon: true
show_name: true
entity: switch.pi_hole_100_switch
icon: 'mdi:pi-hole'
name: Pi-Hole 100 & 101 Status
icon_height: 40px

You can use the one from the comment I put just below the post
Itā€™s not a button but a switch. Maybe there are some custom cards that can help you out to combine an entity card and button card.

Iā€™m having some issues getting this sensor working with the Pi-Hole community addon.

Given the addonā€™s use of ingress, I canā€™t figure out what URL to give it. Iā€™ve tried variations on the theme below, though nothingā€™s worked yet:

https://localhost/api/hassio_ingress/OWGOF_egjXrkcHOGUwEjVD4lXkzB-P4G7z2Syxxxxxx/admin/api.php?status

Is this even possible?

Thanks for the guide @wlouckx!

According to the guide of the pi-hole addon, the pihole should be available at localhost:4865

Can you try to change the command line to:
command: curl -X GET 'http://localhost:4865/admin/api.php?status'

Normally it should be the same url as you would use to access the pihole admin pages with /api.php?status attached to it.

I use a dedicated pi-hole (not addon to home-assistant) so I canā€™t really research this type of setup.

hi, how can i do if i want to separate pihole 1 and pihole 2 switches?

- platform: template
  switches:
    pi_hole1_switch:
      friendly_name: "Pi-hole1 Enabled"
      value_template: "{{ is_state('sensor.pihole1', 'enabled') }}"
      turn_on:
        service: pi_hole1.enable
      turn_off:
        service: pi_hole1.disable
        data:
          duration: '12:00:00'

- platform: template
  switches:
    pi_hole2_switch:
      friendly_name: "Pi-hole2 Enabled"
      value_template: "{{ is_state('sensor.pihole2', 'enabled') }}"
      turn_on:
        service: pi_hole2.enable
      turn_off:
        service: pi_hole2.disable
        data:
          duration: '12:00:00'

where is the error(s)? that there is only pi_hole enable or disable (on service card)

Make sure that you also have double hosts in your integration:

pi_hole:
  - host: 'ip host 1'
    api_key: 'api key host 1'
    name: 'pi_hole1'
  - host: 'ip host 2'
    api_key: 'api key host 2` 
    name: 'pi_hole2'

and 2 sensors:

sensor:
  - platform: command_line
    name: Pi-hole1
    scan_interval: 10
    command: curl -X GET 'http://192.168.x.x/admin/api.php?status'
    value_template: >
      {{ value_json.status }}
  - platform: command_line
    name: Pi-hole2
    scan_interval: 10
    command: curl -X GET 'http://192.168.x.x/admin/api.php?status'
    value_template: >
      {{ value_json.status }}

Replace the IP address with the IPā€™s of your piholes

Also reformat the switches configuration to:

- platform: template
  switches:
    pi_hole1_switch:
      friendly_name: "Pi-hole1 Enabled"
      value_template: "{{ is_state('sensor.pihole1', 'enabled') }}"
      turn_on:
        service: pi_hole1.enable
      turn_off:
        service: pi_hole1.disable
        data:
          duration: '12:00:00'
    pi_hole2_switch:
      friendly_name: "Pi-hole2 Enabled"
      value_template: "{{ is_state('sensor.pihole2', 'enabled') }}"
      turn_on:
        service: pi_hole2.enable
      turn_off:
        service: pi_hole2.disable
        data:
          duration: '12:00:00'

do it, homeassistant create only pi_hole.enable and pi_hole.disable service, not pi_hole1.enable and pihole_2 enable ecc ecc

i read that on service card:

name
[Optional] When multiple Pi-holes are configured, the name of the one to enable. If omitted, all configured Pi-holes will be enabled.	Pi-Hole

i donā€™t understand :frowning:

ok i found a solution (by official docs). For who has 2 piholes:

- platform: template
  switches:
    pi_hole1_switch:
      friendly_name: "Pi-hole1 Enabled"
      value_template: "{{ is_state('sensor.pi_hole1', 'enabled') }}"
      turn_on:
        service: pi_hole.enable
        data:
          name: pi_hole1
      turn_off:
        service: pi_hole.disable
        data:
          name: pi_hole1
          duration: '12:00:00'


    pi_hole2_switch:
      friendly_name: "Pi-hole2 Enabled"
      value_template: "{{ is_state('sensor.pi_hole2', 'enabled') }}"
      turn_on:
        service: pi_hole.enable
        data:
          name: pi_hole2
      turn_off:
        service: pi_hole.disable
        data:
          name: pi_hole2
          duration: '12:00:00'

The problem of template is an error in this line:
value_template: ā€œ{{ is_state(ā€˜sensor.piholeā€™, ā€˜enabledā€™) }}ā€
you need change for this:
value_template: ā€œ{{ is_state(ā€˜sensor.pi_holeā€™, ā€˜enabledā€™) }}ā€

I donā€™t think that will workā€¦ The sensor is clearly listed as sensor.pihole in the entities tab (and it works likewise on my setup). But of course I might have overlooked something (home assistant changes all the time) so please elaborate why to use the underscore :slight_smile:

Sorry, edited.

Thank you very much for this!

Is it possible to have something that can be used in an automation to switch between pi_hole.enable and pi_hole.disable?
I want to use a flic button to switch Pi-Hole on/off and I want to use only eg. short press for on and then short press for off.
If I am using eg. a device light bulb I can choose the action switch between on/off (I donā€™t know the english wording, in German it is ā€œSchalte ā€¦ umā€). But there is no device Pi-Hole.

Maybe not the correct topic,
But i was not able to disable pi-hole from home assistant out.
Turns out the API key was missing, (but re-configuring the integration does not ask for a API key)
Removed the integration, restarted Home assistant, then re-added the integration, now it asked for a API key in the second page/tab.
So now i can disable pi-hole from home assistant out.

Anyone played with nice lovelace interface for pihole willing to share? Thanks in advance

Great integration! Solves a key need to make it easy to disable/enable a pi-hole (or multiple, even better).

I got it working by adding the provided snippet to my configuration.yaml, but only after trying the web GUI integration add screen; this doesnā€™t seem to have a space for the API token?
Is this still being worked on?

Also, it doesnā€™t seem to refresh the status of the pi-hole. If I call pi_hole.disable with a time of 00:03 (for 3 minutes) the pi-hole behaves correctly - it disables, then 3 minutes later it enables. The switch state in HA initially updates to ā€˜offā€™, but doesnā€™t refresh back to ā€˜onā€™ in a timely fashion (takes a few minutes after that at least). Is there any way to improve the polling?