# Calculate the time of operation between ON and OFF given Boolean

**URL:** <https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494>\
**Category:** General\
**Created:** [31 May 2021 08:11 UTC](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494 "2021-05-31T08:11:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![nguyenthanhhau-cqu](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nguyenthanhhau-cqu/32/42651_2.png) [@nguyenthanhhau-cqu](https://discourse.nodered.org/u/nguyenthanhhau-cqu)\
**Post date:** [31 May 2021 08:11 UTC](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494/1 "2021-05-31T08:11:22Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![Ranki](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ranki/32/41282_2.png) [@Ranki](https://discourse.nodered.org/u/Ranki)\
**Post date:** [31 May 2021 08:20 UTC](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494/2 "2021-05-31T08:20:06Z")

</div>

Hello @nguyenthanhhau-cqu ,

welcome to the forum; you can try it with this custom node:  
[node-red-contrib-hourglass](https://flows.nodered.org/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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [31 May 2021 09:09 UTC](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494/3 "2021-05-31T09:09:09Z")

</div>

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

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 May 2021 14:39 UTC](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494/4 "2021-05-31T14:39:21Z")

</div>

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`:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/5/e53c9ffd052b893841bd948e09ec0eefec171e8e.png)

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. 😀 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>`.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [31 May 2021 16:30 UTC](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494/5 "2021-05-31T16:30:22Z")

</div>

> [@TotallyInformation](#):
>
> this is **also** sent to MQTT.

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

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [31 May 2021 20:22 UTC](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494/6 "2021-05-31T20:22:54Z")

</div>

> [@Paul-Reed](#):
>
> 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 😀

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [14 June 2021 20:23 UTC](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494/7 "2021-06-14T20:23:25Z")

</div>

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