question

nickdb avatar image
nickdb asked

Working with Tuya and Node Red

Recently I wanted to control Tuya-based devices from the GX. Most of the smart timers, plugs, isolators etc that we get locally all seem to use Tuya.

I thought, for those interested I would document my experience and how to go about it.

My use case was to control a geyser, I wanted to use all the free battery capacity in the morning but not hit the ESS limit.

My plan was to create a control logic that monitors the geyser state and battery SOC and turns the geyser on/off as needed until it is at temp.

First step is getting the local access key for each device. This requires moving the device from any third party tuya apps onto the default Tuya smart life app, which is required to access the API.

Follow this video for how to get the local key:

https://www.youtube.com/watch?v=N2EI96Rw9-0

Controlling the device via the cloud was too slow, instead managing them over the LAN is much more reliable.

For this you need the device IP of each, you need to set this to be static on the device or via your home router using reserved DHCP.

At this point you can load node-red-contrib-tuya-smart-device on the cerbo and use the tuya smart device node to connect using the local IP and local key of the device.

My flow is basic it uses the dps register 1 of tuya to turn the device on and off and tracks power consumptions from register 23.

You can query what your device supports via the tuya API on their dev portal.

For my use case, I turn the device on via a normal timer on the app and have a smart scene set that tracks when power drops below 100W (indicating it is heated) and then it turns the geyser off. So no matter how it is activated it will run as long as required to fully heat, then stop.

My flow tracks if the geyser turned itself off, or if it was stopped due to SOC minima, in the latter case, it will keep heating the geyser once the SOC is 4% above the min ESS value (this could be done smarter and linked to the ESS node) and is turned off at 2% above ESS min SOC.

It is a really useful way to control devices via the Cerbo, optimise use and avoid unnecessary grid usage.

The same method can be used for activating pumps, turning on light bulbs (or setting their colour). Next project is to make some house lighting LED's pulse in red on grid loss.


screenshot-2023-08-18-at-143645.png

flows-7.json.zip

Node-RED
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

7 Answers
rgh-1 avatar image
rgh-1 answered ·

@nickdb

When I try and install the tuya-smart-device node I get the error "ERR! notsup Unsupported engine for node-red-contrib-tuya-smart-device@5.2.0: wanted: {"node":">=16.0.0"} (current: {"node":"14.17.6","npm":"6.14.15"})" Are you running the latest release candidate on your GX, or have you manually installed a later version of node-red on your GX?

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

markusvanlaak avatar image
markusvanlaak answered ·

Same issue here. How have you managed to install the tuya-smart-device node?

3 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

nickdb avatar image nickdb ♦♦ commented ·
I am on the RC of venus, it had no issues installing. @Dirk-Jan Faber any suggestions?
0 Likes 0 ·
Dirk-Jan Faber (Victron Energy) avatar image Dirk-Jan Faber (Victron Energy) ♦♦ nickdb ♦♦ commented ·

In order to be able to install it you do need to run the candidate firmware. On the current latest release (3.00), it won't work because of the node version.
So either switch to the latest candidate or wait for the 3.10 release.

1 Like 1 ·
rgh-1 avatar image rgh-1 Dirk-Jan Faber (Victron Energy) ♦♦ commented ·

I installed the candidate release which has a later version of Node Red and this resolved the issue.

0 Likes 0 ·
derrick thomas avatar image
derrick thomas answered ·

@nickdb I just installed the tuya smart device pallet and after some fussing finally got some switches communicating. I have one question I'm hoping you might be able to answer. My switches are just the cheap wifi smart plugs, they only seem to support DPS register 1 and 11. (switch_1 and timer). Am I limited to only turning the switch on and off? Is there no way to get the status of switch_1? Would be nice if I could poll the status of switch_1 and have a dashboard indicator of it's status shown in the event that the switch is turned on or off outside of node red.

3 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

nickdb avatar image nickdb ♦♦ commented ·

Hey @derrick thomas

Tuya has a few quirks, so some experimentation is required.

You can query a device, though I had variable success with different devices, as it often just seemed to ignore the request.

In theory, just injecting the following (with the relevant register) would provide state:


{
    "operation": "GET",
    "payload": {
        "dps": 1
    }
}

What I also noticed, is that they tend to broadcast state constantly - attach a debug node to the output from the device to check - that means you can write a function to intercept and action based on filtering the output.

I did the same with my geysers, I could turn them on remotely with the tuya app, and the node red logic for control worked fine.

Timer is probably countdown - seconds to either turn the device on or off (depending which state it is in).

This is helpful if you wanted to, for example, specify a fixed run time based on how much SOC you have left.


0 Likes 0 ·
derrick thomas avatar image derrick thomas nickdb ♦♦ commented ·
I did play around with the debug node hoping for success, but found that the state was only being broadcast when a command was sent, then the response was received. I will play around with it some more and see what I can do. Thanks for the direction.
0 Likes 0 ·
nickdb avatar image nickdb ♦♦ derrick thomas commented ·
If you get stuck just @ me. Happy to get you sorted. If you struggle we can always make a plan to connect outside the forum framework.

It is pretty straightforward when you get the hang of it.

0 Likes 0 ·
derrick thomas avatar image
derrick thomas answered ·

Got everything sorted (mostly) and made some control flows to achieve what I want. I am having trouble with one of the smart switches. The tuya manager found the switch, but it does not list any data points for it. I can connect the node and get the status with no issues. The status shows several dps with their values, but I cannot write to any of them. I get "code not found". Any ideas?

5 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

nickdb avatar image nickdb ♦♦ commented ·
What device is it?

To clarify, it appears on the iot cloud site as a device, but if you query its details it does not list DPS registers?

It should.

Any chance it is faulty or needs an update? (especially if you have others of the same type)

If you can control it from the app it has to have writable registers.

0 Likes 0 ·
Show more comments
dansonamission avatar image
dansonamission answered ·

Do you know if there is another way to find the local key without using the IOT Core on the tuya website?

Looks like i signed up last year and now the trial to IOT Core has expired.

3 comments
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

nickdb avatar image nickdb ♦♦ commented ·
Shouldn't need a trial.

The video I linked to has the process.

As long as you move all devices to the official Tuya app and don't use the suppliers app, you can open a free account on iot.tuya.com

From there you can query the devices and the account never expires.

Mine hasn't anyway.

Just don't sign up as a Plus member, a basic account is fine.

0 Likes 0 ·
dansonamission avatar image dansonamission nickdb ♦♦ commented ·
The account doesn't expire but you need access to service requests via IOT Core, this expires. I've extended the trial period to get what I need for now.

Thanks!

0 Likes 0 ·
nickdb avatar image nickdb ♦♦ dansonamission commented ·
Weird one. I just signed up on the iot site, it never asked for a trial and my ability to query the devices never stopped.
0 Likes 0 ·
derrick thomas avatar image
derrick thomas answered ·

You can renew the trial period. I don't believe there is an alternate option for getting the local keys.

2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

hominidae avatar image
hominidae answered ·

...as a side note, an alternative for integrating tuya devices could be cross flashing them to tasmota or ESPHome firmware....this will also free you from the chinese cloud lock-in.

1 comment
2 |3000

Up to 8 attachments (including images) can be used with a maximum of 190.8 MiB each and 286.6 MiB total.

nickdb avatar image nickdb ♦♦ commented ·
Unfortunately, not all of them seem able to be reflashed. It depends on the hardware.

In any case, the cloud is only needed to retrieve the keys, once done they are controlled locally, independent of the cloud.

0 Likes 0 ·