Update RPi3 DateTime by WiFi

Hi,
In my RPI3 projet, I use this node to display DateTime in ui toolbar. When I use RPi3 WiFi remote access, ui DateTime is the PC/Smartphone DateTime. Normal.
Now I would like update RPi3 DateTime with PC/Smartphone DateTime. Is it possible?
I can't use RTC or Ethernet in this project.
Thank you for yours answers.
Vincent
NodeRed beginner

Hi Vincent,

And the Wifi connection is not on the internet ?
If not check this solution that syncs the time of a Pi from a users Browser.

Hello UnborN,
Thanks a lot for your answer. It's usefull. I just need to change timeStamp to string DateTime and I will be a happy man ; )
Vincent

I changed the TimeStamp in String with $fromMillis but I have a problem with TimeZone.
Vincent

Hi Vincent,

You can use the Moment node that is very useful in converting datetime in correct format including timezones.

Once you have the datetime in the correct format you send it to an Exec node with the command
sudo timedatectl set-time and its important to have the Append checked so the dt will be appended to it.

image

I havent tested it because i dont have a RPi setup now but here's a more complete flow :

[{"id":"4fbd5cb6.955534","type":"ui_template","z":"ac0f61dd.69e26","group":"c205e26f.6865a","name":"","order":0,"width":"0","height":"0","format":"  <html>\n  <head>\n  <title>Time</title>\n  <script type=\"text/javascript\"> \n  \n  var date = new Date().toString();\n  document.getElementById('ct').innerHTML = date;\n  \n  this.scope.sendDate = function() {\n      console.log(\"in function\")\n   // return {payload: new Date().toString()}; // .valueOf() \n    return {payload: new Date().valueOf()}; // \n  }\n \n</script>\n  \n  </head>\n\n  <body>\n  <div id='ct' style=\"margin-bottom: 40px;\"></div>\n  <br>\n  <br>\n<md-button ng-click=\"send(sendDate())\">\n    Send time to Node-red\n</md-button>\n\n  </body>\n  </html>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":360,"y":820,"wires":[["193cbc9b.13c77b"]]},{"id":"1b156e9f.e82be9","type":"debug","z":"ac0f61dd.69e26","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1050,"y":820,"wires":[]},{"id":"193cbc9b.13c77b","type":"moment","z":"ac0f61dd.69e26","name":"","topic":"","input":"payload","inputType":"msg","inTz":"Europe/London","adjAmount":0,"adjType":"days","adjDir":"add","format":"YYYY-MM-DD HH:mm:ss","locale":"en-GB","output":"payload","outputType":"msg","outTz":"Europe/London","x":580,"y":820,"wires":[["1fdc2584.f79f72","4cd3096d.55811"]]},{"id":"1fdc2584.f79f72","type":"exec","z":"ac0f61dd.69e26","command":"sudo timedatectl set-time","addpay":true,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"","x":850,"y":820,"wires":[["1b156e9f.e82be9"],[],[]]},{"id":"aeee1b9e.304af","type":"comment","z":"ac0f61dd.69e26","name":"sudo timedatectl set-time 'yyyy-mm-dd hh:mm:ss'","info":"","x":860,"y":740,"wires":[]},{"id":"4cd3096d.55811","type":"debug","z":"ac0f61dd.69e26","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":720,"y":920,"wires":[]},{"id":"c205e26f.6865a","type":"ui_group","name":"Default","tab":"20af7edd.a91fca","order":1,"disp":true,"width":"12","collapse":false},{"id":"20af7edd.a91fca","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]

I had problem to download catalogue. Without moment I created this code:

[
    {
        "id": "c4c4b1e9.2e3c8",
        "type": "exec",
        "z": "1e369b26.8cd9e5",
        "command": "sudo date -s ",
        "addpay": true,
        "append": "",
        "useSpawn": "false",
        "timer": "1",
        "oldrc": false,
        "name": "",
        "x": 1050,
        "y": 100,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "7a163e24.fc81b",
        "type": "ui_template",
        "z": "1e369b26.8cd9e5",
        "group": "6a269076.815ee",
        "name": "Clock Update",
        "order": 0,
        "width": "0",
        "height": "0",
        "format": "<html>\n  <head>\n  <title>Time</title>\n  <script type=\"text/javascript\"> \n  \n  var date = new Date().toString();\n  document.getElementById('ct').innerHTML = date;\n  \n  this.scope.sendDate = function() {\n    console.log(\"in function\");\n    //return {payload: new Date().toString()};\n    //return {payload: new Date().valueOf()};\n    \n    var d = new Date();\n    var year, month, day, hours, minutes, seconds; \n\n    year = 1900+d.getYear();\n    year = year.toString();\n    month = 1+d.getMonth();\n    month = month.toString();\n    day = d.getDate();\n    day = day.toString();\n    hours = d.getHours();\n    hours = hours.toString();\n    minutes = d.getMinutes();\n    minutes = minutes.toString();\n    seconds = d.getSeconds();\n    seconds = seconds.toString();\n\n    return {payload: \"'\"+year+\"/\"+month+\"/\"+day+\" \"+hours+\":\"+minutes+\":\"+seconds+\"'\"};\n  }\n  </script>\n  </head>\n\n  <body>\n  <div id='ct' style=\"margin-bottom: 40px;\"></div>\n  <br>\n  <br>\n  <md-button ng-click=\"send(sendDate())\">\n    Send time to Node-red\n  </md-button>\n  </body>\n  \n</html>",
        "storeOutMessages": true,
        "fwdInMessages": true,
        "resendOnRefresh": true,
        "templateScope": "local",
        "x": 860,
        "y": 40,
        "wires": [
            [
                "c4c4b1e9.2e3c8",
                "b13be7c1.360b58"
            ]
        ]
    },
    {
        "id": "b13be7c1.360b58",
        "type": "debug",
        "z": "1e369b26.8cd9e5",
        "name": "DateUser",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "targetType": "full",
        "x": 1040,
        "y": 40,
        "wires": []
    },
    {
        "id": "6a269076.815ee",
        "type": "ui_group",
        "z": "",
        "name": "Clock",
        "tab": "dab605fb.c8a088",
        "order": 1,
        "disp": true,
        "width": "6",
        "collapse": false
    },
    {
        "id": "dab605fb.c8a088",
        "type": "ui_tab",
        "z": "",
        "name": "Update Clock",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": false
    }
]

Vincent

Maybe this ?

var d = new Date();
var year, month, day, hours, minutes, seconds; 

    year = d.getFullYear();

    month = 1+d.getMonth();
    month = month < 10 ? "0" + month : month  // add leading 0
   
    day = d.getDate();
    day = day < 10 ? "0" + day : day  // add leading 0
   
    hours = d.getHours();
    hours = hours < 10 ? "0" + hours : hours  // add leading 0
   
    minutes = d.getMinutes();
    minutes = minutes < 10 ? "0" + minutes : minutes  // add leading 0
  
    seconds = d.getSeconds();
    seconds = seconds < 10 ? "0" + seconds : seconds  // add leading 0
   

    return {payload: `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`};

I created almost the same code. See above:

var d = new Date();
var year, month, day, hours, minutes, seconds;
year = 1900+d.getYear();
year = year.toString();
month = 1+d.getMonth();
month = month.toString();
day = d.getDate();
day = day.toString();
hours = d.getHours();
hours = hours.toString();
minutes = d.getMinutes();
minutes = minutes.toString();
seconds = d.getSeconds();
seconds = seconds.toString();
return {payload: \"'\"+year+\"/\"+month+\"/\"+day+\" \"+hours+\":\"+minutes+\":\"+seconds+\"'\"};

And Exec sudo date -s + payload

Thanks a lot UnborN

Vincent

1 Like

Another way of converting the Datetime is using a Change node with a Jsonata expression
the Moment library is loaded by default in Jsonata (forgot about it)

$moment(msg.payload).format("YYYY-MM-DD HH:mm:ss")

image

Sample Flow :

[{"id":"1b56d6f7.0c1151","type":"change","z":"ac0f61dd.69e26","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$moment(msg.payload).format(\"YYYY-MM-DD HH:mm:ss\")\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":1040,"wires":[["4cd3096d.55811"]]},{"id":"77b16f17.c1b88","type":"debug","z":"ac0f61dd.69e26","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":350,"y":980,"wires":[]},{"id":"4fbd5cb6.955534","type":"ui_template","z":"ac0f61dd.69e26","group":"c205e26f.6865a","name":"","order":0,"width":"0","height":"0","format":"  <html>\n  <head>\n  <title>Time</title>\n  <script type=\"text/javascript\"> \n  \n  var date = new Date().toString();\n  document.getElementById('ct').innerHTML = date;\n  \n  this.scope.sendDate = function() {\n      console.log(\"in function\")\n   // return {payload: new Date().toString()}; // .valueOf() \n    return {payload: new Date().valueOf()}; // \n  }\n \n</script>\n  \n  </head>\n\n  <body>\n  <div id='ct' style=\"margin-bottom: 40px;\"></div>\n  <br>\n  <br>\n<md-button ng-click=\"send(sendDate())\">\n    Send time to Node-red\n</md-button>\n\n  </body>\n  </html>","storeOutMessages":true,"fwdInMessages":true,"resendOnRefresh":true,"templateScope":"local","x":220,"y":1040,"wires":[["77b16f17.c1b88","1b56d6f7.0c1151"]]},{"id":"4cd3096d.55811","type":"debug","z":"ac0f61dd.69e26","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":570,"y":1040,"wires":[]},{"id":"c205e26f.6865a","type":"ui_group","name":"Default","tab":"20af7edd.a91fca","order":1,"disp":true,"width":"12","collapse":false},{"id":"20af7edd.a91fca","type":"ui_tab","name":"Home","icon":"dashboard","disabled":false,"hidden":false}]
1 Like