# Hour meter without stopwatch

**URL:** <https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077>\
**Category:** General\
**Tags:** function-node\
**Created:** [3 March 2023 20:14 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077 "2023-03-03T20:14:38Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![jannis\_avstria](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jannis\_avstria](https://discourse.nodered.org/u/jannis_avstria)\
**Post date:** [3 March 2023 20:14 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/1 "2023-03-03T20:14:38Z")

</div>

I'm new here - thanks for the indulgence  
I have a problem and need help.  
I'm looking for a way to record the switch-on time. However, it should not be a stopwatch function.  
Thank you for a support

[ein-aus-zeit.json](https://discourse.nodered.org/uploads/short-url/12kmOVSm62Yhhzm4zaF4KJDQJTz.json) (5.6 KB)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [3 March 2023 21:40 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/2 "2023-03-03T21:40:51Z")

</div>

> [@jannis\_avstria](#):
>
> I'm looking for a way to record the switch-on time.

Since the button returns an 'OPEN' or ' CLOSE' when the button is flipped, you could add a `switch` node to test for 'OPEN' and have an 'otherwise' condition. Connect the 'OPEN' output of the `switch` to a change node and save a timestamp in a msg variable to do something with (like store it in a database) and flow back to the 'set msg.payload' node.

Connect the 'otherwise' output of the `switch` to the 'set msg.payload' node. For example

```auto
[{"id":"f16b866d48f40ae0","type":"ui_switch","z":"a05b8e52de06b336","name":"","label":"switch","tooltip":"","group":"452af4065a1eead3","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"Wohnung/Badezimmer/Licht","topicType":"str","style":"","onvalue":"CLOSE","onvalueType":"str","onicon":"","oncolor":"","offvalue":"OPEN","offvalueType":"str","officon":"","offcolor":"","animate":false,"className":"","x":150,"y":200,"wires":[["37e9a2da86067daf"]]},{"id":"9693e7e71b76ac4c","type":"change","z":"a05b8e52de06b336","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":680,"y":200,"wires":[[]]},{"id":"37e9a2da86067daf","type":"switch","z":"a05b8e52de06b336","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"OPEN","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"_mcu":{"mcu":false},"x":310,"y":200,"wires":[["434dffc6f6011716"],["9693e7e71b76ac4c"]]},{"id":"434dffc6f6011716","type":"change","z":"a05b8e52de06b336","name":"","rules":[{"t":"set","p":"starttime","pt":"msg","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"_mcu":{"mcu":false},"x":470,"y":100,"wires":[["9693e7e71b76ac4c","3a095e8e4eafed66"]]},{"id":"3a095e8e4eafed66","type":"link out","z":"a05b8e52de06b336","name":"maybe store in Database","mode":"link","links":[],"_mcu":{"mcu":false},"x":730,"y":100,"wires":[],"l":true},{"id":"452af4065a1eead3","type":"ui_group","name":"Standard","tab":"c01e87d721bb455a","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"c01e87d721bb455a","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![jannis\_avstria](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jannis\_avstria](https://discourse.nodered.org/u/jannis_avstria)\
**Post date:** [4 March 2023 14:58 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/3 "2023-03-04T14:58:18Z")

</div>

Sorry, I didn't express myself correctly. I wanted to use timestamp. if I'm not mistaken the timestamp is the system time. so i thought if i turn on and write the timestamp (ts1) and then turn off and write the timestamp (ts2) i can calculate ts2 - ts1 and get the turn on time which will currently be written to a file.  
Now when I switch off my process writes the ts1 variable to the ts 2 variable which is not correct.  
actually, the variables should be empty after the calculation, that would be when switching off, which enables the time stamp ts1 = close and switching off ts2 = open to be supplied with the corresponding time stamp the next time it is switched on.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [4 March 2023 17:25 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/4 "2023-03-04T17:25:15Z")

</div>

A timestamp is the current time in milliseconds since January 1st, 1970.

I'm sorry, but I don't understand what you are saying. Can you lay it out step by step? I.e:

1. the switch is turned on so I grab the current timestamp and put it in msg.startTime
2. etc etc

---

<div class="post-metadata">

**Author:** ![ralphwetzel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ralphwetzel/32/53713_2.png) [@ralphwetzel](https://discourse.nodered.org/u/ralphwetzel)\
**Post date:** [4 March 2023 18:14 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/5 "2023-03-04T18:14:24Z")

</div>

Hi @jannis_avstria

Welcome to this forum!  
I'll change to German for the answer - anticipating that's easier for you to follow.

Das ist der Flow, den Du gepostet hast:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/8/48e04215d40ecab5e6b89f6924a5ab31ebe8a539.png)

Folgende Themen fallen mir auf:

- Der `complete` node ist überflüssig. Verbinde einfach `set msg.payload` mit `function 1`.
- `function 1` speichert nur einen `timestamp`. Wenn Du also Deine Berechnung durchführst, bekommst Du entweder den Zeitraum `On<->Off` oder `Off<->On`. Wenn Dich nur `On<->Off` interessiert, solltest Du nur den `timestamp` für `On` im `flow` context speichern, und diesen dann löschen, nachdem Du `Off` erkannt hast.
- Für die Berechnung der Zeitdifferenz gibt es fertige Funktionen in `node.js` ... und somit keinen Grund, alles zu Fuss zu berechnen.
- Da Du `ts1` & `ts2` nicht in Dein `msg` Objekt einträgst, können die `debug` nodes auch nicht `msg.ts1` oder `msg.ts2` ausgeben.

Hilft Dir das weiter?

---

<div class="post-metadata">

**Author:** ![jannis\_avstria](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jannis\_avstria](https://discourse.nodered.org/u/jannis_avstria)\
**Post date:** [5 March 2023 10:26 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/6 "2023-03-05T10:26:51Z")

</div>

Thanks for the support.  
I just want to set the current systemtimestamp when the switch is "close" and set a current systemtimestamp when the switch is "open". The difference would then be the duty cycle.  
the same by the next time you turn.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [5 March 2023 11:01 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/7 "2023-03-05T11:01:35Z")

</div>

A quick google search using 'javascript calculate time duration in seconds' gives all sorts of answers. Here is a little flow with an example;

```auto
[{"id":"ef115d0382c7ebd8","type":"inject","z":"7335944434544722","name":"Close","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","_mcu":{"mcu":false},"x":150,"y":80,"wires":[["c9b6b160693e0911"]]},{"id":"59c3f928d7d13d71","type":"inject","z":"7335944434544722","name":"Open","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","_mcu":{"mcu":false},"x":150,"y":180,"wires":[["e8283644fc24ffb9"]]},{"id":"e8283644fc24ffb9","type":"function","z":"7335944434544722","name":"function 3","func":"let close = flow.get(\"close\");\nlet open = msg.payload\nlet elapsed = open - close\nnode.warn(`Duty cycle time: ${elapsed/1000} seconds`)\nmsg.payload = elapsed\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"_mcu":{"mcu":false},"x":340,"y":180,"wires":[["b8aebf45108cba4f"]]},{"id":"b8aebf45108cba4f","type":"debug","z":"7335944434544722","name":"debug 202","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","_mcu":{"mcu":false},"x":570,"y":180,"wires":[]},{"id":"c9b6b160693e0911","type":"change","z":"7335944434544722","name":"","rules":[{"t":"set","p":"close","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"_mcu":{"mcu":false},"x":360,"y":80,"wires":[[]]}]

```

 ![Screenshot 2023-03-05 at 6.08.28 AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/c/2c3381a245a26f52ebeef1e97009b2339dce485f.png)  
{edited to use Close - Open = duty cycle

---

<div class="post-metadata">

**Author:** ![jannis\_avstria](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jannis\_avstria](https://discourse.nodered.org/u/jannis_avstria)\
**Post date:** [5 March 2023 11:02 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/8 "2023-03-05T11:02:28Z")

</div>

Danke für die Unterstützung.  
Ich möchte einfach den aktuellen Systemzeitstempel setzen wenn die Schalterstellung "close" ist und einen aktuellen Systemzeitstempel setzen wenn die Schalterstellung "open" ist. Die Differenz wäre dann die Einschaltdauer.  
Beim nächsten Schalten dasselbe Spiel.  
Der Complete-Node ist der Versuch den Ablauf der Einschaltdauer-Rechnung zu standardisieren.  
Die oftmalige Beschäftigung mit der Rechnung der Einschaltdauer macht "blind",  
eine fertige Funktion in node.js finde ich nicht.

---

<div class="post-metadata">

**Author:** ![jannis\_avstria](https://avatars.discourse-cdn.com/v4/letter/j/ed8c4c/32.png) [@jannis\_avstria](https://discourse.nodered.org/u/jannis_avstria)\
**Post date:** [5 March 2023 13:10 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/10 "2023-03-05T13:10:32Z")

</div>

Unfortunately I only have one switch that allows open and close  
thanks

---

<div class="post-metadata">

**Author:** ![juntiedt](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/juntiedt/32/58357_2.png) [@juntiedt](https://discourse.nodered.org/u/juntiedt)\
**Post date:** [5 March 2023 14:44 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/11 "2023-03-05T14:44:31Z")

</div>

[runtime](https://discourse.nodered.org/t/yet-another-engine-runtime-recorder/72028)

have a look

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [5 March 2023 15:25 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/12 "2023-03-05T15:25:00Z")

</div>

@zenofmud 's flow was an example. This is his example with one switch (and a toggle function)

```auto
[{"id":"59c3f928d7d13d71","type":"inject","z":"71622dfc02dd7a7b","name":"Switch","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"str","x":450,"y":1100,"wires":[["97d65fd72c99ff24"]]},{"id":"e8283644fc24ffb9","type":"function","z":"71622dfc02dd7a7b","name":"Calculate Elapsed Time","func":"let close = flow.get(\"close\")\n\nlet open = Date.now()\nlet elapsed = open - close\n\nnode.warn(`Duty cycle time: ${elapsed / 1000} seconds`)\n\nmsg.payload = elapsed\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1070,"y":1080,"wires":[["b8aebf45108cba4f"]]},{"id":"b8aebf45108cba4f","type":"debug","z":"71622dfc02dd7a7b","name":"Elapsed Data","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1300,"y":1080,"wires":[]},{"id":"97d65fd72c99ff24","type":"function","z":"71622dfc02dd7a7b","name":"Toggle Switch State","func":"// Get saved state. Set 'state' to 'false' if flow variable does not yet exist\nlet savedState = flow.get('savedState') ?? false\n\nsavedState = !savedState\nflow.set('savedState', savedState)\n\nmsg.payload = (savedState) ? 'Open' : 'Closed'\nmsg.topic = 'Switch ' + msg.payload\n\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":670,"y":1100,"wires":[["ea5cb91b731abda8"]]},{"id":"c64759d51def0a8e","type":"change","z":"71622dfc02dd7a7b","name":"Set Close Time","rules":[{"t":"set","p":"close","pt":"flow","to":"","tot":"date"}],"action":"","property":"","from":"","to":"","reg":false,"x":1040,"y":1140,"wires":[[]]},{"id":"ea5cb91b731abda8","type":"switch","z":"71622dfc02dd7a7b","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"Open","vt":"str"},{"t":"eq","v":"Closed","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":850,"y":1100,"wires":[["e8283644fc24ffb9"],["c64759d51def0a8e"]]}]

```

---

<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:** [19 March 2023 15:25 UTC](https://discourse.nodered.org/t/hour-meter-without-stopwatch/76077/13 "2023-03-19T15:25:30Z")

</div>

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