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

Dynamic Config parameter is not realized #172

Closed jarru66 closed 10 months ago

jarru66 commented 10 months ago

I have defined Function node, which is getting numeric value for "hoursOn" from MQTT. I think dynamic config object is correctly sent to PS-STRATEGY-LOWEST-PRICE -node. It should change config dynamically, but seems that it doesn't. It's providing error/info msg: "Payload is missing priceData" and that’s true, but it should use previous price data, if I have understand correctly the docs. And at least parameter should have impact, when the price data is coming next time, but dynamic parameter is not still taking place (as seen by debug node 149).

** Function node content **** // The MQTT message payload is number, but this support also if the number as a string var chargetime = parseFloat(msg.payload); // Convert the payload to a number if needed // Create an object with the MQTT number as a parameter var config = { "hoursOn": chargetime }; // Set the output of the Function node to the object msg.payload = config; return msg;


image

image

image

ottopaulsen commented 10 months ago

If you put a debug node after your function node and look at the data you are trying to send as config, you will see that it does not satisfy the spec. Te spec is here. Example:

"payload": {
  "name": "Lowest Price",
  "config": {
    "maxHoursToSaveInSequence": 4,
    "minSaving": 0.02
  }
}

The msg.payload object shall hold an object named config, but your does not. Your code msg.payload = config will result in a msg object similar to this:

"payload": {
  "hoursOn": 5
}

Try change that line of your code to

msg.payload = { config };
ottopaulsen commented 10 months ago

By the way, I am not sure it works with float. You are using parseFloat. But you can try.

jarru66 commented 10 months ago

Excellent answer as you provided the fixing cfg part as well. I'm novise with Node-red. I change my Function node as you guided and it works. It seems to work with parse-function too (however the currently I have input coming from MQTT as number, so parse is not needed). Working cfg below.


// The MQTT message payload is number, but this support also if the number as a string var chargetime = parseFloat(msg.payload); // Convert the payload to a number if needed

// Create an object with the MQTT number as a parameter var config = { "name": "Lowest Price", "hoursOn": chargetime, };

// Set the output of the Function node to the object // msg.payload = config; // This row was wrong msg.payload = { config }; return msg;

image

image