Find out if a client is connected to router

Hi, I have a Cradlepoint router as my main router on my LAN. It provides an api, which you can call like this:
http://router.ip.addtress/api/status/lan/clients
This return a json response, which has an array containing mac and ip addresses of attached clients. I would like to use this to determine if certain mac/ip addresses are connected to the router to use as presence detection, running the query say every 30 seconds.
My question is how can I do this in Home Assistant and how can I parse the returned JSON to determine if my target mac/ip combinations are connected?

I’m not afraid to do some coding (I was a professional programmer at one time), but I really need some pointers as to where to start. I have been using Home assistant for about 2 years and am used to writing my automations too.

Any suggestions as to where to start would be much appreciated.
Thank you all

Hi Peter,

I would inspect the JSON being provided by the router and see if you can create a Restful sensor

If you get stuck, post your JSON and I’m sure a smart arse will be along shortly to provide you with the correct value template to use :slight_smile:

But I suggest you try yourself as the experience may lead to bigger and better things :slight_smile:

Hi /dev/null,
I did look at the rest sensor, but it doesnt seem to be able to iterate an array to determine if a client is connected.
I believe my requirement may require some python coding running under appdaemon or pyscript as ieration and compare logic is required.
Cheers

So, presence detection is probably not a computer, but a phone.

We already have this. Check if the device is connected to a WIFI connection.

In sensor.yaml:

- platform: template
  sensors:
    my_phone_home:
      value_template: >-
        {% if states('sensor.my_phone_wifi_connection') == "wifiname" %}
          home
        {% else %}
          away
        {% endif %}
      friendly_name: 'My Home-Wifi'
    wife_phone_home:
      value_template: >-
        {% if states('sensor.wife_phone_wifi_connection') == "wifiname" %}
          home
        {% else %}
          away
        {% endif %}
      friendly_name: 'Wife Home-Wifi'

I believe you need to turn on some sensors in the device. But, currently doing this and it is working.

Post the JSON reply here and someone will probably have a way of passing it with an one-line-wonder-code.

Thanks for the replies. Jeffcrum. Thanks for that. Im not sure that would work for me as I have 3 APs which the phones can connect to and 3 SSIDs on each. I have found a way of doing exactly what I want, here is some code :

import appdaemon.plugins.hass.hassapi as hass
import requests
class CradlepointRouter(hass.Hass):
    def initialize(self):
        self.log("Cradlepoint Router Monitor Started")
        self.run_in(self.query_router, 10, args="1whatever you want to pass")
    def query_router(self, kwargs):
        self.run_in(self.query_router, 10, args="whatever you want to pass")
        url = 'http://<cradlepoint router ip address/api/status/lan/clients'
        username = ',username>'
        password = '<password>'
        session = requests.Session()
        session.auth = (username, password)
        auth = session.post('http://<cradlepoint router ip address>')
        response = session.get(url)

        person1_is_home = self.get_entity("switch.person1_is_home")
        person2_is_home = self.get_entity("switch.person2_is_home")
        person3_is_home = self.get_entity("switch.person3_is_home")
#
        if '<static ip address of person1's phone>' in response.text:
            person1_is_home.call_service('turn_on')
        else:
            person1_is_home.call_service('turn_off')
        if 'if '<static ip address of person2's phone>' in response.text:' in response.text:
            person2_is_home.call_service('turn_on')
#            self.log("person2 is home")
#            self.log(response.text)
        else:
            person2_is_home.call_service('turn_off')
#            self.log("person2 is away")
        if '<static ip address of person3's phone>' in response.text:
           person3_is_home.call_service('turn_on')
        else:
            person3_is_home.call_service('turn_off')

Finding the ip address in the client list was very easy - just use the in function in python.
This code does rely on the phones not using randomised macs and being assigned static ip’s by the router, but it does work very well. You could of course search for the mac address instead of the ip address.

Glad you found what you needed.

For my way above, you could certainly us ‘in’ instead of == and list the SSIDs.

I am putting this here in case anyone comes behind and can use it.