ottopaulsen / node-red-contrib-power-saver

A Node-RED node to saver money by turning off when the power is most expensive
Other
70 stars 17 forks source link

Powersaver does not work with link call functionality in node-red #162

Open epaulsen opened 1 year ago

epaulsen commented 1 year ago

First off, thank you for a truly great product.
This is not critical and I do have a workaround for the issue. The issue is that powersaver nodes tends to re-write the entire msg object, and that breaks link functionality. Linked flows works sort of like a function in code, you pass in the msg object in one end via link in, and receive on the other end via link out. Any programming language pushes the return point on to the stack before calling a function, in node-red that is the msg._linkSource object.
Consider this flow: image

Here I am attempting to abstract away fetching prices from Nordpool and calling the two base nodes in powersaver that is used in all of my powersaving flows. Idea is not to repeat them all over. The debug output from that flow is as follows: image

As you can see from the debug output encircled in red, the first powesaver-node rewrites the entire msg object, so we loose the _linkSource property, resulting in an error in the link out node(blue highlight).

As a workaround, I have implemented this: image

This works, but the resulting node-red flow becomes more complex than needed.
In case anyone else stumbles upon this issue, I have added the workaround as a gist here: https://gist.github.com/epaulsen/ab687c0b084365857f2596977d8f3c67

ottopaulsen commented 1 year ago

Do you know the solution? Would it be to keep the msg object, and replace only the payload?

epaulsen commented 1 year ago

Yes. That is what the workaround at the bottom does.