Coder Social home page Coder Social logo

hemtjanst / tradfri-mqtt Goto Github PK

View Code? Open in Web Editor NEW
38.0 8.0 9.0 123 KB

Mirrors Trådfri gateways endpoints into MQTT and send commands to the gateway from MQTT

Home Page: https://hemtjan.st/tradfri-mqtt

License: Apache License 2.0

JavaScript 0.79% TypeScript 97.60% Dockerfile 1.61%
mqtt tradfri nodejs smarthome home-automation domotic

tradfri-mqtt's Introduction

Trådfri-MQTT

This project mirrors most of the Trådfri gateways endpoints into MQTT and can be used to send commands to the Trådfri gateway over MQTT

See hemtjan.st/tradfri-mqtt for more information

Using NPM

Installing via NPM:

npm install -g tradfri-mqtt

To start the service:

# 192.168.0.99 = Your trådfri gateway
# abcdefgh = The pre-shared key printed under the gateway
# tcp://127.0.0.1:1883 = Address to MQTT

tradfri-mqtt -g 192.168.0.99 -p abcdefgh -a tcp://127.0.0.1:1883

Using docker

docker volume create tradfri-mqtt-data
docker run -d \
  --name tradfri-mqtt \
  --volume tradfri-mqtt-data:/data \
  --env TRADFRI_GATEWAY=192.168.0.99 \
  --env TRADFRI_PSK=abcdefgh \
  --env MQTT_ADDRESS=tcp://127.0.0.1:1883 \
  hemtjanst/tradfri-mqtt

For armv7 (i.e. raspberry pi 3+), use hemtjanst/tradfri-mqtt:arm7

Arguments

Argument Alias Environment Var Description Default
--gateway <ip> -g TRADFRI_GATEWAY IP Address of Trådfri Gateway Required
--psk <key> -p TRADFRI_PSK Pre-shared key of gateway
--mqtt tcp://mqtt-broker:1883 -a MQTT_ADDRESS Address of MQTT broker Required
--mqttUsername foo -n MQTT_USERNAME Username of MQTT broker
--mqttPassword bar -w MQTT_PASSWORD Password of MQTT broker
--topicPrefix <topic> -x MQTT_TOPIC_PREFIX Topic prefix tradfri-raw
--topicCommand <topic> -c MQTT_TOPIC_CMD Topic for commands tradfri-cmd
--username <username> -u TRADFRI_USERNAME Username for authentication token
--token <token> -t TRADFRI_TOKEN Authentication token (not the same as PSK!)
--storage <path> -s TRADFRI_STORAGE Path to store data in

Getting updates

tradfri-mqtt will try to observe everything that's being published from the gateway, and mirror the messages into MQTT with the prefix tradfri-raw/.

For example, if the trådfri pushes and update for the lightbulb 65554, the raw json message will be published to the MQTT topic tradfri-raw/15001/65554.

Subscribing to tradfri-raw/# will give you all messages, tradfri-raw/15001/# all accessory updates and tradfri-raw/15004/# all group updates.

Sending commands

Commands to the trådfri gateway can be sent by publishing them to the MQTT topic tradfri-cmd. The payload should be a json-encoded string matching the definition:

export declare type TfCommand = {
    // CoAP Method
    method: "get" | "post" | "put" | "delete" | "reset",
    // URL without any prefixes, i.e. "15001/65540"
    url: string,
    // Optional id of the request, will be included in the reply
    id?: string,
    // Replies will be sent to this MQTT topic
    replyTopic?: string,
    // Payload of the request (for POST or PUT)
    payload?: object|string,
}

If replyTopic is set, a reply will be sent to that topic. The definition of the reply:

export declare type TfReply = {
    // ID of the request as set in TfCommand
    id?: string,
    // Response code, for example "2.04"
    code: string,
    // Format of the response, 50 = json
    format: number,
    // Payload, if format is json the payload is decoded into an object,
    // otherwise it will be a raw string
    payload: string|object,
}

Sample requests

Put request

Sent to the tradfri-cmd topic:

