Hour meter without stopwatch

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 (5.6 KB)

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

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

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.

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

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:

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?

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.

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

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


{edited to use Close - Open = duty cycle

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.

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

runtime

have a look

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

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

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