# 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:** 1\
**Showing post:** 4

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

---

_[View the full topic](https://discourse.nodered.org/t/calculate-the-time-of-operation-between-on-and-off-given-boolean/46494)._
