Pi-Hole switch

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

1 Like

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?

@raoul.teeuwen here is the right documentation https://home-assistant.io/components/switch.command_line/ for that switch

Thank you. I have the following section in my configuration.yaml:

switch:
  - platform: template
    switches:
      pihole:
        value_template: '{{ states.sensor.pihole_status.state }}'
        turn_on:
          service: script.turn_on
          entity_id: script.pihole_on
        turn_off:
          service: script.turn_on
          entity_id: script.pihole_off

  - platform: command-line
    switches:
      pihole_switch:
        command_on: "curl -X GET 'http://192.168.1.134/admin/api.php?enable&auth=5f81246fe0ccfc7ec7c67c010fb4d28c9510590c0bb778551abbee56936e0948'"
        command_off: "curl -X GET 'http://192.168.1.134/admin/api.php?disable=3600&auth=5f81246fe0ccfc7ec7c67c010fb4d28c9510590c0bb778551abbee56936e0948'"
        command_state: "curl -X GET 'http://192.168.1.134/admin/api.php?status'"
        value_template: "{{ value_json.status == 'enabled' }}"

On compile/check i get “ERROR (MainThread) [homeassistant.loader] Unable to find component switch.command-line”. I don’t know why.

Nice work @rob. Thanks for the idea which I’ve taken as inspiration and expanded upon.

I wanted a little more control over the amount of time that I could disable Pi-Hole for and ended up with the following solution utilising these HA components.

  1. Shell command
  2. Command line Sensor
  3. Scripts
  4. Input Select
  5. Group
  6. Pi-Hole Sensor
  7. Automation

Input Select

Adds a selection menu of configured options to the HA frontend so you can choose the amount of time to disable Pi-Hole for. Options can be added or removed to suit your requirements.

input_select:
  pihole_time:
    name: Disable Time (seconds)
    options:
      - Select Time
      - 10
      - 30
      - 60
      - 300
      - 600
      - 900
      - 1800
      - 3600
    initial: Select Time

Shell Commands

In order to pass a number, in this case the amount of seconds we want to disable Pi-Hole, we’ll need to use the following shell commands.

shell_command:
  pihole_enable: curl -X GET 'http://yourpiholeip/admin/api.php?enable&auth=yourauth'
  pihole_disable: curl -X GET 'http://yourpiholeip/admin/api.php?disable={{ states.input_select.pihole_time.state }}&auth=yourauth'

Script

Allow the shell command to be triggered in HA via a script.

script:
  enable_pihole:
    alias: Enable PiHole
    sequence:
      service: shell_command.pihole_enable

Sensor

Store the current status of the Pi-Hole server in a sensor with a state of either enabled or disabled.

sensor:
  platform: command_line
  name: PiHole Status
  command: curl -X GET 'http://yourpiholeip/admin/api.php?status'
  value_template: >
    {{ value_json.status }}

Automations

The below automation will use the selected value from input_select.pihole_time and send that to the Pi-Hole API via the shell command.

automation:
  alias: Disable Pi-Hole
  trigger:
    platform: state
    entity_id: input_select.pihole_time
  action:
    service: shell_command.pihole_disable

Optional: If you want to reset the value of input_select.pihole_time add the following automation which will reset input select to the initial value Select Time anytime script.enable_pihole is triggered or it detects sensor.pihole_status has changed from disabled to enabled.

alias: Reset PiHole Disable Time
trigger:
  - platform: state
    entity_id: script.enable_pihole
  - platform: state
    entity_id: sensor.pihole_status
    from: 'disabled'
    to: 'enabled'
action:
  service: input_select.select_option
  data:
    entity_id: input_select.pihole_time
    option: Select Time

Group

Finally tie the whole thing together with a group along with the sensors from Pi-Hole.

group:
  pihole:
  name: Pi Hole Status
  entities:
    - sensor.pihole_status
    - script.enable_pihole
    - input_select.pihole_time
    - sensor.pihole_ads_blocked_today
    - sensor.pihole_ads_percentage_blocked_today
    - sensor.pihole_dns_queries_cached
    - sensor.pihole_dns_queries_forwarded
    - sensor.pihole_dns_queries_today
    - sensor.pihole_dns_unique_clients
    - sensor.pihole_dns_unique_domains
    - sensor.pihole_domains_blocked

End Result

25 Likes

anyone knows how to do this having pihole hassio addon?

