question

keith-1 avatar image
keith-1 asked

First time user of Node-Red trying to generate flow for Tuya device

Hi Guys,

I have recently built a system to be off grid capable , 12 kw of solar, 8.2 Fronius single ph inverter, 15 kw Victron Multiplus II , 52.5 kw battery (5x10.5 kw packs) all controlled by Cerbo GX

works great and I have full remote monitoring working

Now comes the reason for the questions

” Controlling when the hot water heating kicks in”

As most of you will have experienced, a storage hot water system is thermostatically controlled and kicks in when the hot water system detects that the storage tank temp has dropped below the minimum threshold. No I live in Australia and many hot water systems are located outside. As a result the water heater is affected to some extent by the outside temp and with the coldest part of the day being just before dawn the heater wants to switch on around 5.00 to 6.00 in the morning.

As the solar output has not yet ramped up at that time this means the water heater is calling on power from my battery to reheat.

My solution is to put a Tuya “Geyser” smart switch into the power feed to the water heater and then using Node-Red to monitor Battery SOC and Inverter output to automatically heat the water when the solar output has reached > 4000 Watts ( Heater is 4800 Watts) and the Battery SOC is above 90% ( These values could be changed based on results after testing)

Now I have installed OS Large with Node-Red

I have downloaded the Tuya nodes for the smart devices

I have downloaded and installed the Tuya Smart App to my phone and connected the Geyser Switch and I can control it from my phone

i have set up an IOT.Tuya account and created a project

I have linked the device to my IOT project

I have used this to determine the Device ID and the device local key

so I think I now have all the data to enter into a Node-Red flow to connect the flow to my device,

However as I am new to node-red I have NO IDEAR on how to set up a node-Red flow to

1 set up a simple manual. ON / OFF

Detect Battery SOC

Detect Solar Output

Use them to Switch the Geyser

———————

Now I have googled, read tutorial and watched YouTube videos ( That is how I have got this far) buy there is nothing out there - that I can find - that gives a step by step introduction to Node-Red, Tuya and Victron for someone who has never created a Node-Red flow before.

They all assume you have some knowledge of Tuya and Node-Red

As so many devices are now controlled by Tuya switching I thought I might put it out there to the Victron Community to see if we can collectively not only solve my problem but see if we can develop a Flow or even a series of Flows that we can document not only the flow development but also highlight what and where in the flow steps we have to insert the correct detail of each switching device ( device Id, key etc) for the flow to work.

I am happy to use the cloud control for the device switching for now and I notice one gentleman has changed to local IP switching access, which we can incorporate later.

I am hoping this post will generate some interest from community members to develop these flows which we can make available with instructions to all members

I do want to make the initial flows simple and not too complicated to start with so the Dummies like me can start simple

I look forward to receiving input from those of you out there that are far more experienced than me in this area

Keith

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.

10 Answers
nickdb avatar image
nickdb answered ·

Have you done a quick search on the subject here?

I did a topic to help get people started. If you get stuck after this, ask some specifics and I will try help.

https://community.victronenergy.com/questions/225863/working-with-tuya-and-node-red.html

2 |3000

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

keith-1 avatar image
keith-1 answered ·

Hi Nick, Yes I have followed your post and it helped me get some of the detail that got me this far.

However I admit I am a dummy when it comes to Node-Red and I have no idea how you set up each of the items in your flow

So for example can you help me set up a simple flow to turn the Geyser On and Off and document what has to be changed in each element of the flow so it talks to my device and my Tuya Cloud App Account.

With that as a first step then we can start looking at how we read SOC of the Battery and add that into the flow and then the same for Solar Output

Keith

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 ·
Definitely. I’ll put something together for you in the morrow.
0 Likes 0 ·
nickdb avatar image
nickdb answered ·

As a noob, the best place to start before jumping into the deep end will be to get a feel for nodered, the basics and the victron nodes.

There are 3 short webinars that will help.

Here is part one, two and three are on the same page:

https://www.youtube.com/watch?v=i_iaciqn_Fg

There is a good cookbook as well to help with context:

https://cookbook.nodered.org/

Just fiddle a bit getting some basic flows going, pulling data from victron nodes etc.


For Tuya, particularly water heating applications, I recommend creating a smart scene in the smart life app.

Set it to look for when power drops beneath a low value ~100W and have it turn the device off. The scope will be every day 24 hours a day.

img-0283.jpg

With this, any time you manually or remotely turn the appliance on, tuya will automatically turn it off when it is at temp. This Guarantees one heating cycle, then you don't have to worry about additional logic in node red.

