From 7a9dde716ce9060f3c97f9aa2006fc34511f9a9f Mon Sep 17 00:00:00 2001 From: KarstenSiedentopp Date: Tue, 9 Apr 2019 18:20:03 +0200 Subject: [PATCH] UPD: README and CHANGELOG --- CHANGELOG.md | 3 +- README.md | 247 +++++++++++++++++++++++++++++++++++---------------- 2 files changed, 174 insertions(+), 76 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cc3d8d1..dec3085 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [2.0.1] ### Added -- Added capability to set multiple dps values ofer MQTT-Command +- Added capability to set multiple dps values over MQTT-Command ### Changed - MQTT-Topic no longer requires a device type @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## [2.0.0] ### Added +- Added supprot for OH MQTT-Binding 2.4 ### Changed diff --git a/README.md b/README.md index 2251533..69a7427 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# TuyaAPI-MQTT Client +# tuyAPI-MQTT Client MQTT interface for Tuya home automation devices sold under various names. This is a wrapper script for the Project codetheweb/tuyapi. https://github.com/codetheweb/tuyapi @@ -12,143 +12,239 @@ https://community.openhab.org/t/step-by-step-guide-for-adding-tuya-bulbs-smart-l Download this project to your openhab2-script-folder "/etc/openhab2/scripts" and install tuyapi from the same folder that the tuya-mqtt.js is in ``` cd /etc/openhab2/scripts -git clone git@github.com:TheAgentK/tuyaapi_mqtt.git // this project -cd tuyaapi_mqtt -npm install //downloads codetheweb/tuyapi + +// clone this project +git clone git@github.com:TheAgentK/tuya-mqtt.git + +// change directory to the project directory +cd tuya-mqtt + +//installs this project along with codetheweb/tuyapi project +npm install ``` -This involves MIM of the connection. Instructions can be found here: https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md +See the setup instructions found here: https://github.com/codetheweb/tuyapi/blob/master/docs/SETUP.md -Create your configuration file: + +## Basic Usage + +### Create your configuration file: ``` cp config.json.sample config.json -nano config.json // edit the configuration file + +// edit the configuration file +nano config.json ``` -Start command +### Start command ``` node tuya-mqtt.js -// For debugging purpose +// For debugging purpose, to use DEBUG : https://www.npmjs.com/package/debug + +//on Linux machines at the bash command prompt: DEBUG=* tuya-mqtt.js -``` -MQTT Topic -``` -Current device state: - tuya/////state +// on Windows machines at the cmd.exe command prompt: +Set DEBUG=* tuya-mqtt.js +``` +URL to [DEBUG](https://www.npmjs.com/package/debug) + + + +### MQTT Topic's (send data) +``` Change device state (by topic): - tuya/////command/ - + tuya////command/ + Example: - tuya/////command/on - tuya/////command/off + - tuya////command/on + - tuya////command/off + - tuya////command/ON + - tuya////command/OFF + - tuya////command/1 + - tuya////command/0 + - tuya////command/toggle + - tuya////command/TOGGLE + - tuya////command/{ "dps": 1, "set": true } + - tuya////command/{ "multiple": true, "data": { "1": true, "7": true } } 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: +Use with OpenHAB 2.X MQTT bindings or others where only a single command topic is preferred: +NOTE: notice that nothing follows the word command, DO NOT but a "/" in after command. + + tuya////command Example: - tuya/lightbulb////color // Color as Payload as hexColor + "ON" + "OFF" + "on" + "off" + "1" + "0" + "toggle" + "TOGGLE" + "{ \"dps\": 1, \"set\": true }" + "{ \"multiple\": true, \"data\": { \"1\": true, \"7\": true } }" -Read data from device: - tuya/////dps // returns JSON.stringify(dps) values, use with care, does not always contain all dps values +Change color of lightbulb (payload as HSB-Color) + tuya////color - tuya/////dps/ // return single dps data value + Example: + 64,0,100 ``` -#### Issues +### MQTT Topic's (read data) +``` +Current device state (allways DPS[1]-Value): + tuya////state + +Device DPS-Values: + // returns JSON.stringify(dps) values, use with care, does not always contain all dps values + tuya////dps + + // return single dps data value + tuya////dps/ +``` + +## Issues There are some reliability issues with tuyapi. Latest changes changed the syntax but still getting error maybe at an even higher rate. -All questions regarding the TuyaAPI please ask in the project https://github.com/codetheweb/tuyapi . +All questions regarding the tuyAPI please ask in the project https://github.com/codetheweb/tuyapi . -## 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 +## Example items for OpenHAB 1.x Bindings (still works with OH > 2.4 but only if legacy 1.x MQTT bindings are enabled) +### simple switch on/off ``` + Switch tuya_kitchen_coffeemachine_mqtt "Steckdose Kaffeemaschine" () ["Switchable"] { - mqtt="<[broker:tuya/////state:state:default:.*], - >[broker:tuya/////command/on:command:ON:true], - >[broker:tuya/////command/off:command:OFF:false]" + mqtt="<[broker:tuya////state:state:default:.*], + >[broker:tuya////command/on:command:ON:true], + >[broker:tuya////command/off:command:OFF:false]" } Switch tuya_livingroom_ledstrip_tv "LED Regal" () ["Lighting"] { - mqtt="<[broker:tuya/lightbulb////state:state:default:.*], - >[broker:tuya/lightbulb////command/on:command:ON:true], - >[broker:tuya/lightbulb////command/off:command:OFF:false]" + mqtt="<[broker:tuya/lightbulb////state:state:default:.*], + >[broker:tuya/lightbulb////command/on:command:ON:true], + >[broker:tuya/lightbulb////command/off:command:OFF:false]" } + ``` -#### change color of lightbulb + +### change color of lightbulb ``` + # .items Group gTuyaLivingColor "Tuya color group" -Color tuya_livingroom_colorpicker "Stehlampe farbe" (LivingDining, Wohnzimmer) +Color tuya_livingroom_colorpicker "Stehlampe farbe" (LivingDining) -String tuya_livingroom_ledstrip_tv_color "Set color [%s]" (gTuyaLivingColor, LivingDining, Wohnzimmer) { - mqtt=">[broker:tuya/lightbulb////color:command:*:default]" +String tuya_livingroom_ledstrip_tv_color "Set color [%s]" (gTuyaLivingColor, LivingDining) { + mqtt=">[broker:tuya/lightbulb////color:command:*:default]" } + + # .rules import org.openhab.core.library.types.HSBType; rule "Set HSB value of item RGBLed to RGB color value" -when - Item tuya_livingroom_colorpicker received command -then - var appName = "Colorpicker.livingroom" - var color = receivedCommand.toString; + when + Item tuya_livingroom_colorpicker received command + then + var appName = "Colorpicker.livingroom" + var color = receivedCommand.toString; - // get all colors and send it via mqtt if light ist enabled - gTuyaLivingColor.members.forEach[ i | - var name = i.name; - var stateName = name.toString.split("_color").get(0); - var stateItem = gTuyaLights.allMembers.filter [ conf | conf.name.contains(stateName.toString) ].head; + // get all colors and send it via mqtt if light ist enabled + gTuyaLivingColor.members.forEach[ i | + var name = i.name; + var stateName = name.toString.split("_color").get(0); + var stateItem = gTuyaLights.allMembers.filter [ conf | conf.name.contains(stateName.toString) ].head; - if(stateItem.state == ON){ - logInfo(appName, name + " change to color: " + color); - i.sendCommand(color); - Thread::sleep(400); - } - ] -end + if(stateItem.state == ON){ + logInfo(appName, name + " change to color: " + color); + i.sendCommand(color); + Thread::sleep(400); + } + ] + end ``` + ## Example items for OpenHAB 2.4 Bindings -#### simple switch on/off +### 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: + + tuya////state + + tuya////command ``` -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" ] - } -} + +If you prefer using configuration files vs PaperUI, it should look something like this: +See also OpenHAB 2.X MQTT binding [documentation](https://www.openhab.org/v2.4/addons/bindings/mqtt.generic/) + +``` + +Bridge mqtt:broker:myUnsecureBroker [ host="localhost", secure=false ] +{ + + Thing mqtt:topic:myCustomMQTT { + Channels: + Type switch : tuya_kitchen_coffeemachine_mqtt "Kitchen Coffee Machine MQTT Channel" [ + stateTopic="tuya////state", + commandTopic="tuya////command", + + // optional custom mqtt-payloads for ON and OFF + on="{ \"dps": 1, \"set\": true }, + off="0" + ] + } + +} + +# *.item Example +Switch tuya_kitchen_coffeemachine_mqtt "Kitchen Coffee Machine Switch" (gKitchen, gTuya) ["Switchable"] { + channel="mqtt:topic:myMosquitto:tuya:coffeemachine" +} + +``` + +For one RGB bulb you would need a separate channel with the command topic set to +`tuya////color` and link that to your color item. + +``` + +Bridge mqtt:broker:myUnsecureBroker [ host="localhost", secure=false ] +{ + + Type colorHSB : livingroom_floorlamp_1_color "Livingroom floorlamp color MQTT Channel" [ + stateTopic="tuya/lightbulb/05200399bcddc2e02ec9/b58cf92e8bc5c899/192.168.178.49/state", + commandTopic="tuya/lightbulb/05200399bcddc2e02ec9/b58cf92e8bc5c899/192.168.178.49/color" + ] + +} + +# *.item Example +Color tuya_livingroom_colorpicker "Floorlamp colorpicker" (gLivingroom){ + channel="mqtt:topic:myMosquitto:tuya:livingroom_floorlamp_1_color" +} + ``` -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 ``` -Switch item=tuya_kitchen_coffeemachine_mqtt mappings=[ON="On", OFF="Off"] -Switch item=tuya_livingroom_ledstrip_tv mappings=[ON="On", OFF="Off"] + +Switch item=tuya_kitchen_coffeemachine_mqtt # Colorpicker for Lightbulbs -Colorpicker item=tuya_livingroom_colorpicker label="RGB Lampenfarbe" icon="slider" sendFrequency=30000 +Colorpicker item=tuya_livingroom_colorpicker label="RGB lamp color" sendFrequency=30000 + ``` ## Contributors @@ -156,6 +252,7 @@ Colorpicker item=tuya_livingroom_colorpicker label="RGB Lampenfarbe" icon="slide - [tsightler](https://github.com/tsightler) - [Tycale](https://github.com/Tycale) - [crashdummymch](https://github.com/crashdummymch) +- [GadgetAngel](https://github.com/GadgetAngel) ## Related Projects: