Calculate the time of operation between ON and OFF given Boolean

I assumed, there are 2 button ON and OFF. When user click on ON, it will return an array contains a variable "button=True", and when they click on OFF, it returns a variable "button=False". My questions is how to calculate the time between ON and OFF

const start = new Date();
let tt = msg.payload.d.running ; // this will return a Boolean value based on ON or OFF 

let l;
let h;

if(tt == true) {
   l = start.getMinutes(); 
}else {
   h = start.getMinutes(); 
  msg.result= h-l;
  return msg;    
} 

thanks in advance.

Hello @nguyenthanhhau-cqu ,

welcome to the forum; you can try it with this custom node:
node-red-contrib-hourglass

It is from my point of view the easiest way; the manual way would be to store the "last value" and time in flow variables and identify the change by comparison of the values and calculating the timedifference in between. Possible, but the node above might be easier.

Cheers
Ranki

Here are two examples, one with a change node and JSONata and one with a function node.

[{"id":"f6d60f43.f99f28","type":"inject","z":"c74669a0.6a34f8","name":"true","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"d\":{\"running\":true}}","payloadType":"json","x":140,"y":4800,"wires":[["8137485b.2adc38","1ae2aea3.fb16f9"]]},{"id":"8137485b.2adc38","type":"change","z":"c74669a0.6a34f8","name":"","rules":[{"t":"set","p":"ontime","pt":"flow","to":"payload.d.running = true ? $millis() : $flowContext(\"ontime\")","tot":"jsonata"},{"t":"set","p":"result","pt":"msg","to":"payload.d.running = false ? ($millis()-$flowContext(\"ontime\"))/1000 ","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":4820,"wires":[["32050b10.1fbba4"]]},{"id":"1ae2aea3.fb16f9","type":"function","z":"c74669a0.6a34f8","name":"","func":"let tt = msg.payload.d.running ; // this will return a Boolean value based on ON or OFF \n\n\n\n\nif(tt == true) {\n   context.set(\"ontime\", new Date()); \n}else {\n  \n  msg.result= (new Date() - (context.get(\"ontime\") || 0))/1000 ;\n}\n  return msg;    \n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":4880,"wires":[["32050b10.1fbba4"]]},{"id":"231785df.d067ca","type":"inject","z":"c74669a0.6a34f8","name":"false","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"d\":{\"running\":false}}","payloadType":"json","x":140,"y":4860,"wires":[["8137485b.2adc38","1ae2aea3.fb16f9"]]},{"id":"32050b10.1fbba4","type":"debug","z":"c74669a0.6a34f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":580,"y":4820,"wires":[]}]
1 Like

Bit late to the conversation but I store last_on and last_off as part of any IoT switch, this is also sent to MQTT. When the state changes, I record the on_duration and off_duration:

The calculations are done by a single function node that enriches the known_devices data both in the global variable and MQTT.

The updated and updated_by properties are ones that I now use as standard all over the place since my flows have got a bit complex. :grinning: It means that it is much quicker to go tweak something a year or so later when you've forgotten what bit of flow does what. The naming convention is <nr instance>/<flow tab>/<group of nodes>/<fn/node or fn/node name>.

So is it sent to MQTT purely for recording purposes, or is the device also controlled via MQTT

No, it is sent to InfluxDB for recording :grinning:

MQTT output is largely used at the moment simply for event monitoring. By sending to MQTT, I can write other, flexible flows that listen for changes. The Global var is used when you need to compare something with input from a different event.

In fact though, I also use MQTT as a visual monitor since, with the help of MQTT Explorer, it is easy to see what is changing when.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.