Automate device "reconnect" in Unifi Network

I am using the Unifi Network integration, and I have one device (my car charger), that sometimes becomes unresponsive in Home Assistant (probably because it’s on the other side of a brick wall).

I’ve noticed that if I go into the Unifi App, choose the device (car charger), and force a “reconnect”, that usually fixes this immediately.

Is there a way to do this from Home Assistant? I.e., whenever that device is unavailable, automatically ask Unifi to reconnect it?

Yes there is - here is my example :slight_smile:

alias: Restart Outdoor Lamp
description: ""
trigger:
  - platform: homeassistant
    event: start
condition: []
action:
  - service: unifi.reconnect_client
    data:
      device_id: fec6a37a28c512dcdf793494bb6cdb38
mode: single

If you use the GUI the device ID is auto populated from the friendly name. In your instance you’d trigger it when the state of the charger goes to “unavailable”.

2 Likes

Sweet, I’ll take a stab at this tonight.

Thanks a lot!

1 Like

I posted the detail here but only a SMALL number of my entities have a matching device.

Is there a way to update the list of devices?

https://community.home-assistant.io/t/unifi-new-device-tracking/469486/6?u=pureetofu

I have the same issue, I try to reconnect an IoT device which is MQTT based. It is an entity in HA but not a device.

I ended up just making a script from: https://community.ui.com/questions/Reconnect-automatically-IoT-device-What-is-the-easiest-way-to-achieve-that-task/13dafc1d-d441-4787-9443-277b74d89c0d
This script is then called by HA when there is no longer data being received.

edit: above doesn’t work: that script apparently doesn’t work with the latest UAP firmwares.

edit: finally got it working using the following butchered bash script which does a json api call

#!/bin/bash
#based on https://dl.ubnt.com/unifi/6.0.43/unifi_sh_api

username=
password=
baseurl=https://ip:8443
site=default
mac=

cookie=file

curl_cmd="curl --tlsv1 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "

${curl_cmd} --data "{\"username\":\"$username\", \"password\":\"$password\"}" $baseurl/api/login

${curl_cmd} --data "{\"cmd\":\"kick-sta\", \"mac\":\"${mac}\"}" $baseurl/api/s/$site/cmd/stamgr

${curl_cmd} $baseurl/logout
exit

How would I go about creating a script that reconnected a device whenever Home Assistant detected it was offline / unavailable?

I confirmed the unifi.reconnect_client works for the device, but I am unfamiliar how to indicate that when an entity belonging to that device becomes unavailable, I should call reconnect_client for that device.

I’ve been scratching around this problem on and off for a few days trying to get a template for the device_id to work.

Then I found this thread UniFi Network: Reconnect wireless client - #3 by ha_user22323. The HA user in Unifi needs to be admin!

I need to do a bit of thinking if I want that?

For some reason I struggled to get the syntax correct for the template of the device id? I’ve added this action to the script that updates all my ESPHOME devices. I have a random mDNS issue that affects some devices sometimes and this fix works whilst I try and find the mDNS issue.

action: unifi.reconnect_client
data:
  device_id: "{{ device_id(repeat.item) }}"
enabled: true