{
  "method": "put",
  "url": "15004/162515",
  "id": "group-set-on",
  "replyTopic": "tradfri-reply/xyz",
  "payload": {
    "5850": 1
  }
}

Received from the tradfri-reply/xyz topic:

{
  "id": "group-set-on",
  "code": "2.04",
  "format": null,
  "payload": ""
}

Get request

Sent to the tradfri-cmd topic:

{
  "method": "get",
  "url": "15004/162515",
  "id": "group-get",
  "replyTopic": "tradfri-reply/xyz"
}

Received from the tradfri-reply/xyz topic:

{
  "id": "group-get",
  "code": "2.05",
  "format": 50,
  "payload": {
    "5850": 1,
    "5851": 0,
    "9001": "Name of group",
    "9002": 1498068278,
    "9003": 162515,
    "9018": {
      "15002": {
        "9003": [65546,65547,65548]
      }
    },
    "9039": 220248
  }
}

tradfri-mqtt's People

Contributors

benochsson avatar bonan avatar cipher2k avatar daenney avatar dependabot[bot] avatar rjbez17 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tradfri-mqtt's Issues

Add Arm support

Would it possible to add an arm image to the docker hub so we can run it on for example a Raspberry Pi?

How to control curtain?

I have tried to control a curtain in a group the same way as a dimmers in a group would be controlled. But havent succeded. Is it possible?

Authentication rejected: The DTLS handshake timed out since latest update of Gateway

Hi,

I am running into this issue now since the latest update of Tradfri Gateway:

Authentication rejected Error: The DTLS handshake timed out at normalizeReason (/usr/local/lib/node_modules/tradfri-mqtt/node_modules/node-coap-client/build/lib/DeferredPromise.js:6:16) at Promise.rej [as reject] (/usr/local/lib/node_modules/tradfri-mqtt/node_modules/node-coap-client/build/lib/DeferredPromise.js:14:34) at Socket.onError (/usr/local/lib/node_modules/tradfri-mqtt/node_modules/node-coap-client/build/CoapClient.js:1003:33) at Object.onceWrapper (events.js:422:26) at Socket.emit (events.js:315:20) at Socket.killConnection (/usr/local/lib/node_modules/tradfri-mqtt/node_modules/node-dtls-client/build/dtls.js:221:22) at Socket.expectHandshake (/usr/local/lib/node_modules/tradfri-mqtt/node_modules/node-dtls-client/build/dtls.js:138:22) at Timeout.<anonymous> (/usr/local/lib/node_modules/tradfri-mqtt/node_modules/node-dtls-client/build/dtls.js:96:61) at listOnTimeout (internal/timers.js:554:17) at processTimers (internal/timers.js:497:7)

I am not sure if it is related with the firmware or just a coincidence. But it worked for quite a few weeks and today I cannot control tradfri via MQTT anymore. IKEA app access works fine.

[NFR] Friendly topic and payload

Thnx for releasing v0.0.5, it works really well.
I'm wondering if I could drop a New Feature Request here.

It would be really handy if we can subscribe and publish to the same endpoint where the endpoint contains parsed attributes.

For example if I have a group called "outside" with 2 lights (garden, front door)

Subscribe to: /tradfri/groups/outside 
An update gives:
{
   brightness  : 0, // [0..254 ID 5851]
   state: 0 // [0/1, ID 5850]
}
Publish to: /tradfri/groups/outside
Payload:
{
   brightness  : 200,
   state: 1
}
Subscribe to: /tradfri/lights/garden 
An update gives:
{
   brightness  : 0, // [0..254 ID 5851]
   state: 0 // [0/1, ID 5850]
}
etc...

I found a list of attributes here
Mqtt 3.1. can handle UTF-8 characters so using names in the topic should be a problem.
One issue could be that the group or light is not unique in that case both groups or light should be triggered.

Possibility to control (activate) scenes?

Is there a possibility to activate the scenes created in the Tradfri app through tradfri-mqtt?

