Response time of API monitoring

Hi,

I’m using several API’s for my work and want to monitor the response time. If it hits a specific MS I need to trigger a notification and want to log it. To start, I need some advice how to achieve this.

Should I use the PING option (as a sensor) in Home Assistant or use the RESTful sensor?

Thanks in advance,

ping is for ICMP pings, so that’s not what you’re after.

For response time tracking though, why not use a tool designed for that? From Nagios and similar high end tools to Uptime-Kuma, there’s lots of options.

Hi Tinkerer,

Thanks for the suggestions. I wasn’t aware of Nagios. One of the reasons I want Home Assistant to monitor is simply the notifications + IoT options (as in, change colours of lamps etc).

I managed to get a cURL working via terminal curl -H "Token: <token>" -s -w %{time_total} https://api.domain.com . The %{time_total} will help me in the end. Any clue how to use this in Home Assistant?

Update on the RESTful sensor, this is working.

  - platform: rest
    name: "Rest API Test"
    resource: https://api.domain.com/check
    headers:
      Token: <token>
    scan_interval: 15
    value_template: "{{ value_json.id }}"

Question remains, how can I get the response time and use this as sensor?

Create a rest sensor with polling disabled that always returns a new value from your resource (e.g. a count or timestamp).

Use a time pattern triggered automation to save a current timestamp then (using the homeassistant update entity service) update your non polled sensor.

Use a triggered template sensor that triggers on the non polled sensor updating (any state change), when this occurs calculate the difference between the current timestamp and your saved timestamp from the automation in the state template.

1 Like

And another update on the cURL way (which is working!)

sensor:
  - platform: command_line
    name: cURL test
    command: curl -o /dev/null -s -w "{\"response_time\":%{time_total}}\\n" -H 'Token:<token>' -X GET https://api.domain.com/check
    scan_interval: 60
    value_template: "{{ value_json.response_time }}"

Quick explanation what the code is doing:

  • Using command_line to send the command
  • Naming the sensor
  • curl writing -o to /dev/null
  • curl silensing -s output
  • curl -write output of %{time_total} as JSON with "{\"response_time\":%{time_total}}\\n"
  • Scan_interval for each 60 seconds
  • value_template: Read the response_time which is in JSON format

As I’m a n00b in cURL let alone programming (okay, I’m at basic level). I will finetune this eventually.