Trying to evaluate time to use 2 different constants for energy usage


Hi Everyone,

I am trying to create logic using the Function node where:

(1) peak times (3PM - 8PM), I assign and use one peak rate, a constant, that will be used as a factor
(2) non-peak times (outside of 3-8), would have a lower rate

I want to take the factor and multiple it by an instantaneous Energy reading (Watts), which I am able to pull from an HTTP request.

Here’s what I have within the function Node:

var peak = ‘0.32’
var nonpeak = ‘0.22’

var now = new Date();
var hour = now.getHours();
// var day = now.getDay();
// var minutes = now.getMinutes();
IF(hour > 15 || hour < 20)
msg.topic = peak;
else msg.topic = nonpeak;
return msg;

I’m not doing something right, because regardless of my logic, I keep getting the msg.topic = 0.32. In short, it looks like I am able to see the output with msg.topic = 0.32, but I am unable to apply the logic to evaluate the current time.

Any help would be appreciated.


Here’s the JSON export:

[{“id”:“c04b8dfd.7bc”,“type”:“function”,“z”:“c750655c.34e948”,“name”:“rate determine”,“func”:“var peak = ‘0.32’\nvar nonpeak = ‘0.22’\n\nvar now = new Date();\n var hour = now.getHours();\n// var day = now.getDay();\n// var minutes = now.getMinutes();\n IF(hour > 15 || hour < 20)\n msg.topic = peak; \n else msg.topic = nonpeak;\nreturn msg;”,“outputs”:1,“noerr”:3,“x”:410.5,“y”:393,“wires”:[[“e56a45be.a23d08”]]}]



if (hour > 15 || hour < 20)

So if the hour is greater than 15 or the hour is less than 20.
Which covers every hour of the day.

if (hour > 15 && hour < 20)
i.e. if the hour is greater than 15 AND less than 20



Thanks. ukmoose. I’ll give that a try.



Thanks for the response ukmoose. I was able to get the logic to work. Now I am stuck trying how to figure out how to multiple two constants: (1) the rate which comes out of the function node earlier in this thread; (2) times an instantaneous value coming.

(1) has been assigned the msg.topic
(2) is the msg.payload

I have been trying to add a subsequent f(x) node that basically takes the topic and multiplies it with the payload.

Is it possible to simple take (topic * payload)? Or do I need to “change” attributes in order to do what I am attempting?

Right now, all I am getting is the product = NULL



Post the code of the function node that is intended to do the multiplication. Without that it is impossible to know what the problem is.



Sure Colin:
[Edited by @knolleary to wrap flow in a code block ]

[{"id":"c04b8dfd.7bc","type":"function","z":"c750655c.34e948","name":"rate determine","func":"var L = {msg.payload.watt}\nvar peak = 0.32\nvar nonpeak = 0.22\nvar now = new Date();\nvar hour = now.getHours();\n  if (hour > 14 && hour < 21) {\n    msg.payload.watt = peak\n  }\n  else {\n    msg.payload.watt = nonpeak\n  }\nreturn {msg, hour, now, peak, nonpeak};\n\n","outputs":1,"noerr":0,"x":1398.5,"y":328,"wires":[["16663fb5.3bbd7","8ecf69dd.f5a058"]]},{"id":"16663fb5.3bbd7","type":"change","z":"c750655c.34e948","name":"switch 2","rules":[{"t":"set","p":"payload","pt":"msg","to":"msg.payload","tot":"num"},{"t":"set","p":"topic","pt":"msg","to":"msg.topic","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":1516.5,"y":157,"wires":[["bc77adff.58d9","3a737057.2c9ac"]]},{"id":"3a737057.2c9ac","type":"function","z":"c750655c.34e948","name":"Multiply Rate * Watt","func":"var rate = msg.msg.topic\nvar watt = msg.msg.payload\n\nvar wattrate = rate * watt\nreturn {msg, wattrate};\n\n/*payload is msg.msg.payload\nmsg.msg.topic*/","outputs":1,"noerr":0,"x":1756.6428833007812,"y":209.71428203582764,"wires":[["e56a45be.a23d08"]]}]```


Look at the first line of the rate determine function node. It has a red X by it and if you hover over that it tells you that there is a syntax error. Since you are not using the variable L at the moment I suggest you just delete that line or comment it out. I don’t know what you are trying to achieve with the return statement, I imagine it should just read

return msg;

Next you have a Change node called Switch 2 which in itself is a bit confusing, and I have no idea what it is supposed to be doing. Perhaps you could explain.

In your mulitply node you are referencing msg.msg.topic and msg.msg.payload instead of msg.topic and msg.payload and again I don’t understand what you are trying to return. I would have thought that the multiply node should just be something like

msg.payload = msg.topic * msg.payload;
return msg;

except for the fact that msg.payload is not a number, it is an object containing a property watt so perhaps you had better try and explain in more detail what data you have coming into the flow and what you want to get out.



Hi Colin,

Thanks for asking your questions. Let me see if I can help clear up why I did some of the things which I did:

(1) the “msg.msg.payload” and the “msg.msg.topic” had double “msg” because I noticed when I drilled into the path of the particular variable, after it went through two function nodes (the first, being the “rate determine” node, the second, being the “Mulitply Rate * Watt”), it looks as if the data was being passed in an embedded msg node within a top level msg node.

(2) In the rate determine node, I had it returning msg, rate, watt, etc. for debugging purposes, because I was curious to see how the output was behaving, no other reason than that.

(3) “Switch 2” was my feeble attempt to convert / strip down whatever I need to do for a “transform” in order to take basically two numbers and multiply them. Here’s the basic outline of where the numbers are coming from:

WATT - This is a reading from an HTTP request that has been converted into JSON format and exists as one item in the array object coming in

Rate - I am trying to assign two different constants (rate1 and rate2, for instance) based on the time of day, which is the login here:

var peak = 0.32;
var nonpeak = 0.22;
var now = new Date();
var hour = now.getHours();
if (hour > 14 && hour < 21) {
msg.payload.watt = peak;
else {
msg.payload.watt = nonpeak;
return msg;



I would start by finding an fixing the code that results in the data being in msg.msg.payload