Giter Site home page Giter Site logo

unifi2mqtt's Introduction

unifi2mqtt

mqtt-smarthome NPM version dependencies Status Build Status XO code style License

Connect Ubiquiti UniFi controller to MQTT ๐Ÿ“ก

Install

$ sudo npm install -g unifi2mqtt

I suggest to use pm2 to manage the unifi2mqtt process (start on system boot, manage log files, ...)

Usage

Usage: unifi2mqtt [options]

Options:
  -a, --unifi-host      unifi hostname or address         [default: "127.0.0.1"]
  -p, --unifi-port      unifi port                               [default: 8443]
  -c, --unifi-user      unifi user                            [default: "admin"]
  -s, --unifi-password  unifi password                                [required]
  -w, --unifi-site      unifi site                          [default: "default"]
  -k, --insecure        allow ssl connections without valid certificate
  -v, --verbosity       possible values: "error", "warn", "info", "debug"
                                                               [default: "info"]
  -n, --name            instance name. used as topic prefix   [default: "unifi"]
  -u, --url             mqtt broker url            [default: "mqtt://127.0.0.1"]
  -h, --help            Show help                                      [boolean]
  --version             Show version number                            [boolean]

Topics and Payloads

Topics and Payloads follow mqtt-smarthome architectural proposal.

Topics published by unifi2mqtt

  • <name>/status/wifi/<ssid>/client/<hostname> - {"val":true} if client is connected to the wifi network, {"val":false} otherwise. Object also contains the properties mac and ts. Retained.
  • <name>/status/wifi/<ssid>/event/connected - Client connect event. Payload as above. Not retained.
  • <name>/status/wifi/<ssid>/event/disconnected - Client disconnect event. Payload as above. Not retained.
  • <name>/status/wifi/<ssid>/clientCount - number of clients in specific wifi network, e.g. {"val":5}. Retained.
  • <name>/status/wifi/<ssid>/enabled - wifi enabled status, e.g. {"val":true}. Retained.
  • <name>/status/clientCount - number of clients in all wifi networks, e.g. {"val":12}. Retained.
  • <name>/status/device/<device>/led - status of a device led, e.g. {"val":"on"} Retained.
  • <name>/connected - 1 if connected to mqtt broker, 2 if connected to UniFi Controller, 0 on last will. Retained.

Topics subscribed by unifi2mqtt

  • <name>/set/device/<device>/led - switch device led on or off. Payload on, off or default (default uses site-wide led setting).
  • <name>/set/wifi/<ssid>/enabled - enable/disable wireless network. Payload true or false.

License

MIT ยฉ Sebastian Raff

unifi2mqtt's People

Contributors

hobbyquaker avatar mach327 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

unifi2mqtt's Issues

Empty ssid

Sometimes no ssid messages happend.

My unifi has 2 ssid's (stas, iot) but sometimes event if I'm sure that device connected to stas ssid message published with unifi/status/wifi//event/connected topic

Update for Wired and Wireless Environment

The code presently does not totally understand the difference between UniFi AP's and other UniFi equipment... It needs to be extended to deal with switches in particular.

In my case there is one strange thing happening - the LED on the US-24 is missing the 'default' whilst the US-16-PoE includes it.

  • unifi/status/device/Switch/led - {}
  • unifi/status/device/PoE Switch/led - {"val":"default"}

unifi/status/clientCount is the count of all AP's not AP's and switches. Perhaps this should be the aggregate of wired and wireless clients, and unifi/status/wireless/clientCount should be for wireless alone.

I am not 100% sure of how to model the UniFi data into MQTT, else I would be proposing it, and potentially implementing it. I am mostly concerned with end user clients, rather than connections between switches. Connected to each switches are clients with MAC addresses. There can be more than one client on each port (since you can have an unmanaged switch connected to a port). There is also the ability for that port to be PoE, which could be power cycled (outside the scope at the moment).

Ideally I would like to be able to monitor when devices appear in the network, and have things happen then, and also have things happen when they disappear.

ALSO:

I am getting items like
unifi/status/wifi//client/UniFi-CloudKey - {"val":false,"ts":1533539244350}

This is a device that is definitely on my wired network and will always be. There is no reason for it to be reporting as a WiFi.

Got error if using hint from Dockerfile to pass arguments

If one uses the hint from the Dockerfile to pass arguments, the script exits with an error:

$ docker run --rm 118f2b38c69f -e "unifi-password=supersekrit"
[eval]:1
unifi-password=supersekrit
^^^^^^^^^^^^^^

