Node-red "easy" home automation

A few weeks ago, we had solar panels installed. Not before time given the energy price rises.

The question naturally arose as this isn't as yet a battery storage system as to how to use the excess energy when there is any? An obvious answer is to whack on the immersion heater if there is sufficient spare.

We are using the Solax Cloud system for monitoring our panels and this provides a very simple API to get all the information needed in one REST call.

So next up was to call an electrician to put the immersion on a socket and get a suitably rated smart plug one that doesn't just switch but reports consumption for good measure.

Like most smart plugs this is a "tuya" device and so also has an API.

So how am I going to program this with any number of languages to choose from?

I have a house full of smart gadgets, harmony remotes, hue lights, nest thermostats, alexa routines, but I have never felt the need to look for a programming solution before now as I have never had an issue that needed solving.

At this point my googling found node-red which promises that I can do all I want to do and endless amounts more just using a web browser to drag and drop "nodes" to create flows. 

I even find a guide that is practically a blueprint of what I am trying to do:

https://notenoughtech.com/home-automation/tuya-cloud-api/

So it looks like 3rd generation programming is for dummies?

My first attempt to get this running didn't immediately work, there is a setup form provided where you enter the assorted passwords and keys the API demands, but that form is missing a needed secret key. Turn out I have to configure that manually in each of the nodes that uses it.

It's not a big deal, but it's not going to be fun on a big system if secret bits of information get endlessly duplicated.

Aha I say, if you look at the documentation for the problematic node, the author has anticipated the issue and allows you to pass the secret in a message.

Now at this point solving this might have worked, but I have been playing with a few other node-red bits and bobs and my "palette" of nodes is building.

When I try and inject the secret using a value I have put in the setup dialogue it fails miserably.

It fails because despite indications to the contrary,:

"node-red-contrib-crypto-js": "~0.1.1",
"node-red-contrib-crypto-js-dynamic": "~0.1.22",

in the packages,js function clash and I have no control over which one is called. node-red seems to have no means or control over the nodes people have written conflicting with each other.

One of these packages is a fork of the other and the function names have not been changed. My solution is:

"home-node-red": "file:../Projects/home-node-red",

 where I simply copy in the function I need under an unambiguous name. It's a reminder that even the simplest of projects can still have a programming underbelly. My next exercise is interfacing the my harmony systems where I have also ended up forking a git project to actually get everything working.

*** three week pause ***

I now have a pretty decent system and have added:

https://github.com/marsjupiter1/node-red-contrib-tuya-auth

to github for others to use. This contains a lot more that just a tuya authorisation node. It even has a node for local communication via a socket. The socket code is a compilation of my own code and a couple of attempts I found on github. One had a lack of features, the other had no lack of bugs. Hopefully mine is the best of both worlds.

There have been many "lessons" of perhaps affirmations along the way.

I have long held the view that with any programming project error handling is pretty much the first thing to get right. When you start with someone's simple  example code, you tend to be doing the opposite. There's a lot of things that can happen with a http request and only one of them is getting the right answer!

 


Comments

Popular posts from this blog

A reliable tuya authentication in node-red

Arduino boards - what I wish I'd known when starting

node-red a full home automation example