Just installed this at home, works a treat. You are able to get information from your Unifi software and populate devices in HS using MQTT
https://github.com/hobbyquaker/unifi2mqtt
Pete
https://github.com/hobbyquaker/unifi2mqtt
Pete
If this is your first visit, be sure to check out the FAQ. You must register before you can post.
IMPORTANT: Your first post will be checked for appropriate content. This may take a bit of time.
BusyBox v1.25.1 () built-in shell (ash) _________ / /\ _ ___ ___ ___ / LE / \ | | | __| \| __| / DE / \ | |__| _|| |) | _| /________/ LE \ |____|___|___/|___| lede-project.org \ \ DE / \ LE \ / ----------------------------------------------------------- \ DE \ / Reboot (17.01.4, r3560-79f57e422d) \________\/ -----------------------------------------------------------
#!/bin/sh DEFAULT_MODE="event" DEFAULT_MQTT_SERVER="10.1.1.50" DEFAULT_LAST_SEEN_UPDATE_PERIOD_S=120 MODE=$DEFAULT_MODE MQTT_SERVER=$DEFAULT_MQTT_SERVER LAST_SEEN_UPDATE_PERIOD_S=$DEFAULT_LAST_SEEN_UPDATE_PERIOD_S MQTT_ID_EVENT="OpenWRT-Presence-Event" MQTT_ID_LASTSEEN="OpenWRT-Presence-LastSeen" MQTT_TOPIC="owrtwifi/status/mac-" SCRIPT_NAME="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")" # Parse command line args test_for_mode(){ param_mode=$1 if [ "$param_mode" == "event" -o "$param_mode" == "lastseen" ]; then MODE=$param_mode return 0 fi return 1 } test_for_ipv4(){ param_ip=$1 echo $param_ip | grep -E '\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b' > /dev/null if [ $? -eq 0 ]; then MQTT_SERVER=$param_ip return 0 fi return 1 } test_for_update_periode_s(){ param_up=$1 expr $param_up : '[0-9][0-9]*$' if [ $? -eq 0 ]; then LAST_SEEN_UPDATE_PERIOD_S=$param_up return 0 fi return 1 } print_usage(){ cat << EOF Supported optional parameters: mode: "event" or "lastseen" (default: $DEFAULT_MODE) In event mode changes of registered mac addresses are imediately pushed to the MQTT server In lastseen mode the registered mac addresses are periodically pushed to the MQTT server MQTT server IP: the IPv4 address of the MQTT server (default $DEFAULT_MQTT_SERVER) Udate periode [s]: only relevant for lastseen mode (default $LAST_SEEN_UPDATE_PERIOD_S) Examples: $SCRIPT_NAME $SCRIPT_NAME [B]192.168.1.2 *[/B] $SCRIPT_NAME lastseen 300 EOF } for param in "$@"; do test_for_mode $param || \ test_for_ipv4 $param || \ test_for_update_periode_s $param || \ { print_usage; exit 1; } done if [ "$MODE" == "event" ]; then echo "$SCRIPT_NAME, mode: $MODE, MQTT server: $MQTT_SERVER" iw event | \ while read LINE; do if echo $LINE | grep -q -E "(new|del) station"; then EVENT=`echo $LINE | awk '/(new|del) station/ {print $2}'` MAC=`echo $LINE | awk '/(new|del) station/ {print $4}'` echo "Mac: $MAC did $EVENT" mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_EVENT -t "$MQTT_TOPIC${MAC//:/-}/event" -m $EVENT fi done elif [ "$MODE" == "lastseen" ]; then echo "$SCRIPT_NAME, mode: $MODE, MQTT server: $MQTT_SERVER, period: $LAST_SEEN_UPDATE_PERIOD_S" while true do for interface in `iw dev | grep Interface | cut -f 2 -s -d" "` do # for each interface, get mac addresses of connected stations/clients maclist=`iw dev $interface station dump | grep Station | cut -f 2 -s -d" "` for mac in $maclist do echo "lastseen epoch ${mac//:/-} $(date +%s)" mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_LASTSEEN -t "$MQTT_TOPIC${mac//:/-}/lastseen/epoch" -m "$(date +%s)" -r echo "lastseen iso8601 ${mac//:/-} $(date +%Y-%m-%dT%H:%M:%S%z)" mosquitto_pub -h $MQTT_SERVER -i $MQTT_ID_LASTSEEN -t "$MQTT_TOPIC${mac//:/-}/lastseen/iso8601" -m "$(date +%Y-%m-%dT%H:%M:%S%z)" -r done done sleep ${LAST_SEEN_UPDATE_PERIOD_S} done fi
# Setup Node.js repository sudo curl -sL [URL]https://deb.nodesource.com/setup_10.x[/URL] | sudo -E bash - # Install Node.js sudo apt-get install -y nodejs git make g++ gcc # Verify that the correct nodejs and npm (automatically installed with nodejs) # version has been installed node --version # Should output v10.X npm --version # Should output 6.X # Clone unifi2mqtt repository sudo git clone [URL]https://github.com/hobbyquaker/unifi2mqtt.git[/URL] /opt/unifi2mqtt sudo chown -R pi:pi /opt/unifi2mqtt # Install dependencies cd /opt/unifi2mqtt npm install #then to run sudo unifi2mqtt -k -s whateveryourunifipasswordis
Comment