# Set time function on dashboard

**URL:** <https://discourse.nodered.org/t/set-time-function-on-dashboard/24008>\
**Category:** General\
**Created:** [1 April 2020 06:08 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008 "2020-04-01T06:08:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [1 April 2020 06:08 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/1 "2020-04-01T06:08:36Z")

</div>

Hi, i'm trying to make a function to select an hour to turn off the lights at 19:00 the only method that i used before is a delay but i was looking and i found the get mydate get hour, there is way to use a function like this.

var set hour to turn on = new Date("April 2, 2020 01:15:00");  
if get Dte hour == set hour to turn on  
msg.payload = "on";  
return msg;

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 April 2020 06:14 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/2 "2020-04-01T06:14:18Z")

</div>

So you want a GUI to select the hour?

That will require a `dashboard` to be used.  
(Not that difficult)

The example (I'm guessing) is just that. There is no sign of `19:00` in it.

Why do you want to turn the lights `OFF` at 19:00? That's around sunset and I would have thought you would want them to be turned `ON`.

How much of a flow do you have?

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [1 April 2020 20:03 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/3 "2020-04-01T20:03:06Z")

</div>

@Trying_to_learn i know this is what i have until now i kinda everithing that i need but i need to join it.

```auto
[{"id":"763e3b57.a7e4a4","type":"ui_text","z":"217dd16.38f122e","group":"141b826d.d51e0e","order":2,"width":"","height":"","name":"Time:","label":"Time:","format":"{{msg.payload}}","layout":"","x":1050,"y":640,"wires":[]},{"id":"577c9ede.3ec36","type":"function","z":"217dd16.38f122e","name":"Calculate time","func":"global.set(msg.topic,msg.payload);\nvar hour = global.get(\"timehour\");\nvar minute = global.get(\"timeminute\");\nif (hour!==undefined && minute!==undefined) {\n var mytime = new Date();\n mytime.setHours(hour, minute, 0);\n mytime.setMilliseconds(0);\n mytime.setFullYear(2000,01,01);\n global.set(\"start\",mytime.getTime());\n}\nmsg.payload = hour + \":\" + minute;\nreturn msg;","outputs":1,"noerr":2,"x":868,"y":641,"wires":[["763e3b57.a7e4a4"]]},{"id":"5061166f.4dd8d8","type":"ui_dropdown","z":"217dd16.38f122e","name":"Hour","label":"Hour","tooltip":"","place":"","group":"141b826d.d51e0e","order":3,"width":0,"height":0,"passthru":true,"options":[{"label":"00","value":"0","type":"str"},{"label":"01","value":"1","type":"str"},{"label":"02","value":"2","type":"str"},{"label":"03","value":"3","type":"str"},{"label":"04","value":"4","type":"str"},{"label":"05","value":"5","type":"str"},{"label":"06","value":"6","type":"str"},{"label":"07","value":"7","type":"str"},{"label":"08","value":"8","type":"str"},{"label":"09","value":"9","type":"str"},{"label":"10","value":"10","type":"str"},{"label":"11","value":"11","type":"str"},{"label":"12","value":"12","type":"str"},{"label":"13","value":"13","type":"str"},{"label":"14","value":"14","type":"str"},{"label":"15","value":"15","type":"str"},{"label":"16","value":"16","type":"str"},{"label":"17","value":"17","type":"str"},{"label":"18","value":"18","type":"str"},{"label":"19","value":"19","type":"str"},{"label":"20","value":"20","type":"str"},{"label":"21","value":"21","type":"str"},{"label":"22","value":"22","type":"str"},{"label":"23","value":"23","type":"str"}],"payload":"","topic":"timehour","x":658,"y":627,"wires":[["577c9ede.3ec36"]]},{"id":"bcbea3a4.17f92","type":"ui_dropdown","z":"217dd16.38f122e","name":"Minute","label":"Minute","tooltip":"","place":"","group":"141b826d.d51e0e","order":4,"width":0,"height":0,"passthru":true,"options":[{"label":"00","value":"0","type":"str"},{"label":"01","value":"1","type":"str"},{"label":"02","value":"2","type":"str"},{"label":"03","value":"3","type":"str"},{"label":"04","value":"4","type":"str"},{"label":"05","value":"5","type":"str"},{"label":"06","value":"6","type":"str"},{"label":"07","value":"7","type":"str"},{"label":"08","value":"8","type":"str"},{"label":"09","value":"9","type":"str"},{"label":"10","value":"10","type":"str"},{"label":"11","value":"11","type":"str"},{"label":"12","value":"12","type":"str"},{"label":"13","value":"13","type":"str"},{"label":"14","value":"14","type":"str"},{"label":"15","value":"15","type":"str"},{"label":"16","value":"16","type":"str"},{"label":"17","value":"17","type":"str"},{"label":"18","value":"18","type":"str"},{"label":"19","value":"19","type":"str"},{"label":"20","value":"20","type":"str"},{"label":"21","value":"21","type":"str"},{"label":"22","value":"22","type":"str"},{"label":"23","value":"23","type":"str"},{"label":"24","value":"24","type":"str"},{"label":"25","value":"25","type":"str"},{"label":"26","value":"26","type":"str"},{"label":"27","value":"27","type":"str"},{"label":"28","value":"28","type":"str"},{"label":"29","value":"29","type":"str"},{"label":"30","value":"30","type":"str"},{"label":"31","value":"31","type":"str"},{"label":"32","value":"32","type":"str"},{"label":"33","value":"33","type":"str"},{"label":"34","value":"34","type":"str"},{"label":"35","value":"35","type":"str"},{"label":"36","value":"36","type":"str"},{"label":"37","value":"37","type":"str"},{"label":"38","value":"38","type":"str"},{"label":"39","value":"39","type":"str"},{"label":"40","value":"40","type":"str"},{"label":"41","value":"41","type":"str"},{"label":"42","value":"42","type":"str"},{"label":"43","value":"43","type":"str"},{"label":"44","value":"44","type":"str"},{"label":"45","value":"45","type":"str"},{"label":"46","value":"46","type":"str"},{"label":"47","value":"47","type":"str"},{"label":"48","value":"48","type":"str"},{"label":"49","value":"49","type":"str"},{"label":"50","value":"50","type":"str"},{"label":"51","value":"51","type":"str"},{"label":"52","value":"52","type":"str"},{"label":"53","value":"53","type":"str"},{"label":"54","value":"54","type":"str"},{"label":"55","value":"55","type":"str"},{"label":"56","value":"56","type":"str"},{"label":"57","value":"57","type":"str"},{"label":"58","value":"58","type":"str"},{"label":"59","value":"59","type":"str"}],"payload":"","topic":"timeminute","x":657,"y":670,"wires":[["577c9ede.3ec36"]]},{"id":"cfc3c08f.ca39f","type":"ui_switch","z":"217dd16.38f122e","name":"Lamp","label":"Lamp","tooltip":"","group":"615b9ae0.a9d4c4","order":1,"width":0,"height":0,"passthru":true,"decouple":"false","topic":"room/lamp","style":"","onvalue":"on","onvalueType":"str","onicon":"","oncolor":"","offvalue":"off","offvalueType":"str","officon":"","offcolor":"","x":890,"y":760,"wires":[["be71ba07.d9ec58"]]},{"id":"be71ba07.d9ec58","type":"mqtt out","z":"217dd16.38f122e","name":"Lamp","topic":"room/lamp","qos":"2","retain":"","broker":"865f61ef.13d","x":1150,"y":760,"wires":[]},{"id":"e0d7f45d.3da998","type":"function","z":"217dd16.38f122e","name":"Sleep Timer","func":"\nvar value = context.get('value') || 60000;\n\nif (msg.topic == \"value\")\n{\n value = msg.payload * 60000;\n context.set('value',value); \n}\n\nelse if (msg.topic == \"start\")\n{\n\nvar timer = setTimeout(function(){msg.payload = \"off\";node.send([msg,{ payload: \"Ende\" }]);var countdownstop = context.get('countdown');clearInterval(countdownstop)},value);\ncontext.set('timer',timer); \n\nvar count = value/60000;\n//count = count -1 ;\nmsg.payload=count;\nnode.send([null,msg]);\nvar countdown = setInterval(function() {count=count-1;msg.payload=count; node.send([null,msg]) }, 60000);\ncontext.set('countdown',countdown);\n\n}\n\nelse if (msg.topic == \"stop\")\n{\nvar timerstop = context.get('timer')\nclearTimeout(timerstop);\n\nvar countdownstop = context.get('countdown')\nclearInterval(countdownstop);\n\n}\n\n\n","outputs":"2","noerr":0,"x":870,"y":900,"wires":[["5080012f.84d6e"],["3217a67a.49beca"]]},{"id":"e28ce442.b64738","type":"ui_button","z":"217dd16.38f122e","name":"","group":"dd70e446.dbe258","order":3,"width":"3","height":"1","passthru":false,"label":"Start","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"start","x":630,"y":880,"wires":[["e0d7f45d.3da998"]]},{"id":"fb5725f2.1d12c8","type":"ui_numeric","z":"217dd16.38f122e","name":"","label":"Sleep timer in minutes","tooltip":"","group":"dd70e446.dbe258","order":2,"width":"0","height":"0","wrap":false,"passthru":true,"topic":"value","format":"{{value}}","min":"1","max":"300","step":1,"x":670,"y":960,"wires":[["e0d7f45d.3da998"]]},{"id":"f5fd6fd8.5748","type":"ui_button","z":"217dd16.38f122e","name":"","group":"dd70e446.dbe258","order":4,"width":"3","height":"1","passthru":false,"label":"Stop","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"stop","x":630,"y":920,"wires":[["e0d7f45d.3da998"]]},{"id":"3217a67a.49beca","type":"ui_text","z":"217dd16.38f122e","group":"dd70e446.dbe258","order":5,"width":0,"height":0,"name":"","label":"Time left","format":"{{msg.payload}} Minuten","layout":"row-spread","x":1040,"y":920,"wires":[]},{"id":"5080012f.84d6e","type":"debug","z":"217dd16.38f122e","name":"","active":true,"console":"false","complete":"false","x":1050,"y":860,"wires":[]},{"id":"141b826d.d51e0e","type":"ui_group","z":"","name":"Start Time","tab":"24e77315.a6b31c","order":1,"disp":true,"width":"6"},{"id":"615b9ae0.a9d4c4","type":"ui_group","z":"","name":"Lamp","tab":"8f4c42b3.c0e6","order":2,"disp":true,"width":"6","collapse":false},{"id":"865f61ef.13d","type":"mqtt-broker","z":"","name":"","broker":"192.168.43.47","port":"1883","clientid":"TEST","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"dd70e446.dbe258","type":"ui_group","z":"","name":"Sockets","tab":"8563fc7f.df68","disp":true,"width":"6","collapse":false},{"id":"24e77315.a6b31c","type":"ui_tab","z":"","name":"Irrigation","icon":"call_split","order":2},{"id":"8f4c42b3.c0e6","type":"ui_tab","z":"","name":"dropdown","icon":"dashboard","order":1,"disabled":false,"hidden":false},{"id":"8563fc7f.df68","type":"ui_tab","z":"","name":"bedroom","icon":"fa-bed","disabled":false,"hidden":false}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 April 2020 20:29 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/4 "2020-04-01T20:29:31Z")

</div>

Well, it imports - eventually.

The first `function` node is a problem. It shows as having an error.

The _problem_ is that when I open it and look at the code, none of the lines have an error beside them.  
Usually the line with an error is shown clearly.

I'll look at it and see what I can make of the flow.

Ok, this line:  
`mytime.setFullYear(2000,01,01);`

It seems to be creating an error.

Another _Problem_ is in the other `function` node:

```auto
var value = context.get('value') || 60000;

if (msg.topic == "value")
{
  value = msg.payload * 60000;
  context.set('value',value);  
}

```

That is asking for trouble.  
It gets the `value` in the first line.  
Then it checks if the message changes the _value_.

It probably would work a lot better if you had it this way:

```auto
if (msg.topic == "value")
{
  value = msg.payload * 60000;
  context.set('value',value);  
}

var value = context.get('value') || 60000;

```

I am struggling to see exactly what you are wanting - though you have said it - I am just missing something.

You have 3 `dashboard` (thanks it took me a while to find all the buttons, etc).  
Why?  
If you are wanting them all to work together, I am - again - missing something.

I like some of the _magic_ you use in the `sleep timer` node.  
But the `node.send([null,msg]);` command is beyond my scope.

I think someone else will help you better than I can.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [1 April 2020 20:40 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/5 "2020-04-01T20:40:11Z")

</div>

Would something like this help?

[https://flows.nodered.org/flow/79a66966a6cc655a827872a4af794b94](https://flows.nodered.org/flow/79a66966a6cc655a827872a4af794b94)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [1 April 2020 21:41 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/6 "2020-04-01T21:41:46Z")

</div>

This is a crude flow I put together.

I hope this is of some use to you.

I haven't connected things up. But the `ACTION!` message is when things match.

```auto
[{"id":"2fd81001.aef0c","type":"ui_text","z":"b4f1ccd4.5e8d1","group":"45aba177.06a458","order":2,"width":"","height":"","name":"Time:","label":"Time:","format":"{{msg.payload}}","layout":"","x":580,"y":120,"wires":[]},{"id":"d2bbab5b.2ef68","type":"function","z":"b4f1ccd4.5e8d1","name":"Calculate time","func":"global.set(msg.topic,msg.payload);\nvar hour = global.get(\"timehour\");\nvar minute = global.get(\"timeminute\");\nif (hour!==undefined && minute!==undefined) {\n //\n global.set(\"THE_TIME\",hour+\":\"+minute);\n //\n var mytime = new Date();\n mytime.setHours(hour, minute, 0);\n mytime.setMilliseconds(0);\n// mytime.setFullYear(2000,01,01);\n global.set(\"start\",mytime.getTime());\n}\nmsg.payload = hour + \":\" + minute;\nreturn msg;","outputs":1,"noerr":0,"x":390,"y":120,"wires":[["2fd81001.aef0c"]]},{"id":"a5506d01.92985","type":"ui_dropdown","z":"b4f1ccd4.5e8d1","name":"Hour","label":"Hour","tooltip":"","place":"","group":"45aba177.06a458","order":3,"width":0,"height":0,"passthru":true,"options":[{"label":"00","value":"0","type":"str"},{"label":"01","value":"1","type":"str"},{"label":"02","value":"2","type":"str"},{"label":"03","value":"3","type":"str"},{"label":"04","value":"4","type":"str"},{"label":"05","value":"5","type":"str"},{"label":"06","value":"6","type":"str"},{"label":"07","value":"7","type":"str"},{"label":"08","value":"8","type":"str"},{"label":"09","value":"9","type":"str"},{"label":"10","value":"10","type":"str"},{"label":"11","value":"11","type":"str"},{"label":"12","value":"12","type":"str"},{"label":"13","value":"13","type":"str"},{"label":"14","value":"14","type":"str"},{"label":"15","value":"15","type":"str"},{"label":"16","value":"16","type":"str"},{"label":"17","value":"17","type":"str"},{"label":"18","value":"18","type":"str"},{"label":"19","value":"19","type":"str"},{"label":"20","value":"20","type":"str"},{"label":"21","value":"21","type":"str"},{"label":"22","value":"22","type":"str"},{"label":"23","value":"23","type":"str"}],"payload":"","topic":"timehour","x":188,"y":107,"wires":[["d2bbab5b.2ef68"]]},{"id":"3ec08be8.0abf1c","type":"ui_dropdown","z":"b4f1ccd4.5e8d1","name":"Minute","label":"Minute","tooltip":"","place":"","group":"45aba177.06a458","order":4,"width":0,"height":0,"passthru":true,"options":[{"label":"00","value":"0","type":"str"},{"label":"01","value":"1","type":"str"},{"label":"02","value":"2","type":"str"},{"label":"03","value":"3","type":"str"},{"label":"04","value":"4","type":"str"},{"label":"05","value":"5","type":"str"},{"label":"06","value":"6","type":"str"},{"label":"07","value":"7","type":"str"},{"label":"08","value":"8","type":"str"},{"label":"09","value":"9","type":"str"},{"label":"10","value":"10","type":"str"},{"label":"11","value":"11","type":"str"},{"label":"12","value":"12","type":"str"},{"label":"13","value":"13","type":"str"},{"label":"14","value":"14","type":"str"},{"label":"15","value":"15","type":"str"},{"label":"16","value":"16","type":"str"},{"label":"17","value":"17","type":"str"},{"label":"18","value":"18","type":"str"},{"label":"19","value":"19","type":"str"},{"label":"20","value":"20","type":"str"},{"label":"21","value":"21","type":"str"},{"label":"22","value":"22","type":"str"},{"label":"23","value":"23","type":"str"},{"label":"24","value":"24","type":"str"},{"label":"25","value":"25","type":"str"},{"label":"26","value":"26","type":"str"},{"label":"27","value":"27","type":"str"},{"label":"28","value":"28","type":"str"},{"label":"29","value":"29","type":"str"},{"label":"30","value":"30","type":"str"},{"label":"31","value":"31","type":"str"},{"label":"32","value":"32","type":"str"},{"label":"33","value":"33","type":"str"},{"label":"34","value":"34","type":"str"},{"label":"35","value":"35","type":"str"},{"label":"36","value":"36","type":"str"},{"label":"37","value":"37","type":"str"},{"label":"38","value":"38","type":"str"},{"label":"39","value":"39","type":"str"},{"label":"40","value":"40","type":"str"},{"label":"41","value":"41","type":"str"},{"label":"42","value":"42","type":"str"},{"label":"43","value":"43","type":"str"},{"label":"44","value":"44","type":"str"},{"label":"45","value":"45","type":"str"},{"label":"46","value":"46","type":"str"},{"label":"47","value":"47","type":"str"},{"label":"48","value":"48","type":"str"},{"label":"49","value":"49","type":"str"},{"label":"50","value":"50","type":"str"},{"label":"51","value":"51","type":"str"},{"label":"52","value":"52","type":"str"},{"label":"53","value":"53","type":"str"},{"label":"54","value":"54","type":"str"},{"label":"55","value":"55","type":"str"},{"label":"56","value":"56","type":"str"},{"label":"57","value":"57","type":"str"},{"label":"58","value":"58","type":"str"},{"label":"59","value":"59","type":"str"}],"payload":"","topic":"timeminute","x":187,"y":150,"wires":[["d2bbab5b.2ef68"]]},{"id":"324e453c.9589aa","type":"inject","z":"b4f1ccd4.5e8d1","name":"","topic":"","payload":"","payloadType":"date","repeat":"30","crontab":"","once":false,"onceDelay":0.1,"x":180,"y":210,"wires":[["50254f02.fa4858"]]},{"id":"50254f02.fa4858","type":"moment","z":"b4f1ccd4.5e8d1","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"H:m","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":380,"y":210,"wires":[["4eba28c7.84f3a8"]]},{"id":"4eba28c7.84f3a8","type":"switch","z":"b4f1ccd4.5e8d1","name":"","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"THE_TIME","vt":"global"}],"checkall":"true","repair":false,"outputs":1,"x":560,"y":210,"wires":[["a678fe7b.a59988"]]},{"id":"a678fe7b.a59988","type":"change","z":"b4f1ccd4.5e8d1","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"ACTION!","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":210,"wires":[["b94155f1.585998"]]},{"id":"b94155f1.585998","type":"debug","z":"b4f1ccd4.5e8d1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":890,"y":210,"wires":[]},{"id":"45aba177.06a458","type":"ui_group","z":"","name":"Start Time","tab":"9208c511.b5722","order":1,"disp":true,"width":"6"},{"id":"9208c511.b5722","type":"ui_tab","z":"","name":"Irrigation","icon":"call_split","order":2}]

```

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [2 April 2020 05:16 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/7 "2020-04-02T05:16:13Z")

</div>

@Trying_to_learn sorry i will try to explain it better.  
What i want is in my dashboard choose the hour that i want to turn on and turn off the lights so basically in my function i try to set the hour and after the function will send for example at 19:00 a msg.payload to the mqtt out node with "on" and then at 29:59 a msg.payload to the mqtt out node with "off" and then again at 06:00 am a msg.payload with "on" to turn on the lights.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [2 April 2020 05:37 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/8 "2020-04-02T05:37:42Z")

</div>

Very simple nice idea. But to remarks:  
You will have to run two instances of the whole flow for on and off right now.  
By using an interval of 30s on the inject to check the time you have the possibility that the command could be executed twice as the accuracy of the saved time is only down to one minute and so it’s true for a whole 60s.  
Best regards Johannes

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [2 April 2020 05:39 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/9 "2020-04-02T05:39:02Z")

</div>

Hey @Steve-Mcl it looks really similar i will see the functions.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [2 April 2020 05:41 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/10 "2020-04-02T05:41:00Z")

</div>

can you give me an example of that @JGKK please?

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [2 April 2020 07:29 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/11 "2020-04-02T07:29:33Z")

</div>

```auto
[{"id":"8233c164.1bcb58","type":"function","z":"f954564f.03e718","name":"check on & off","func":"const time = new Date();\nconst hour = time.getHours();\nconst minute = time.getMinutes();\nconst ontime = flow.get(\"ontime\");\nconst offtime = flow.get(\"offtime\");\nif(ontime[0] == hour && ontime[1] == minute){\n msg.payload = \"on\";\n return msg;\n} else if (offtime[0] == hour && offtime[1] == minute){\n msg.payload = \"off\";\n return msg;\n}\nreturn null;","outputs":1,"noerr":0,"x":460,"y":3360,"wires":[["37d3efaa.0c2018"]]},{"id":"ef2b9f11.6427a","type":"inject","z":"f954564f.03e718","name":"","topic":"","payload":"","payloadType":"date","repeat":"60","crontab":"","once":false,"onceDelay":0.1,"x":280,"y":3360,"wires":[["8233c164.1bcb58"]]},{"id":"e01a7a53.2a8e7","type":"ui_text_input","z":"f954564f.03e718","name":"","label":"set on time","tooltip":"","group":"7d6a9a85.b4af24","order":1,"width":0,"height":0,"passthru":true,"mode":"time","delay":"0","topic":"","x":270,"y":3220,"wires":[["87736cab.201a8"]]},{"id":"d5826c7e.4f63a","type":"debug","z":"f954564f.03e718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":3220,"wires":[]},{"id":"87736cab.201a8","type":"function","z":"f954564f.03e718","name":"format","func":"const ms = msg.payload;\nlet result = [];\nconst intm = String(ms/3600000).split(\".\");\nresult.push(parseFloat(intm[0]));\nresult.push(Math.round(parseFloat(\"0.\"+intm[1])*60));\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":3220,"wires":[["a21d0407.f1c8"]]},{"id":"a21d0407.f1c8","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"set","p":"ontime","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":3220,"wires":[["d5826c7e.4f63a"]]},{"id":"4866e47a.c892fc","type":"ui_text_input","z":"f954564f.03e718","name":"","label":"set off time","tooltip":"","group":"7d6a9a85.b4af24","order":2,"width":0,"height":0,"passthru":true,"mode":"time","delay":"0","topic":"","x":270,"y":3280,"wires":[["a8906688.b8525"]]},{"id":"f0d7b55b.85e658","type":"debug","z":"f954564f.03e718","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":790,"y":3280,"wires":[]},{"id":"a8906688.b8525","type":"function","z":"f954564f.03e718","name":"format","func":"const ms = msg.payload;\nlet result = [];\nconst intm = String(ms/3600000).split(\".\");\nresult.push(parseFloat(intm[0]));\nresult.push(Math.round(parseFloat(\"0.\"+intm[1])*60));\nmsg.payload = result;\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":3280,"wires":[["5d0757e5.6e6da"]]},{"id":"5d0757e5.6e6da","type":"change","z":"f954564f.03e718","name":"","rules":[{"t":"set","p":"offtime","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":600,"y":3280,"wires":[["f0d7b55b.85e658"]]},{"id":"37d3efaa.0c2018","type":"ui_text","z":"f954564f.03e718","group":"7d6a9a85.b4af24","order":3,"width":0,"height":0,"name":"","label":"state:","format":"{{msg.payload}}","layout":"row-spread","x":630,"y":3360,"wires":[]},{"id":"7d6a9a85.b4af24","type":"ui_group","z":"","name":"Start Time","tab":"b83efc1a.370b88","order":1,"disp":true,"width":"6","collapse":false},{"id":"b83efc1a.370b88","type":"ui_tab","z":"","name":"Irrigation","icon":"call_split","order":2}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/9/59a2612dddf0a71199e3c13be444b198159eebd6.jpeg) ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0cee74acc41a1a021e96c59e3b2365ce622ea3df.jpeg)  
This flow is a very simple implementation of what you want i think. The flow that @Steve-Mcl linked is off course way more powerful and sophisticated than this. This is very basic. It uses text input nodes in time picker mode to set two flow variables. As the input node returns those in milliseconds since midnight we have to do some formatting in a function node. Basically we divide by the number of milliseconds in an hour and split the resulting decimal at the ".". Everything before the decimal point is the hour and we push this to an array. We replace this with a 0 for getting the minutes. We just need to multiply this 0.something by 60 and round it using Math.round() to get the minutes. We push this to the results array too. Now we have an array which has the hours and minutes from the input and this is saved to the time variable. One for the on time and one for the off time.  
It than checks every minute if it is the on or the off time in a function node. It gets the hours and minutes now from a Date object using the getHours() and getMinutes() Methods. It than compares those to the Hours and Minutes in the flow variables and if true returns the corresponding payload.  
Only caveat is that it can take up to 59 seconds for the payload to be send when it’s the set time as we only check once a minute and when this check occurs depends on when you deploy the flow as the check intervall will start at this time.  
Best regards Johannes

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [2 April 2020 20:51 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/12 "2020-04-02T20:51:20Z")

</div>

Johannes thanks a lot that really help me. I'm stilll touching the code.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 April 2020 20:38 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/13 "2020-04-03T20:38:24Z")

</div>

Just saying.....

The flow I posted was pretty much the same, but rather than have two `change` nodes to set the variables (flow/global) mine did it with one and used the `topic` to set the name.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [3 April 2020 21:00 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/14 "2020-04-03T21:00:32Z")

</div>

Sorry i didn’t mean to offend you in any way and as i said above i think your idea is very simple and clean. But i saw the two problems i mentioned above when i answered you and wrote when asked a streamlined version where there wasn’t the problem with double triggering at the time and showing the use of the text input in timepicker mode and checking for both on or off in one function. I didn’t mean to take away from your original idea.  
My apologizes if it looked that way.  
Johannes

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 April 2020 21:04 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/15 "2020-04-03T21:04:00Z")

</div>

That's all right. I just was confused why you did it with two nodes.

Yes, I can get that you wanted it easier to understand.

My brain seems to have problems with parsing information and keeping things in memory just now.

It's ok to post different ideas/way. I think I do it a lot.

I shall have to go back and check/update my intake of what you said, because as I said: I don't seem to remember it.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [3 April 2020 21:06 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/16 "2020-04-03T21:06:26Z")

</div>

i use two change nodes because i set one variable for the time it needs to get turned on and another one for the off time as i need to save both. I think the confusion is also due to that we use different formats of putting in those times from the ui in our flows.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [3 April 2020 21:11 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/17 "2020-04-03T21:11:02Z")

</div>

No Problems.

I went back and only looked at the screen shot. Not actually loading the flow.  
(Didn't help me with my confusion)

I set the scan time at 30 seconds for a reason I can't remember just now, but I think it is something along time lines of "scan at twice the speed you need the sample" because if you are scanning at the same frequency, problems can happen if you miss just one event.

Anyway, I have a new problem just now. On one of my computers and it is annoying.  
TTYL.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [4 April 2020 04:38 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/18 "2020-04-04T04:38:02Z")

</div>

@Trying_to_learn Andrew @JGKK Johannes thanks both flows helps me i'm testing and i'm trying to see what is more functional for what i'm trying to do.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [4 April 2020 04:53 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/19 "2020-04-04T04:53:14Z")

</div>

@JGKK i'm testing your code there is a way to do the opposite of this function.

```auto
const ms = msg.payload;
let result = [];
const intm = String(ms/3600000).split(".");
result.push(parseFloat(intm[0]));
result.push(Math.round(parseFloat("0."+intm[1])*60));
msg.payload = result;
return msg;

```

 ![opposite](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/a/7a18d806be33845fa054dea541c6b6f15dd81290.png)  
if i have the array set it like a number.

---

<div class="post-metadata">

**Author:** ![lalo](https://avatars.discourse-cdn.com/v4/letter/l/f4b2a3/32.png) [@lalo](https://discourse.nodered.org/u/lalo)\
**Post date:** [4 April 2020 06:19 UTC](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008/20 "2020-04-04T06:19:09Z")

</div>

Also could you recomend me how to change the this....  
 ![string to array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/2/52e62dad39e1336b4d620ecef9a8f9cb366b2932.png)  
so i have a string payload and i was looking for a function to convert it in to an array like in the picture the firsts values are strings and with your code are arrays so i want to change my data in to the same format on your code. @JGKK

[Next page](https://discourse.nodered.org/t/set-time-function-on-dashboard/24008.md?page=2)