They are under mqtt-topic "tradfri-raw/15005". I have an example from my home "tradfri-raw/15005/131073/196608". This turns off all lighting in one room (group).

arm Cannot find module 'node-aead-crypto'

docker run -d --name tradfri-mqtt --volume /srv/tradfri/:/data/ --env TRADFRI_GATEWAY=192.168.1.100 --env TRADFRI_PSK=secret --env MQTT_ADDRESS=tcp://mosquitto:1883 hemtjanst/tradfri-mqtt:arm7

module.js:557
    throw err;
    ^

Error: Cannot find module 'node-aead-crypto'
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/app/node_modules/node-dtls-client/build/lib/AEADCrypto.js:54:47)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)

Error: Connection refused: Not authorized

Hi, I'm having an issue running tradfri-mqtt on a RPi -

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: Connection refused: Not authorized
    at MqttClient._handleConnack (/usr/lib/node_modules/tradfri-mqtt/node_modules/mqtt/lib/client.js:920:15)
    at MqttClient._handlePacket (/usr/lib/node_modules/tradfri-mqtt/node_modules/mqtt/lib/client.js:350:12)
    at work (/usr/lib/node_modules/tradfri-mqtt/node_modules/mqtt/lib/client.js:292:12)
    at Writable.writable._write (/usr/lib/node_modules/tradfri-mqtt/node_modules/mqtt/lib/client.js:302:5)
    at doWrite (/usr/lib/node_modules/tradfri-mqtt/node_modules/readable-stream/lib/_stream_writable.js:428:64)
    at writeOrBuffer (/usr/lib/node_modules/tradfri-mqtt/node_modules/readable-stream/lib/_stream_writable.js:417:5)
    at Writable.write (/usr/lib/node_modules/tradfri-mqtt/node_modules/readable-stream/lib/_stream_writable.js:334:11)
    at Socket.ondata (_stream_readable.js:558:20)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)

Any ideas?

Unable to control a device.

I have tradfri-mqtt installed and can see messages related to my Ikea devices in an MQTT Explorer as well as a HomeSeer software plugin. I am using paho MQTT broker.
I want to send a simple On or Off command to an Ikea Control socket.
It appears as; tradfri-raw/15001/65541:3312:01:5850. 5850 changes between 1 and 0 when I control the device via the Ikea app.
The command I have tried is; tradfri-cmd={ "method": "put", "url": "15001/65541", "replyTopic": "tradfri-reply/15001/65541", "payload": { "5850": 1} }
I have also tried a number of variations to this. I see a reaction in my MQTT Explorer but the socket does not turn on or off.
Any suggestions as to the format of the command would be gratefully accepted.
Cheers
Ian

The DTLS handshake timeout error returned

See reaction of @olem10 in last closed issue. The fix in the last build worked for me, but this error shows again since the last Tradfri update:. Hub software version is 1.15.55. Hope there's a fix for this.

Error discovering Trådfri endpoints Error: The DTLS handshake timed out
at normalizeReason (/app/node_modules/node-coap-client/build/lib/DeferredPromise.js:6:16)
at Promise.rej [as reject] (/app/node_modules/node-coap-client/build/lib/DeferredPromise.js:14:34)
at Socket.onError (/app/node_modules/node-coap-client/build/CoapClient.js:1012:29)
at Object.onceWrapper (node:events:514:26)
at Socket.emit (node:events:394:28)
at Socket.killConnection (/app/node_modules/node-dtls-client/build/dtls.js:227:22)
at Socket.expectHandshake (/app/node_modules/node-dtls-client/build/dtls.js:144:22)
at Timeout. (/app/node_modules/node-dtls-client/build/dtls.js:102:61)
at listOnTimeout (node:internal/timers:557:17)
at processTimers (node:internal/timers:500:7)

Originally posted by @Arnootjoo in #15 (comment)

Cannot pull the tradfri-mqtt image into docker

When I try to pull the tradfri-mqtt image in to my docker instance I get an error. I use the following command:

docker pull ghcr.io/hemtjanst/tradfri-mqtt