SyntaxError: Invalid left-hand side in assignment
    at new Script (vm.js:84:7)
    at createScript (vm.js:258:10)
    at Object.runInThisContext (vm.js:306:10)
    at Object.<anonymous> ([eval]-wrapper:9:26)
    at Module._compile (internal/modules/cjs/loader.js:1121:30)
    at evalScript (internal/process/execution.js:80:25)
    at internal/main/eval_string.js:23:3

NoSiteContext

Hi All

I am getting an issue where i am getting a 'NoSiteContext' error message. I only have a single site, so it should be 'default', i have tried specifying this manually and using the default option, but its the same. Full debug below:

`2018-12-19 09:17:18.366 unifi2mqtt 1.1.0 starting
2018-12-19 09:17:18.374 mqtt trying to connect mqtt://192.168.7.12
2018-12-19 09:17:18.485 trying to connect https://127.0.0.1:8443
2018-12-19 09:17:18.803 mqtt connected mqtt://192.168.7.12
2018-12-19 09:17:18.804 mqtt > unifi/connected 1
2018-12-19 09:17:18.807 mqtt subscribe unifi/set/#
2018-12-19 09:17:18.809 mqtt subscribe unifi/status/wifi/+/client/+
2018-12-19 09:17:19.184 mqtt > unifi/connected 2
2018-12-19 09:17:19.185 unifi connected
2018-12-19 09:17:19.186 unifi > rest/wlanconf
Unhandled rejection StatusCodeError: 401 - {"data":[],"meta":{"msg":"api.err.NoSiteContext","rc":"error"}}
at new StatusCodeError (/usr/local/lib/node_modules/unifi2mqtt/node_modules/request-promise-core/lib/errors.js:32:15)
at Request.plumbing.callback (/usr/local/lib/node_modules/unifi2mqtt/node_modules/request-promise-core/lib/plumbing.js:104:33)
at Request.RP$callback [as _callback] (/usr/local/lib/node_modules/unifi2mqtt/node_modules/request-promise-core/lib/plumbing.js:46:31)
at Request.self.callback (/usr/local/lib/node_modules/unifi2mqtt/node_modules/request/request.js:185:22)
at Request.emit (events.js:189:13)
at Request. (/usr/local/lib/node_modules/unifi2mqtt/node_modules/request/request.js:1161:10)
at Request.emit (events.js:189:13)
at IncomingMessage. (/usr/local/lib/node_modules/unifi2mqtt/node_modules/request/request.js:1083:12)
at Object.onceWrapper (events.js:277:13)
at IncomingMessage.emit (events.js:194:15)
at endReadableNT (_stream_readable.js:1129:12)
at process.internalTickCallback (internal/process/next_tick.js:72:19)

`

Any help would be much appreciated! :)

Add support for mqtt username/password authentication

Thanks for this project. I am able to connect using my mqtts server URL on port 8883

Could you add options to specify username and password of mqtt server so that we can secure the system from rogue publishers and subscribers ?

How to implement this?

Hi really basic question but what does this get installed on? i'm running the unifi controller software on my windows 10 pc. does this expect the unifi controller to be on a linux box?

force provision option for unreliable wifi ?

@hobbyquaker ,

unifi.post('upd/wlanconf/<wlan_id>', {enabled: false}).then(console.log);

Is it possible to support a force_provision option for the case when the unifi controller fails to reliably enable/disable wifi on all devices that it controls. Some of the time, some of the AP devices, never receive the provisioning command from the unifi controller to enable/disable wifi. What fixes it is a force_provision on the device that failed to go in the PROVISIONING state. Have you seen this unreliable behavior in the controller not honoring the command for wifi on some of the APs in a multi-AP wan group scenario?

The turning on/off led seems much more reliable

The force_provision is on a per AP mac address basis and looks something like this

curl -X POST 'https://example:8443/api/s/default/cmd/devmgr' -H 'Content-Type: application/json' -H 'Accept: application/json' --data-binary '{"mac":"78:8a:20:4b:56:6d","cmd":"force-provision"}' --insecure

Unifi site id

Maybe you can emphasize that when using a multi site unifi controller, the unifi site should be entered with it's ID, not the name. The ID is found in the URL when you are managing a specific site in the Unifi Controller Web GUI.

Cheers!

getting syntax error

root@vmUniFi:/home/stone/node_modules/unifi2mqtt# unifi2mqtt -a 127.0.0.1 -p 8443 -c username -s sekret -w obscurity -k -u mqtt://username:[email protected]:1883
/usr/local/lib/node_modules/unifi2mqtt/index.js:16
let mqttConnected;
^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:374:25)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3

<error> Unexpected server response: 401

Hello, i was working on my unifi box and i see that i am now getting this error from unifi2mqtt: Unexpected server response: 401

im running unifi 7.3.83 with 2 sites. has anyone run into this? any suggestions on how to debug it?

thanks
Raptor

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.