yes it works:

 pihole_enable: curl -X GET 'http://LOCALIP:PORT/admin/api.php?enable&auth=AUTH'

LOCALIP… Local Ip of your Hassio

PORT: can be set within pi hole addon at --> “admin_port”: XXXX,

AUTH: that was the “tricky” part.

  1. make a backup of your pihole addon (or complete Hassio Snapshot)
  2. Download this backup -> tar file
  3. open the file with 7zip (WINRAR doenst work for me)
  4. find the following file within “pihole”: setupVars.conf
  5. open it… here you will find the line WEBPASSWORD. This is your auth hash for the url
1 Like

Ha! I would have never thought of it. Thanks!

Is your CURL command working from HASSIO?
If I put the below command in a browser it does work, but from within HASS (when I select a time of input_select, the curl is not executed … I guess because of HASSIO?

shell_command:
  pihole_enable: curl -X GET 'http://192.168.1.14:4865/admin/api.php?enable&auth=e37142f6ccc319a42f1cf1c5'
  pihole_disable: curl -X GET 'http://192.168.1.14:4865/admin/api.php?disable={{ states.input_select.pihole_time.state }}&auth=e37142f6f0a890ecccb2f1d6b24cfc319a42f1cf1c5'

I do not use the input select (just fixed 10 minutes disable time).

Try it without the

{{ states.input_select.pihole_time.state }}

and just use a number → 6000 = 10 minutes

pihole_disable: curl -X GET ‘http://192.168.1.14:4865/admin/api.php?disable=**6000**&auth=YOURAUTH

if this works, something is wrong with your inputs / automations.
You can try the shell command via HASSIO → Services → select: shell_command.pihole_disable → call service

Well service is not working, when I put the URL in a browser is working. Not sure where the problem is

hm, this is my setup:

shell_command:
  pihole_enable: curl -X GET 'http://192.168.2.3:4865/admin/api.php?enable&auth=AUTH'
  pihole_disable: curl -X GET 'http://192.168.2.3:4865/admin/api.php?disable=600&auth=AUTH'

and then ->
image

result:
image

can you find anything in the logs?

You use Hassio?

yes:

image

what happens if you hit “call service” ?

Hi, nothing really

I can confirm curl works on hassio with HA 0.95.4

But beware of a space character after auth, it just wont work. I apparently copy-pasted it from the pihole webinterface, so beware.

1 Like

Hello,

I have the code bellow and i’m having an error i cant solve, can you help?
The error is in the automation:
bad indentation of a mapping entry at line 43, column 18:
entity_id: script.enable_pi_hole
^

################################################################################
## SCRIPTS
################################################################################

script:
  enable_pihole:
    alias: Enable PiHole
    sequence:
      service: shell_command.pihole_enable

################################################################################
## SHELL COMMAND
################################################################################

shell_command:
  pihole_enable: curl -X GET 'https://1xx.1xx.1.2xx:4865/admin/api.php?enable&auth='
  pihole_disable: curl -X GET 'https://1xx.1xx.1.2xx:4865/admin/api.php?disable={{ states.input_select.pihole_time.state }}&auth='


################################################################################
#### AUTOMATIONS
################################################################################

automation:
  alias: Disable Pi-Hole
  trigger:
    platform: state
  entity_id: input_select.pi_hole_time
  action:
    service: shell_command.pihole_disable
    alias: Reset PiHole Disable Time
  trigger:
    - platform: state
        entity_id: script.enable_pihole
    - platform: state
        entity_id: sensor.pi_hole_status
        from: 'disabled'
        to: 'enabled'
      action:
        service: input_select.select_option
        data:
          entity_id: input_select.pi_hole_time
          option: Select Time

Hi @joaopequeno,

The indentation in my example is valid YAML syntax and is still working in my setup. In order to assist you better could you please provide more of your config file to see where the error is stemming from?

Hello,

The error is when i save the yaml file and it refers to the first entity of the trigger.

Sorry, just read your post again. The error is due to the additional space you have at the start of entity_id. Please see the correct indentation below.

trigger:
  - platform: state
    entity_id: script.enable_pihole

The state trigger for the pi_hole_status entity also needs to look like the below.

- platform: state
  entity_id: sensor.pi_hole_status
  from: 'disabled'
  to: 'enabled'

Double check my code blocks above to check your config against, ensuring the indentation matches.