You want to use the smart device node, not the hub, this means comms are on your local network, not via the cloud which is way too slow.

Tuya devices send and receive data in registers referred to as dps, with each register numbered from 1 upwards.

Using their iot site you can query your device and see which registers it supports, a simple one will be power on/off, power consumption etc.

You will get and set these register values to control your device using inject nodes and functions.

The device node has two outputs, the top one is the data output (it is quite chatty and will continuously send info while it is running) the bottom one is the connection state.

You need to give your devices a fixed IP, and then you just configure that and the local key for nodered to be able to communicate with the device.

If you look at my sample flow I have many inject nodes which shows the format for turning a device on/off or forcing it to connect/disconnect and fetching the state and power consumption.

It is fairly basic JSON stuff:

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

If you inject this, it will fetch the value of dps register 23, which is power consumption.

If I go to the API explorer on iot.tuya.com and go to device control, query device and pop in its device ID it will show me the capabilities:

{
  "result": {
    "properties": [
      {
        "code": "switch_1",
        "custom_name": "",
        "dp_id": 1,
        "time": 1695804285115,
        "value": false
      },
      {
        "code": "countdown_1",
        "custom_name": "",
        "dp_id": 7,
        "time": 1695804285115,
        "value": 0
      },
      {
        "code": "cycle_time",
        "custom_name": "",
        "dp_id": 17,
        "time": 1610969931673,
        "value": ""
      },
      {
        "code": "random_time",
        "custom_name": "",
        "dp_id": 18,
        "time": 1610969931673,
        "value": ""
      },
      {
        "code": "add_ele",
        "custom_name": "",
        "dp_id": 20,
        "time": 1695389979000,
        "value": 67
      },
      {
        "code": "cur_current",
        "custom_name": "",
        "dp_id": 21,
        "time": 1695804295141,
        "value": 0
      },
      {
        "code": "cur_voltage",
        "custom_name": "",
        "dp_id": 22,
        "time": 1696056671637,
        "value": 2339
      },
      {
        "code": "cur_power",
        "custom_name": "",
        "dp_id": 23,
        "time": 1695804295141,
        "value": 0
      }
    ]
  },
  "success": true,
  "t": 1696061259746,
  "tid": "6c7ec57f5f6811ee95454a52bad04d54"
}

dp_id is the dps register number, so you can use this list for pulling/setting info.

On nodered load the dashboard nodes you can use this to create a custom dashboard and render the data you pull, or provide an interface to set them.

You can load nodes via manage pallette: node-red-dashboard

So start here, if you get stuck let me know.

When you can successfully fetch and set data on your device, then you can start on more complicated logic using functions.






img-0283.jpg (238.9 KiB)
2 |3000

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

keith-1 avatar image
keith-1 answered ·

Hi Nick,

Ok, apart from the cookbook I have viewed all of the above mentioned sites already and I keep coming back to my original question.

Where can you get a step by step instruction on setting up a flow where there is an explanation of the parameters that need to be set for each step of the flow generation. And then when you have built a flow how to insert it into a working system.

Now you mention about jumping into the deep end

i have watched all the YouTube videos, I have read a large number of posts and they may be great for someone who has been playing with Nodered for some time but they don’t solve my problem.

Unfortunately I am not at home at the moment and I probably won’t be back there for maybe another ten days. I was trying to learn and set up a flow using nodered on my system at home but using it to operate a test Geyser that I have here with me. so I could learn whilst I was away. I see your comment on the cloud being too slow but I have found the cloud switching from the Tuya app to be almost instantaneous so I wanted to set up a working flow that I could test without the Geyser actually being installed at home, just plugged into local power here locally.

Based on your comments and my circumstances could I ask for your assistance as follows

If I give you my device ID ( <<moderated>>) could you please set up a flow that you can export and I can import as a start point

If you can make it work with the cloud then I can test it here otherwise if you can make it work on a local IP then please use 10.0.0.35 and I will have to wait till I get home to test.

If you can give me your email address I can send you additional detail on my Tuya App Account and IOT Account if that helps

My Email Is << moderated >>

What country are you in ??


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 answered ·

Hi Keith, I'm going to edit out your email else you're going to get spammed.

The simple answer is there is no step by step to building a specific flow, it has to be pieced together from different sources.

The cloud node limits the number/speed of requests, so when you want to frequently probe or check a devices status, it can be done that way, it just isn't ideal. For a remote learning exercise it will be fine.

It's best to define the logic you need.

Turning it on/off a X time, that is easy. Linking that to a victron status, also fairly easy.

Generally the more complex part is adding error handling to deal with exceptions, but that is something to worry about later.

