How integrate a Tuya device in HA via MQTT

Hi. I'm currently using Node Red and that node to control a fan heater that isn't correctly recognized in Home Assistant (lack of controls e.g. fan swing). The author of the node has shared two example flows, and I'm trying to modify the second one to match my needs. I'm trying to reduce the code to match only one device, but I'm too new to json and Node-Red. Can someone help me to understand how do that, please? Thanks!

OK so you have already extracted the ID and Key i assume ?

I tried to use same plugin for a recent setup i did with a heatpump system and could not get it to work

Post up the code you are using and some screenshots of the debug window so we can see what you have got so far

Craig

Yes I've extracted the key using the tuya developer account method, it works like a charm with my fan heater. The debug window shows the status message on injecting "request". I've renamed the dps in the tuya local node to simplify my life. When I've installed the node, I had to install some dependencies by myself, but I don't remember which ones. I'm posting my flow code below, consider that it is a work in progress and I'm still not able to send or receive MQTT messages from home assistant, but at least you can try to tweak with my mess :wink:


[{"id":"32194e18.6e58b2","type":"tab","label":"TuyaLocal-MQTT-HA Originale","disabled":false,"info":""},{"id":"c6af0fb6.17377","type":"change","z":"32194e18.6e58b2","name":"set tuya_devices","rules":[{"t":"set","p":"tuya_devices","pt":"flow","to":"{\t   \"devId\"      : \"PUT-YOUR-DEVID-HERE\",\t   \"ip\"         : \"111.111.111.111\",\t   \"name\"       : \"YOUR-DEVICE-NAME\"\t}\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":220,"wires":[[]]},{"id":"62d16e70.66252","type":"inject","z":"32194e18.6e58b2","name":"","repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":130,"y":220,"wires":[["c6af0fb6.17377"]]},{"id":"ce7a952.264da68","type":"inject","z":"32194e18.6e58b2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"tuya_devices","payloadType":"flow","x":120,"y":300,"wires":[["cdb3ed47.3c86d","5f83be05.f0e2","731eedaa.412fb4"]]},{"id":"cdb3ed47.3c86d","type":"split","z":"32194e18.6e58b2","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":270,"y":300,"wires":[["5cecfd3c.14c664"]]},{"id":"5cecfd3c.14c664","type":"change","z":"32194e18.6e58b2","name":"set mqtt devices","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t$topic_prefix := \"tuya\";\t$dev_name := payload.name;\t   {\t       \"platform\":\"mqtt\",\t       \"name\": $dev_name,\t       \"state_topic\":$topic_prefix & \"/\" & $dev_name & \"/status\",\t       \"value_template\": \"{{ value_json.state }}\",\t       \"command_topic\":$topic_prefix & \"/\" & $dev_name & \"/set\",\t       \"availability_topic\":$topic_prefix & \"/\" & $dev_name & \"/available\",\t       \"payload_available\":\"online\",\t       \"payload_not_available\":\"offline\",\t       \"json_attributes_topic\": $topic_prefix & \"/\" & $dev_name & \"/status\",\t       \"json_attributes_template\": \"{{ value_json.attributes | tojson }}\",\t       \"payload_on\":\"ON\",\t       \"payload_off\":\"OFF\",\t       \"optimistic\":false,\t       \"qos\":0,\t       \"retain\":false\t   }\t)","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"\"homeassistant/switch/\" & payload.name & \"/config\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":300,"wires":[["d7c7b28d.5a0ce"]]},{"id":"d7c7b28d.5a0ce","type":"mqtt out","z":"32194e18.6e58b2","name":"Bridge HA MQTT","topic":"nodered/termoventilatore","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"47093b2.b68d0c4","x":670,"y":300,"wires":[]},{"id":"9087ca37.c8f878","type":"tuya-local","z":"32194e18.6e58b2","devName":"YOUR-DEV-NAME","devIp":"111.111.111.111","devId":"YOUR-DEV-ID","devKey":"YOUR-LOCAL-KEY","protocolVer":"3.3","renameSchema":"{\"1\":\"01-power\",\"2\":\"02-temperature\",\"3\":\"03-current_temperature\",\"7\":\"07-swing\",\"10\":\"10-unknown\",\"11\":\"11-timer\",\"101\":\"101-hvac_mode\",\"102\":\"102-unknown\"}","filterCB":"","x":680,"y":400,"wires":[["e522187e.d32008","5a2a105f.acab5"]]},{"id":"83d8c789.ad6098","type":"mqtt in","z":"32194e18.6e58b2","name":"","topic":"tuya/+/set","qos":"2","datatype":"auto","broker":"7b9a492a.76f2c8","x":140,"y":440,"wires":[["eda46f7c.fbc5f"]]},{"id":"1bcc4c12.0ac3f4","type":"change","z":"32194e18.6e58b2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"data.available ? \"online\" : \"offline\"","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"\"tuya/\" & data.name & \"/available\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":960,"y":460,"wires":[["481cbfa0.c8d3a"]]},{"id":"481cbfa0.c8d3a","type":"mqtt out","z":"32194e18.6e58b2","name":"","topic":"","qos":"","retain":"","broker":"7b9a492a.76f2c8","x":1170,"y":420,"wires":[]},{"id":"eda46f7c.fbc5f","type":"change","z":"32194e18.6e58b2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t$new_state := (payload = \"ON\") ? true : false; \t{ \"set\" : $new_state, \"dps\": 1 }\t)","tot":"jsonata"},{"t":"set","p":"topic","pt":"msg","to":"(\t$name:= $split(topic, \"/\")[1];\t   $tuya:= [\t       {\t           \"devId\":\"82180707c44f33a6743a\",\t           \"ip\":\"10.10.4.81\",\t           \"localKey\":\"acb2ad7b927d1dac\",\t           \"name\":\"switch_1\",\t           \"state_t\":\"medion/switch_1\",\t           \"cmd_t\":\"medion/switch_1/set\",\t           \"attr_t\":\"medion/switch_1/attributes\",\t           \"avai_t\":\"medion/switch_1/available\",\t           \"entity_id\":\"switch.medion_switch_1_mqtt\"\t       },\t       {\t           \"devId\":\"82180707c44f3387a0fe\",\t           \"ip\":\"10.10.4.82\",\t           \"localKey\":\"58c3558e3ca45c9e\",\t           \"name\":\"switch_2\",\t           \"state_t\":\"medion/switch_2\",\t           \"cmd_t\":\"medion/switch_2/set\",\t           \"attr_t\":\"medion/switch_2/attributes\",\t           \"avai_t\":\"medion/switch_2/available\",\t           \"entity_id\":\"switch.medion_switch_2_mqtt\"\t       }\t   ];\t$device := $filter($tuya, function($x) {\t    $x.name = $name\t});\t$device.ip\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":440,"wires":[["b43c0c23.54e02"]]},{"id":"b43c0c23.54e02","type":"switch","z":"32194e18.6e58b2","name":"","property":"topic","propertyType":"msg","rules":[{"t":"eq","v":"tuya_devices.0.ip","vt":"flow"},{"t":"eq","v":"tuya_devices.1.ip","vt":"flow"}],"checkall":"true","repair":false,"outputs":2,"x":450,"y":440,"wires":[["9087ca37.c8f878"],["82a9a56b.4a3ac8"]]},{"id":"a6f1aa8.e43b158","type":"function","z":"32194e18.6e58b2","name":"","func":"const dps =  msg.payload.dps;\nlet state = null;\nlet power = null;\nlet voltage = null;\nlet current = null;\nif (\"1\" in dps) {\n    state = (dps['1']) ? \"ON\" : \"OFF\";\n}\n\nmsg.payload = {\n    state: state,\n    attributes: {power: \"0 W\",current:\"0 mA\",voltage:\"0 V\"}\n};\n\nif (\"19\" in dps) {\n    msg.payload.attributes.power = (dps['19']/10).toString() + \" W\";\n}\n\nif (\"20\" in dps) {\n    msg.payload.attributes.voltage = (dps['20'] /10).toString() + \" V\";\n}\n\nif (\"18\" in dps) {\n    msg.payload.attributes.current = (dps['18']).toString() + \" mA\";\n}\n\n\n\nif (msg.payload.state === null) {\n    delete msg.payload.state;\n}\n\nmsg.topic = \"tuya/\" + msg.data.name + \"/status\"\nreturn msg;","outputs":1,"noerr":0,"x":930,"y":420,"wires":[["481cbfa0.c8d3a"]]},{"id":"b82a5a0d.39fce8","type":"comment","z":"32194e18.6e58b2","name":"set device information","info":"","x":140,"y":160,"wires":[]},{"id":"6f183dbb.4b2ff4","type":"comment","z":"32194e18.6e58b2","name":"set homeassistant discovery","info":"","x":160,"y":260,"wires":[]},{"id":"58d7f120.73226","type":"comment","z":"32194e18.6e58b2","name":"control-status","info":"","x":110,"y":380,"wires":[]},{"id":"82a9a56b.4a3ac8","type":"tuya-local","z":"32194e18.6e58b2","d":true,"devName":"switch_2","devIp":"10.10.4.82","devId":"82180707c44f3387a0fe","devKey":"58c2568e3ca45c9e","protocolVer":"3.3","renameSchema":"","filterCB":"","x":640,"y":480,"wires":[["e522187e.d32008"]]},{"id":"e522187e.d32008","type":"function","z":"32194e18.6e58b2","name":"","func":"if (\"payload\" in msg) {\n    return [msg,msg];\n} else {\n    return [null,msg];\n}","outputs":2,"noerr":0,"x":780,"y":440,"wires":[["a6f1aa8.e43b158"],["1bcc4c12.0ac3f4"]]},{"id":"6efd6bf9.5a25f4","type":"inject","z":"32194e18.6e58b2","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"true","payloadType":"bool","x":290,"y":700,"wires":[[]]},{"id":"285f25a0.ee74ba","type":"inject","z":"32194e18.6e58b2","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"false","payloadType":"bool","x":290,"y":740,"wires":[[]]},{"id":"c3baf10a.6d7d2","type":"inject","z":"32194e18.6e58b2","name":"{\"set\": true, \"dps\" : 1}-Accendi","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"set\": true, \"dps\" : 1}","payloadType":"json","x":1160,"y":40,"wires":[["9087ca37.c8f878"]]},{"id":"b38290b1.1ae12","type":"inject","z":"32194e18.6e58b2","name":"request","props":[{"p":"payload","v":"request","vt":"str"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"request","payloadType":"str","x":270,"y":40,"wires":[["9087ca37.c8f878"]]},{"id":"10c644c2.eb744b","type":"inject","z":"32194e18.6e58b2","name":"disconnect","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"disconnect","payload":"disconnect","payloadType":"str","x":600,"y":40,"wires":[["9087ca37.c8f878"]]},{"id":"a553d40c.894fd8","type":"inject","z":"32194e18.6e58b2","name":"connect ","props":[{"p":"payload","v":"connect","vt":"str"},{"p":"topic","v":"","vt":"string"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"connect","payloadType":"str","x":430,"y":40,"wires":[["9087ca37.c8f878"]]},{"id":"6659b7a8.32a158","type":"inject","z":"32194e18.6e58b2","name":"{\"set\": false, \"dps\" : 1}->Spegni","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"set\": false, \"dps\" : 1}","payloadType":"json","x":850,"y":40,"wires":[["9087ca37.c8f878"]]},{"id":"6f8cd559.9e21fc","type":"inject","z":"32194e18.6e58b2","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"toggle","payloadType":"str","x":290,"y":780,"wires":[[]]},{"id":"e2b3e181.94eee","type":"inject","z":"32194e18.6e58b2","name":"{\"set\": true, \"dps\" : 7}->Swing On","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"set\": true, \"dps\" : 7}","payloadType":"json","x":1170,"y":100,"wires":[["9087ca37.c8f878"]]},{"id":"9aa1a403.6241e8","type":"inject","z":"32194e18.6e58b2","name":"{\"set\": false, \"dps\" : 7} -> Swing Off","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"set\": false, \"dps\" : 7}","payloadType":"json","x":860,"y":100,"wires":[["9087ca37.c8f878"]]},{"id":"5a2a105f.acab5","type":"debug","z":"32194e18.6e58b2","name":"Status TuyaLocal","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1080,"y":300,"wires":[]},{"id":"5f83be05.f0e2","type":"debug","z":"32194e18.6e58b2","name":"boh","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":290,"y":340,"wires":[]},{"id":"731eedaa.412fb4","type":"debug","z":"32194e18.6e58b2","name":"boh2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"topic","targetType":"msg","statusVal":"","statusType":"auto","x":290,"y":400,"wires":[]},{"id":"47093b2.b68d0c4","type":"mqtt-broker","name":"YOUR-MQTT","broker":"127.0.0.1","port":"1883","clientid":"node-red-mqtt","usetls":false,"protocolVersion":"4","keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willPayload":"","willMsg":{},"sessionExpiry":""},{"id":"7b9a492a.76f2c8","type":"mqtt-broker","name":"hass","broker":"10.10.0.152","port":"1883","clientid":"nr-docker","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

OK so just to confirm - and put aside HA for the time being -

You can talk to the fan through NR OK i.e. the Tuya.local node is working and you can send and receive messages to the fan to change DPS etc ?

I could not get that node to work reliably with my heat pump - but if you confirm it does work i will go back and have another look - are those DPS values in the debug window being echoed back/sent as a status update from the Fan - or are you just debugging stuff you are sending out ?

Craig

@craigcurtin here is explained a possible cause of your issues with the heat pump, I mean the tcp connection. I have experienced a lot of disconnections of the fan from both the smartlife app and node red when trying to send commands from the one and from the other. Now I’m testing the fan controlled only from HA and MQTT, it seems to work fine and without disconnections.

And the message in the debug tab is the result of injecting the command “request”. Usually the device will output only the status of the dps that have changed by command or by a change of the room temperature sensor.

Yeah i have already been through the TuyaApi stuff pretty extensively.

I am using the TUYA-MQTT project (which is now in maintenance only mode) and this just puts a nice front end of MQTT commands onto the TuyaAPI.

I have grabbed the JSON you posted and imported it.

However even after performing a manual install of the NPM (as user Pi not Root) i am still getting the Tuya Local device coming up as Unknown.

Did you just follow the install as per the link that you posted ?

WHat version of NR are you running ?

Craig

[EDIT] - a complete restart of the VM had the device appear

OK in terms of having the code match just one device.

On your tuya.local input node - put a switch node on the output of it - and in the switch node search for the property that contains the device ID

such as

payload.devId - put in your ID that you wish to match and then direct that out to output 1 and continue processing from there.

Craig

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.