Automation to turn on and off switches on Adguard Home

Hi,
I am using Adguard home to block ads and manunally block access to some Social Networks like youtube, facebook…via quick built-in filters on Adguard Home.
However, to make it more convenient, can somebody have solutions to make automation to switch these switches on and off according to specific time???


Thank you

Did you work this out in the end?

If so how?

Yes, I found solution from Gieljnssns thread

Hi the URL list reported in this thread is outdated… and moreover to block a social (for example facebook) ther are a lot of IPs but AdGuard have the possibility to block a service instead a set of URLs.
Is it possible to toggle the service switches with an automation?

I have found here something that for me is not working.

https://www.diyenjoying.com/2022/12/30/manage-adguard-blocked-services-with-home-assistant/

for example


curl -X GET -m 10000 -H 'Authorization: Basic bG9naW46cGFzc3dvcmQ=' -H 'Content-Type: application/json' http://<my homeassistant IP>:53/control/blocked_services/list

curl: (52) Empty reply from server

How the adguard home API can be used?

I have found a solution. I will post here hope could helps.

Here
https://lirantal.com/blog/block-lan-clients-from-accessing-youtube-and-other-media-with/
I have understood that for using AdGuardHome API, TSL must be disabled and the port 3000 must be enabled (is not active by default)

Now I have followed this post:
https://www.diyenjoying.com/2022/12/30/manage-adguard-blocked-services-with-home-assistant/

but I have made some changes to the script since the API used are deprecated as reported here:

https://github.com/AdguardTeam/AdGuardHome/blob/5c2ecfaa41d6be849c69fb4a4940526250890034/openapi/openapi.yaml

So I have changed the script as follow

#!/bin/bash

#Variables
#old deprecated API (POST)  -d '[ "tiktok" ]'
#AdGuard_URL='http://192.168.1.67:3000/control/blocked_services/set'

#new PUT method -d '{ids:["vk"]}'
AdGuard_URL='http://<my_home_assistant_ip>:3000/control/blocked_services/update'
AdGuard_URL_GET='http://<my_home_assistant_ip>:3000/control/blocked_services/get'

#Login_password generated echo -n 'login:password' | base64
#use the home assistant login credentials
Login_password=“<my encoded credentials>”

#Get bloked services set from file
csv_string=$(<$1)
csv_string=${csv_string//$'\n'/\",\"}
str_b="{\"ids\":[\""
str_e="\"]}"
filter="$str_b$csv_string$str_e"

#for debuging the list of filters 
#echo "$filter $AdGuard_URL"

#curl  -X POST -m 10000 -H "Authorization: Basic $Login_password" -H "Content-Type: application/json" -d "$filter" "$AdGuard_URL"
curl  -X PUT -m 10000 -H "Authorization: Basic $Login_password" -H "Content-Type: application/json" -d "$filter" "$AdGuard_URL"

#Testing filter
#curl  -X GET -m 10000 -H "Authorization: Basic $Login_password" -H "Content-Type: application/json" "$AdGuard_URL_GET"

Then I have created the files for enable/disable services

➜  AdGuard_API ls -l                                      
total 28
-rw-r--r--    1 root     root          1042 Jul 22 08:49 AdGuard_block_services.sh
-rw-r--r--    1 root     root            94 Jul 22 09:37 disable_all
-rw-r--r--    1 root     root            69 Jul 22 09:26 disable_games
-rw-r--r--    1 root     root            25 Jul 22 09:20 disable_social
-rw-r--r--    1 root     root             3 Jul 22 09:41 enable_all
-rw-r--r--    1 root     root             3 Jul 22 09:13 enable_games
-rw-r--r--    1 root     root             3 Jul 21 10:06 enable_social
➜  AdGuard_API pwd             
/root/config/AdGuard_API

➜  AdGuard_API cat disable_games                             
minecraft
roblox
nintendo
rockstar_games
riot_games
steam
epic_games

And tested the script (uncomment the testing filter curl in the script)

bash -x /config/AdGuard_API/AdGuard_block_services.sh /config/AdGuard_API/disable_games

At this point I have created the switches in configuration.yaml

command_line:
  - switch:
      name: social_mode_filters
      command_on: 'bash /config/AdGuard_API/AdGuard_block_services.sh /config/AdGuard_API/disable_social'
      command_off: 'bash /config/AdGuard_API/AdGuard_block_services.sh /config/AdGuard_API/enable_social'
  - switch:
      name: game_mode_filters
      command_on: 'bash /config/AdGuard_API/AdGuard_block_services.sh /config/AdGuard_API/disable_games'
      command_off: 'bash /config/AdGuard_API/AdGuard_block_services.sh /config/AdGuard_API/enable_games'
  - switch:
      name: all_filters
      command_on: 'bash /config/AdGuard_API/AdGuard_block_services.sh /config/AdGuard_API/disable_all'
      command_off: 'bash /config/AdGuard_API/AdGuard_block_services.sh /config/AdGuard_API/enable_all'

Then added the buttons on dashbord and finally create an automation for diasble all unwanted services at 22:30

alias: Diabilita giochi e social
description: ""
trigger:
  - platform: time
    at: "22:30:00"
condition: []
action:
  - service: switch.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: switch.all_filters
mode: single

That’s all.