Accessing the Home Assistant API locally

I have the following sensor that checks on the state on my InfluxDB addon, and it works well:

- platform: rest
  resource: https://my_domain_redacted.duckdns.org/api/hassio/addons/a0d7b954_influxdb/info
  name: InfluxDB
  value_template: "{{ value_json.data.state == 'started'}}"
  scan_interval:
    minutes: 1
  headers:
    Authorization: 'Bearer my_bearer_token_redacted'
    Content-Type: application/json
  device_class: connectivity

In the interests of being kind to my router I thought I’d access the resource via the local address rather than relying on hairpinning.

I’ve tired all of these but none can contact the api endpoint:

resource: http://127.0.0.1/api/hassio/addons/a0d7b954_influxdb/info
resource: http://127.0.0.1:8123/api/hassio/addons/a0d7b954_influxdb/info # actually I didn't try this one 
resource: http://10.1.1.100/api/hassio/addons/a0d7b954_influxdb/info
resource: http://10.1.1.100:8123/api/hassio/addons/a0d7b954_influxdb/info

( where 10.1.1.100 is my ha server local ip address).

Is this possible?
If so, what am I doing wrong?

Took me a little bit of time to work out what you’re doing there, but persisted as it’s inspiring to see how much you help others.

By elimination:

There two won’t work as you are reaching port :80 and not the listening port for HA which is :8123 (by default).

You haven’t mentioned how your HA is set up, but I could see these two options working.


I went ahead and set up my own test to see if I could emulate your error:

- platform: rest
  resource: http://127.0.0.1:8123/api/
  name: APIup
  value_template: "{{ value_json.message == 'API running.'}}"
  scan_interval:
    minutes: 1
  headers:
    Authorization: 'Bearer eyJ0eXAiO.....K9RsNNsJzw2_Q'
    Content-Type: application/json

But this worked for me on HA Docker Container.
Feel free to copy paste this one and see if it works for you, as all the call does is return:

{
    "message": "API running."
}

As mentioned, how is your HA hosted?
The one you didn’t try is the one that worked for me :stuck_out_tongue: Care to try it?

HassOS (NUC image).

Thanks for trying, unfortunately I get:

Error fetching data: http://127.0.0.1:8123/api/hassio/addons/a0d7b954_influxdb/info failed with ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))

In fact it is causing the whole restful sensor platform to abort.

Platform rest not ready yet. Retrying in 30 seconds.
Platform rest not ready yet. Retrying in 60 seconds.
Platform rest not ready yet. Retrying in 90 seconds.
Platform rest not ready yet. Retrying in 120 seconds

Returning to using the duckdns url fixes it.

EDIT: I’m guessing this is a https thing.

I have to use https as I have letsencrypt installed along with duckdns. Even when using the local IP address, but there I can add a browser certificate exception. I can’t do that with the rest sensor, so I cant use https with the local ip address or loopback address.

The solution to this is to use a command line sensor:

- platform: command_line
  name: InfluxDB
  command: 'curl -sSL -H "Authorization: Bearer $SUPERVISOR_TOKEN" http://supervisor/addons/a0d7b954_influxdb/info'
  value_template: "{{ 'ON' if value_json.data.state == 'started' else 'OFF'}}"
  scan_interval: 30
  device_class: connectivity

Or set verify_ssl to false and use this endpoint in the rest sensor:

- platform: rest
  resource: https://homeassistant:8123/api/hassio/addons/a0d7b954_influxdb/info
  name: InfluxDB
  value_template: "{{ value_json.data.state == 'started'}}"
  verify_ssl: false
  scan_interval: 60
  headers:
    Authorization: !secret ha_api_token
    Content-Type: application/json
  device_class: connectivity

This is actually better. The rest sensor has less overhead than the command line sensor.