Skip to content
  • MySensors
  • OpenHardware.io
  • Categories
  • Recent
  • Tags
  • Popular
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Brand Logo
  1. Home
  2. Controllers
  3. Node-RED
  4. Injecting a Timestamp in Node Red

Injecting a Timestamp in Node Red

Scheduled Pinned Locked Moved Node-RED
4 Posts 2 Posters 4.6k Views 3 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jgrieco
    wrote on last edited by
    #1

    I'm wondering if anyone has been able to use node red to inject a timestamp and join it to each incoming message from a serial gateway. My end goal is to output a .csv file with timestamped sensor values, so I want to attach a timestamp immediately upon receiving each message.
    I am having trouble with the msg. type that is required for the join node, as well as being able to trigger the inject timestamp node upon receiving a message from the serial port.

    Thanks in advance!

    Joel

    1 Reply Last reply
    0
    • electrikE Offline
      electrikE Offline
      electrik
      wrote on last edited by
      #2

      Well, I'm not using a serial gateway but this flow adds a timestamp to a mqtt message and logs it.
      You can tune this to your needs.

      [{"id":"844d0a60.59e128","type":"function","z":"850b8bf6.c34fe8","name":"Add timestamp","func":"msg.payload = new Date().toLocaleString() + '.' + new Date().getMilliseconds() + ', topic, ' + msg.topic + ', payload: ' + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":580,"wires":[["bef9e569.e5f048"]]},{"id":"f2c362e0.a3bf4","type":"mqtt in","z":"850b8bf6.c34fe8","name":"","topic":"mygateway1-out/#","qos":"2","broker":"5f4cad33.82b5b4","x":150,"y":580,"wires":[["844d0a60.59e128"]]},{"id":"bef9e569.e5f048","type":"file","z":"850b8bf6.c34fe8","name":"","filename":"/home/pi/mysensors.log","appendNewline":true,"createDir":false,"overwriteFile":"false","x":550,"y":620,"wires":[[]]},{"id":"5f4cad33.82b5b4","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]
      

      By the way, how are you using the serial gateway? I've tried it before but couldn't send and receive messages at the same time as the serial port was in use by either the sending or the receiving flow.

      J 1 Reply Last reply
      0
      • electrikE electrik

        Well, I'm not using a serial gateway but this flow adds a timestamp to a mqtt message and logs it.
        You can tune this to your needs.

        [{"id":"844d0a60.59e128","type":"function","z":"850b8bf6.c34fe8","name":"Add timestamp","func":"msg.payload = new Date().toLocaleString() + '.' + new Date().getMilliseconds() + ', topic, ' + msg.topic + ', payload: ' + msg.payload;\nreturn msg;","outputs":1,"noerr":0,"x":360,"y":580,"wires":[["bef9e569.e5f048"]]},{"id":"f2c362e0.a3bf4","type":"mqtt in","z":"850b8bf6.c34fe8","name":"","topic":"mygateway1-out/#","qos":"2","broker":"5f4cad33.82b5b4","x":150,"y":580,"wires":[["844d0a60.59e128"]]},{"id":"bef9e569.e5f048","type":"file","z":"850b8bf6.c34fe8","name":"","filename":"/home/pi/mysensors.log","appendNewline":true,"createDir":false,"overwriteFile":"false","x":550,"y":620,"wires":[[]]},{"id":"5f4cad33.82b5b4","type":"mqtt-broker","z":"","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","willTopic":"","willQos":"0","willPayload":""}]
        

        By the way, how are you using the serial gateway? I've tried it before but couldn't send and receive messages at the same time as the serial port was in use by either the sending or the receiving flow.

        J Offline
        J Offline
        jgrieco
        wrote on last edited by jgrieco
        #3

        Thanks! I actually figured out the javascript and made a function exactly like this one. I am using the serial port just to receive data currently, so i only have one flow. I am using it with relatively default settings:

        [{"id":"7fc64f75.67286","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"1f992795.acb908","type":"serial in","z":"7fc64f75.67286","name":"Seral Port","serial":"d11eb944.68b128","x":60,"y":620,"wires":[["19c6b2bb.f878dd"]]},{"id":"19c6b2bb.f878dd","type":"function","z":"7fc64f75.67286","name":"Get_Datetime","func":"var now = new Date()\nvar x = now.toLocaleString()\n\n\nmsg.datetime = x\n\nreturn msg;\n\n","outputs":1,"noerr":0,"x":260,"y":280,"wires":[["1197d270.3caa9e"]]},{"id":"1197d270.3caa9e","type":"function","z":"7fc64f75.67286","name":"Merge_datetime and payload","func":"msg.payload = msg.datetime+ ' ;' + msg.payload\nreturn msg;","outputs":1,"noerr":0,"x":510,"y":280,"wires":[["e44f08e9.aae3f8"]]},{"id":"e44f08e9.aae3f8","type":"file","z":"7fc64f75.67286","name":"","filename":"/home/pi/Desktop/dataNodered_nov5.csv","appendNewline":false,"createDir":false,"overwriteFile":"false","x":840,"y":280,"wires":[[]]},{"id":"d11eb944.68b128","type":"serial-port","z":"","serialport":"/dev/ttyACM0","serialbaud":"115200","databits":"8","parity":"none","stopbits":"1","newline":"\\n","bin":"false","out":"char","addchar":false,"responsetimeout":"10000"}]
        

        I am about to begin to look into sending my nodes data. I want to find a way for them to sync up their data transmissions, and will will be looking into how to send them a variable with a customized sleep time in order to align their transmissions. How have you send data to the nodes via node red in the past? Will I run into trouble once i create a second data flow?

        1 Reply Last reply
        0
        • electrikE Offline
          electrikE Offline
          electrik
          wrote on last edited by
          #4

          I'm using a mqtt gateway to send data to the nodes. It is possible you will run into the same issues I guess but that should be easy to test. Just send data on a regular basis to the out node and try to receive at the same time.

          I use the VAR_1 type in the message of I'm sending data from node red to a node

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          20

          Online

          11.7k

          Users

          11.2k

          Topics

          113.1k

          Posts


          Copyright 2025 TBD   |   Forum Guidelines   |   Privacy Policy   |   Terms of Service
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • MySensors
          • OpenHardware.io
          • Categories
          • Recent
          • Tags
          • Popular