How can I use my sensor.myip to show my ISP?

Hi All.

I’m new to Home Assistant and I’m trying to find a way to build a sensor that will use the output of my dnsip sensor to tell me what ISP my public IP address belongs to. I have multiple internet connections and want to be able to see which one is active very quickly.

I was able to create a sensor to find my public IP and that is working well:
sensor dns_ip in my configuration.yaml file:

sensor dns_ip:
  # Own public IPv4 address
  - platform: dnsip
    scan_interval: 10

I went down a few paths and I’m currently trying this in my configuration.yaml as well but I’m not getting any results (“Unavailable”):

sensor domain:
  - platform: template
    sensors:
      domain_name:
        friendly_name: 'Internet Service Provider'
        value_template: >
          import socket
          {% set isp = socket.gethostbyaddr('sensor.myip') %}
          {{ print(isp) }}

Thanks in advance!

https://www.ipify.org/ has an API that might be used.
The information you need is probably in the IP Geolocation API that is linked just a bit down the page.

Thanks Wally. I went down this rabbit hole but I’d rather find a way to do this without a charge.

Sorry, I did not realize that part of their API was not free.

Maybe this one instead then.
https://ip-api.com/

I would try to do this with the Command line Sensor.

The command matching your attempt would be python3 -c "import socket; print(socket.gethostbyaddr('1.2.3.4')[0])".

I’m no templating expert, but here’s an example in the documentation.