Next I get the following output:

Using default tag: latest
Error response from daemon: manifest unknown

It doesn't make any difference when I define a tag as v0.1.4 for example. I then also the get: manifest unknown. What am I doing?wrong?

tradfri-cmd for getting moods

Hello,
thank you for this great library I just wanted to know if there is a tradfri-cmd to explicitly receive all available moods? If so, how ist the url?
Thanks in advance,
Regards

oberserving all possible topics and debugging

Hi,

whats about the following changes?

a) observing all possible "topics"; you get the list when get the following url from the gateway: cmd {"method":"get","url":".well-known/core","id":"well-known","replyTopic":"ikea/response"}; publish the cmd and read the respone. For myself it Looks like this:
Format 40 --> link list
all items with obs set can be observed:
{"id":"well-known","code":"2.05","format":40,"payload":"<//15006>;ct=0;obs,<//15001>;ct=0;obs,<//15004>;ct=0;obs,<//15004/add>;ct=0,<//15004/remove>;ct=0,<//15010>;ct=0;obs,<//15005>;ct=0;obs,<//15011/15012>;ct=0;obs,<//15011/9034>;ct=0,<//15011/9030>;ct=0,<//15011/9031>;ct=0,<//15011/9102>;ct=0,<//15011/9094>;ct=0;obs,<//15011/9095>;ct=0;obs,<//15011/9104>;ct=0;obs,<//15004/161295>;ct=0;obs,<//15005/161295>;ct=0;obs,<//15005/146016/226828>;ct=0;obs,<//15005/161303/226835>;ct=0;obs,<//15005/161303/226836>;ct=0;obs,<//15005/161303/226837>;ct=0;obs,<//15004/146016>;ct=0;obs,<//15005/146016>;ct=0;obs,<//15001/65557>;ct=0;obs,<//15001/65558>;ct=0;obs,<//15001/65542>;ct=0;obs,<//15001/65538>;ct=0;obs,<//15001/65551>;ct=0;obs,<//15001/65552>;ct=0;obs,<//15001/65560>;ct=0;obs,<//15001/65540>;ct=0;obs,<//15001/65554>;ct=0;obs,<//15001/65539>;ct=0;obs,<//15001/65548>;ct=0;obs,<//15001/65537>;ct=0;obs,<//15001/65550>;ct=0;obs,<//15001/65543>;ct=0;obs,<//15001/65541>;ct=0;obs,<//15001/65549>;ct=0;obs,<//15005/161295/211896>;ct=0;obs,<//15005/161295/224865>;ct=0;obs,<//15005/161295/204477>;ct=0;obs,<//15004/161303>;ct=0;obs,<//15005/161303>;ct=0;obs,<//15005/161304/226838>;ct=0;obs,<//15005/161304/226839>;ct=0;obs,<//15005/161304/226840>;ct=0;obs,<//15004/161304>;ct=0;obs,<//15005/161304>;ct=0;obs,<//15005/146016/212837>;ct=0;obs,<//15005/146016/226810>;ct=0;obs,<//15005/146016/212663>;ct=0;obs,<//15005/146016/202403>;ct=0;obs,<//15004/161299>;ct=0;obs,<//15005/161299>;ct=0;obs,<//15005/161305/226841>;ct=0;obs,<//15005/161305/226842>;ct=0;obs,<//15005/161305/226843>;ct=0;obs,<//15004/161305>;ct=0;obs,<//15005/161305>;ct=0;obs,<//15005/161299/226822>;ct=0;obs,<//15005/161299/226823>;ct=0;obs,<//15005/161299/226824>;ct=0;obs,<//15011/9063>;ct=0,<//15005/146016/226844>;ct=0;obs,<//15010/313073>;ct=0;obs"}

b) send debug Information into topic debug?

Kind regards

Ralf

Add topic to ENV

Thank you for this project.
I would like to change the topic (currently it's tradfri-raw)
Would it possible to add a variable to set the topic where to publish to?
For example
MQTT_ADDRESS=tradfri

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.