Adding timestamp to a switch payload and then writing it to a .CSV file

Hi, i am trying to log the times of when I switch devices on and off within my system, and the flow I have made does not work for some reason?

this is my first Node-Red project.

here is the Json code as well.

[{"id":"e169487f.02ad68","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"cbafe6ae.2ab9e8","type":"mqtt out","z":"e169487f.02ad68","name":"","topic":"esp32/led","qos":"","retain":"","respTopic":"","contentType":"","userProps":"","correl":"","expiry":"","broker":"10e78a89.5b4fd5","x":420,"y":240,"wires":},{"id":"d2c1850.c81b778","type":"ui_switch","z":"e169487f.02ad68","name":"","label":"Room 1","tooltip":"","group":"70b4de64.0819","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","topicType":"str","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","animate":true,"x":140,"y":220,"wires":[["cbafe6ae.2ab9e8","15d5e1a7.97221e"]]},{"id":"15d5e1a7.97221e","type":"simpletime","z":"e169487f.02ad68","name":"","mydate":false,"myymd":false,"myyear":false,"mymonth":false,"mymonthn":false,"mydom":false,"mydoy":false,"myday":false,"myhourpm":false,"myhour":false,"mytime":true,"mytimes":false,"myminute":false,"myminutes":false,"mysecond":false,"mymillis":false,"myepoch":false,"myrawdate":false,"mypm":false,"x":570,"y":200,"wires":[["15564263.1a312e"]]},{"id":"31947253.a97b0e","type":"file","z":"e169487f.02ad68","name":"room2","filename":"/home/ruan/smarthomedata/room2","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":970,"y":200,"wires":[]},{"id":"15564263.1a312e","type":"csv","z":"e169487f.02ad68","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":770,"y":200,"wires":[["31947253.a97b0e"]]},{"id":"10e78a89.5b4fd5","type":"mqtt-broker","name":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"70b4de64.0819","type":"ui_group","name":"Lights","tab":"e7c46d5e.a1283","order":1,"disp":true,"width":"6","collapse":false},{"id":"e7c46d5e.a1283","type":"ui_tab","name":"Dashboard","icon":"dashboard"}]

Hi

you need to post code between backticks as otherwise it gets corrupted
e.g.
```
code
```

As to your issue I would not bother with simple time node or csv node, as all you need to do is construuct a one line string and format the current time.
this could all be done in a change node using a JSONata expression and moment()

Here is an example the & are similar to JS + for concatenating (joining strings)

[{"id":"e4f5e458.7a6d1","type":"inject","z":"e169487f.02ad68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"on","payloadType":"str","x":310,"y":140,"wires":[["d2c1850.c81b778"]]},{"id":"d2c1850.c81b778","type":"ui_switch","z":"e169487f.02ad68","name":"","label":"Room 1","tooltip":"","group":"70b4de64.0819","order":0,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"","topicType":"str","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","animate":true,"x":360,"y":200,"wires":[["322c5eec.745b0a"]]},{"id":"6f91b26f.e5ff84","type":"inject","z":"e169487f.02ad68","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"off","payloadType":"str","x":310,"y":280,"wires":[["d2c1850.c81b778"]]},{"id":"322c5eec.745b0a","type":"change","z":"e169487f.02ad68","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment().tz(\"Europe/london\").format(\"YYYY-MM-DD HH:mm:ss\") & \t\",\" &\tpayload & \"\\n\"\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":200,"wires":[["f2878f0a.4a8d88","31947253.a97b0e"]]},{"id":"f2878f0a.4a8d88","type":"debug","z":"e169487f.02ad68","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":700,"y":300,"wires":[]},{"id":"31947253.a97b0e","type":"file","z":"e169487f.02ad68","name":"room2","filename":"/home/ruan/smarthomedata/room2","appendNewline":true,"createDir":false,"overwriteFile":"false","encoding":"none","x":820,"y":200,"wires":[[]]},{"id":"70b4de64.0819","type":"ui_group","name":"Lights","tab":"e7c46d5e.a1283","order":1,"disp":true,"width":"6","collapse":false},{"id":"e7c46d5e.a1283","type":"ui_tab","name":"Dashboard","icon":"dashboard"}]

The inject nodes are just for testing without having to go to dashboard

1 Like

Dude you are seriously awesome! thank you so much!

1 Like

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