Hello,
Is there a better way to implement this function? How would I implement status check for the switch so it automatically toggles off only when the server says blocking has turned back on? One way maybe is to write the sid to an input text so you can use the rest sensor. I think normally you would want to log out but in this case you would probably have to keep the session open until it times out and get another sid.
Pi-hole v6 API Documentation: https://ftl.pi-hole.net/development-v6/docs/
configuration.yaml
rest_command:
pihole_authenticate:
url: http://192.168.2.11/api/auth
method: POST
payload: '{ "password" : "your_password" }'
content_type: 'application/json; charset=utf-8'
verify_ssl: false
pihole_logout:
url: http://192.168.2.11/api/auth
method: DELETE
verify_ssl: false
headers:
sid: "{{ sid }}"
pihole_disable_5_min:
url: http://192.168.2.11/api/dns/blocking
method: POST
payload: '{ "blocking" : false, "timer" : 300 }'
content_type: 'application/json; charset=utf-8'
verify_ssl: false
headers:
sid: "{{ sid }}"
switch:
- platform: template
switches:
pihole_disable_5_min:
unique_id: "my_uuid"
value_template: "{{ is_state('automation.pi_hole_disable_for_5_minutes', 'on') }}"
turn_on:
service: automation.trigger
target:
entity_id: automation.pi_hole_disable_for_5_minutes
turn_off:
service: automation.trigger
target:
entity_id: automation.pi_hole_disable_for_5_minutes
automation.yaml
automation:
- alias: "Disable Pi-hole for 5 minutes"
triggers:
- trigger: event
event_type: pi_hole_disable_5_min
actions:
- action: rest_command.pihole_authenticate
response_variable: response
- if: "{{ response['status'] == 200 }}"
then:
- alias: "Parse data"
variables:
sid: "{{ response['content']['session']['sid'] }}"
- action: rest_command.pihole_disable_5_min
data:
sid: "{{ sid }}"
- service: rest_command.pihole_logout
data:
sid: "{{ sid }}"
else:
- service: persistent_notification.create
data:
message: "Failed to authenticate with Pi-hole API"
title: "Pi-hole API Error"