RESTful returns unavailable

I’m trying to create a RESTful sensor, and even examples from documentation return “unavalable”:

  - platform: rest
    resource: http://ip.jsontest.com
    name: External IP
    value_template: '{{ value_json.ip }}'

I’m running HASS as hass.io on Ubuntu server in docker. Is there any additional setup I need to do to make these commands work?

Thanks for your time and attention!

I just tried it and it works fine for me!?

You do have it set as:

sensor:

  - platform: rest
    resource: http://ip.jsontest.com
    name: External IP
    value_template: '{{ value_json.ip }}'

Yes, I do have set it with “sensor:” and I have other sensors that are working fine. Just “rest” is not working.

Are you running hass.io, though?

No normal one. I have port 8123 forwarded to what I am hosting HA on, perhaps that is it if you don’t?

I don’t have port forwarding, but I do have nginx as proxy. Either way it should not be an issue, as it should not matter as it’s hass trying to access the outside world, not the other way around.

I mean - You don’t need to port-forward all your devices in your home behind a router to browse internet, right?

You are right, I turned off port forwarding and it still works for me.

Pass?

Sorry, what do you mean - Pass?

Pass on why it doesn’t work for you.

On your normal computer does http://ip.jsontest.com return your IP adress in your Internet Browser?

Yes, of course it does :slight_smile: Even more, on machine that’s running HASS I can also curl that link without any problems.

Here’s what is says in logs

Error fetching data: <PreparedRequest [GET]> from http://ip.jsontest.com/ failed with HTTPConnectionPool(host='ip.jsontest.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f1c629fb940>: Failed to establish a new connection: [Errno -3] Try again',))

I don’t know much about nginx but I am wondering if that is blocking the request.

You will have to wait till someone who knows about nginx takes a look at this post.

Sorry

I set it up after my initial failed attempts. And no, I don’t think it can’t block any requests.

So, I tried to SSH into container:

docker exec -it homeassistant /bin/bash

And tried to ping google.com. That did not work.
Then I tried to ping one of google’s IPs directly, and it worked.
So this likely tells that there’s some error with DNS stuff in docker.

Another update.

This: https://development.robinwinslow.uk/2016/06/23/fix-docker-networking-dns/

Did not help, namely - editing the /etc/docker/daemon.json file

I managed to make it work inside my local network, though, and by using direct IPs, but that won’t work for all services, especially not Google Assistant integration.

Further diggings shows that it may be related to this issue.

When running cat /etc/resolv.conf it shows 172.30.32.2 ip…

Ended up doing the following:

docker exec -it homeassistant /bin/bash
echo "nameserver 8.8.8.8" > /etc/resolv.conf

This overwrites contents of the resolv.conf file, setting up google’s DNS server. Now DNS works, and I can even use it’s package manager.

I’m not sure, however, if these changes are permanent, or if anything I do inside a container is completely overwritten whenever I update to a new hass version? I suspect it’s the latter.

I found a more permanent (or automated at least) solution:

Managed to brute force it by creating a cron job that runs this command every minute or so:

docker exec -d homeassistant bash -c "echo 'nameserver 192.168.1.1' > /etc/resolv.conf"

Maybe it will be useful for someone. Replace IP with your DNS, 192.168.1.1 is my router. You can use 1.1.1.1 or 8.8.8.8 for CloudFlare’s or Google’s DNS. Also homeassistant is the name of the docker container, adjust if needed.