Can you detail a specific scenario and I can try provide a flow where you can just plug in your device details.

If you can log onto iot.tuya.com and go to the cloud section where you created a project and linked your devices, and use its API explorer to query the device from device control menu (just enter the device ID, the other parameter can be left blank) that will help as you need to know what registers it uses. It's in the same general area you used to find the local key.

I am in sunny South Africa.

2 |3000

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

keith-1 avatar image
keith-1 answered ·

Hi Nick, Well I guess SA explains why you were up so late the other night.

Now that you have my email address ( and thanks for editing it out) Maybe we can use email to send detailed info as it is required. If you are happy please send me an email so we can set up that link

Now back to the logic.

Most applications I can foresee involve a switching application of some sort so my learning plan was to start with a simple switching flow ( On/ Off) to insert and check the operation of the device.

Next was to add a monitoring function eg SOC of the battery to add as a required parameter ( learn the function application)

Next was add a second monitoring function (Output of Solar Inverter) and learn how to combine the functions of the two monitoring criteria.

Also I liked your monitoring of the power drop when the water heater reaches temp and cuts off, so probably include that as well.

I figured by the time I learn how all that goes together I will hopefully understand enough of NodeRed and Tuya to be able to start experimenting with other ideas.

I just figured if Tuya is becoming such a major player in the cloud based switching and monitoring arena it was time to learn the interaction with Victron and NodeRed

If you can help me get to achieve the above I would be extremely grateful

Understanding enough of NodeRed and Tuya and then how to make it all operational is my goal.

Keith

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 answered ·
You have mail, hopefully not lurking in your spam folder.
2 |3000

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

keith-1 avatar image
keith-1 answered ·

Hi Nick, Seems to be lost in cyberspace. I don’t have it anywhere.

Address has two “m”’s and one “n” - correct ?

I run a Microsoft Exchange Server and for some reason some emails sent from gmail based accounts don’t seem to arrive. If you are gmail based can you send from a different address and I will reply from my emergency hotmail account so we can use that in future

Thanks

Keith

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 ·
Hi, sorry missed this. Gmail just moaned about the mail server not supporting the right type of encryption. Two M's and one n, indeed.


0 Likes 0 ·
keith-1 avatar image
keith-1 answered ·

Hi Nick, As it seems that we cannot establish email contact is there any chance you can export a copy of your flow and post it here then I can import it and adjust values to accept my device and system

2 |3000

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

keith-1 avatar image
keith-1 answered ·

That hotmail address is “commnetaust”


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 ·

I have tried a hotmail variant. Here is an overview and sample using the local nodes. You need to edit the tuya device node to add either your IP and local key or device-ID and local key.

Here is a basic setup.

You can use either the cloud node or the local one which I prefer. You don't actually need to use static IP's for the local one, you can use the device ID and local key combination as well.

Once your tuya node connects it will automatically broadcast data, generally I will use an inject node that runs every 15 seconds to poll for specific data I want.

I send all the output from the node to a small function that filters out what I want and then sets them into flow variables so they can be accessed from other logic in the flow, such as sending it to a dashboard.

This example just gets data but if you injected a SET command instead, it would change the state of the device.


This is part of a new flow I have started for a heat pump. At the moment I am using it to pull temp info and to display that, next I will add some control logic.


This is the widget I created on a nodered dashboard (the plan is to start the pump when the ambient air temp is optimal):

unnamed.png

To get this I need to pull the data from 4 registers.

Using the IOT site, API explorer and device query I found these registers are 4, 21, 22 and 26. I could also have worked that out by looking at the data the debug node at the output was showing:

unnamed-2.png


So I built a basic flow that looks like this:

unnamed-3.png

The inject node is trying to get the ambient temp (dps 26) every 15 seconds:

unnamed-4.png


At the same time the connected node is broadcasting everything it is doing, this all goes to the function "set power and state":

Where I check that a meaningful value is being returned and if so set it into a flow variable to be used elsewhere:

unnamed-5.png

Now I have the data I want so can use it. Again using a simple dashboard widget flow:

unnamed-6.png

Each inject node runs every 5 seconds (can be any interval you'd like, or a set time etc) and it sends the content of a flow variable to the widget:


unnamed-7.png

And that is it.

The basic premise for control is similar - using flow variables and functions to do if, then else type calls based on data you pull or want set, then injecting them as SET calls.


The next bit I will write will effectively be a "if temp > than X, then power = true", followed by "if battery SOC < Y, then power = false".

flows-8.json.zip

0 Likes 0 ·
unnamed.png (14.8 KiB)