Using Home Assistant automation for parametrized HTTP calls
homeassistant automation
How to use HA automations to run regular HTTP calls with parameters.
October 26, 2023
I've built an API which generates some content that is consumed by other devices. Since this operation is somewhat long-running (~25 seconds) and since devices don't require the freshest content, I wanted to have an automated job that calls the API every 20 minutes.
CRON would be the obvious choice, but as I'm running Home Assistant in my network, I looked at automations as a way to issue these regular HTTP calls.
Requirements
- Repeated schedule - every 20 minutes.
- Call an HTTP endpoint using GET with parameters.
- Make those parameters configurable in the automation - for different types of devices.
Solution
First, I went to configuration.yaml
and created new rest_command
.
rest_command:
refresh_paper:
url: "http://my-ip-address/api/dashboard?force=true&type={{ type }}"
method: GET
Then I had to restart HA in order for the new service to show up (simply reloading the configuration wasn't enough).
Finally, I went to Automations and created a new automation.
- Trigger is Time pattern. Hours and Seconds are empty, but Minutes I set to
/20
(which means every 20-portion of an hour, i.e. 0, 20, 40, 0, 20, etc.). - Actions are individual HTTP calls, for my case I have multiple that should differ by the
type
parameter. So for each of them:- Action type is Call service.
- Service is RESTful command: refresh_paper.
- Then go to Edit in YAML and add the
data
portion, which contains the parameter (type
).
service: rest_command.refresh_paper
data:
type: homeassistant
alias: Call API for "homeassistant"
This results in Home Assistant calling my API for each type of device every 20 minutes.
Feedback
Found something inaccurate or plain wrong? Was this content helpful to you? Let me know!
š§ codez@deedx.cz