From 2c7ed22e14f7911c943b12827b3a1018f2dde7ef Mon Sep 17 00:00:00 2001 From: tsightler Date: Thu, 10 Jan 2019 03:22:28 -0500 Subject: [PATCH] Update to work with OpenHAB 2.4 MQTT Bindings (#8) * Update tuya-mqtt.js * Update tuya-mqtt.js * Revert "Update tuya-mqtt.js" This reverts commit 2ecd4260b163093a5886ced280ec57dc6d9f5145. * Revert "Update tuya-mqtt.js" This reverts commit 9f093db868c0af6cedbd98762f5885cdd844cd53. * Updates for OpenHAB 2.4 MQTT binding Update for OpenHAB 2.4 MQTT binding to act on message in addition to topic * Update README.md * Update README.md --- README.md | 35 +++++++++++++++++++++++++++++++---- tuya-mqtt.js | 6 +++++- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5133455..2f33a53 100644 --- a/README.md +++ b/README.md @@ -38,13 +38,17 @@ MQTT Topic Current device state: tuya/////state -Change device state: +Change device state (by topic): tuya/////command/ - + Example: tuya/////command/on tuya/////command/off +Change device state (by payload) +Use with OpenHAB 2.X MQTT bindings or others where only a single command topic is preferred + tuya/////command // State as Payload (on,off) + Color for lightbulb: Example: @@ -62,7 +66,7 @@ There are some reliability issues with tuyapi. Latest changes changed the syntax All questions regarding the TuyaAPI please ask in the project https://github.com/codetheweb/tuyapi . -## Example items +## Example items for OpenHAB 1.x Bindings (still works with >2.4 but only if legacy 1.x MQTT bindings are enabled) #### simple switch on/off ``` Switch tuya_kitchen_coffeemachine_mqtt "Steckdose Kaffeemaschine" () ["Switchable"] { @@ -77,7 +81,6 @@ Switch tuya_livingroom_ledstrip_tv "LED Regal" () ["Lighting >[broker:tuya/lightbulb////command/off:command:OFF:false]" } ``` - #### change color of lightbulb ``` # .items @@ -113,6 +116,30 @@ then end ``` +## Example items for OpenHAB 2.4 Bindings +#### simple switch on/off + +With OpenHAB 2.X MQTT bindings you can add devices using a generic MQTT Thing via PaperUI or +configuration files. For PaperUI simply at the generic MQTT Thing and set the state and +command topics as follows: +``` + tuya/////state + tuya/////command +``` +If you prefer using configuration files vs PaperUI, it should look something like this: + +``` +Bridge mqtt:broker:myUnsecureBroker [ host="192.168.0.42", secure=false ] +{ + Thing mqtt:topic:mything { + Channels: + Type switch : tuya_kitchen_coffeemachine_mqtt "Steckdose Kaffeemaschine" [ stateTopic="tuya/////state", commandTopic="tuya/////command" ] + } +} +``` +For a light with color you would need a separate channel with the command topic set to +tuya/////color and link that to your +color item. #### Basic UI sitemap ``` diff --git a/tuya-mqtt.js b/tuya-mqtt.js index b7f2fef..5815072 100644 --- a/tuya-mqtt.js +++ b/tuya-mqtt.js @@ -76,7 +76,11 @@ mqtt_client.on('message', function (topic, message) { if (exec == "command") { var status = topic[6]; - device.onoff(status); + if ( status == null ) { + device.onoff(message); + } else { + device.onoff(status); + } } if (exec == "color") { var color = message;