Time parsing - how to cut up time messages

For the sake of this example the payload looks like this:
Time 10:23:4

Shame it isn't better formatted, but anyway.

In light of recent problems I found this problem - or maybe it has only now stuck its head up.

Weirdly it hasn't shown itself until recently.
This dates back to a problem which happened every day at midnight.
But that's a whole other story.

So:
I am sending in the above message to a function node.
I want to determine the "seconds past/since midnight".

This is the code:

var hour = parseInt(msg.payload.slice(0,2));
var minute = parseInt(msg.payload.slice(3,5));
var second = parseInt(msg.payload.slice(6,8));

node.warn("Time " + msg.payload);
node.warn("Hour " + hour);
node.warn("Minute " + minute);
node.warn("Second " + second);

var time = hour*3600 + minute * 60 + second;
msg = {payload: time, topic: 'RTC'};
return msg;

There is a second node which sets the topic as local to differentiate between the clock and the RTC.

Here's the rub:
Screen shot 1:
HWC time parsed by the function node. See results.
Screen shot 2:
RasPi time parsed by the other function node with the same code.
Sorry I didn't have the debug node active.
However: the errors! Why?
It is the same code.

Yes, I am missing an elephant.

I accept I maybe should "split" the string with a :.
That aside, why does one work and the other fail?

Thanks in advance.

I'm trying something else. Trying to use the basic/supplied nodes.

Well, mostly.

So now I have this:
(example flow)

[{"id":"ff317fc3.ea97b","type":"inject","z":"55b8031d.59ac1c","name":"Foo","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":1660,"wires":[["dedc1caf.d3e2c8"]]},{"id":"dedc1caf.d3e2c8","type":"function","z":"55b8031d.59ac1c","name":"","func":"msg.payload = new Date().toLocaleString();\nreturn msg;","outputs":1,"noerr":0,"x":200,"y":1660,"wires":[["6e7526e7.96ed1","837e6529.f438f8"]]},{"id":"837e6529.f438f8","type":"moment","z":"55b8031d.59ac1c","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":320,"y":1770,"wires":[["c7a969f7.45794","c7cb6741.8bf48"]]},{"id":"c7cb6741.8bf48","type":"split","z":"55b8031d.59ac1c","name":"","splt":":","spltType":"str","arraySplt":"2","arraySpltType":"len","stream":false,"addname":"blah","x":510,"y":1770,"wires":[["203042c.cf7ce3e"]]},{"id":"203042c.cf7ce3e","type":"debug","z":"55b8031d.59ac1c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":630,"y":1720,"wires":[]}]

Generate the time (yeah, I know I could use the timestamp in the inject node.

Stick it into a moment node to get Hours, minutes and seconds. HH:mm:ss

Send that into a split node.
But reading the docs and what I get are different.

I want an array. (mgs.blah) with the values.
It isn't happening.

All I am getting is:

{"topic":"","payload":"11","parts":{"id":"4677f279.e1281c","type":"string","ch":":","index":0,"count":3},"_msgid":"cb34d9db.df4848"}
{"topic":"","payload":"04","parts":{"id":"4677f279.e1281c","type":"string","ch":":","index":1,"count":3},"_msgid":"b09564e3.abe768"}
{"topic":"","payload":"23","parts":{"id":"4677f279.e1281c","type":"string","ch":":","index":2,"count":3},"_msgid":"56a620e4.8798b"}

No msg.blah visible.

My best guess is that the string being passed to the function may have some leading spaces. It is always a good practice to trim your string. Try using this line as the first one in the function code. The trim method will remove white spaces from both ends of the string.

msg.payload = msg.payload.trim();

Indeed. According to the split node help

The behaviour of the node is determined by the type of msg.payload :

The output of the moment node is a string, so the split node will disregard the configuration you entered for copying key to msg.blah.

You want to transform a string into an array. This is similar to what has been discussed in this post

You can apply the same solution, a split node + join node.

Testing flow:

[{"id":"5a4f00a0.1539d","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"9496950a.d98538","type":"inject","z":"5a4f00a0.1539d","name":"Foo","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":280,"wires":[["a8bc1ecd.231c"]]},{"id":"a8bc1ecd.231c","type":"function","z":"5a4f00a0.1539d","name":"","func":"msg.payload = new Date().toLocaleString();\nreturn msg;","outputs":1,"noerr":0,"x":250,"y":280,"wires":[["e98349ab.6a3558"]]},{"id":"e98349ab.6a3558","type":"moment","z":"5a4f00a0.1539d","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":420,"y":280,"wires":[["6f0835.a09d27cc"]]},{"id":"f0b653ec.00866","type":"debug","z":"5a4f00a0.1539d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":850,"y":280,"wires":[]},{"id":"6f0835.a09d27cc","type":"split","z":"5a4f00a0.1539d","name":"","splt":":","spltType":"str","arraySplt":"2","arraySpltType":"len","stream":false,"addname":"","x":590,"y":280,"wires":[["79820f63.5886a"]]},{"id":"79820f63.5886a","type":"join","z":"5a4f00a0.1539d","name":"","mode":"custom","build":"array","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":710,"y":280,"wires":[["f0b653ec.00866"]]}]
1 Like

@Andrei
Thanks.

I have since nutted it out.

Though thanks for the help.

This is how I "cheat" to get the time/s working better/how I want:

[{"id":"837e6529.f438f8","type":"moment","z":"55b8031d.59ac1c","name":"","topic":"","input":"","inputType":"msg","inTz":"Australia/Sydney","adjAmount":0,"adjType":"days","adjDir":"add","format":"HH:mm:ss","locale":"en_AU","output":"","outputType":"msg","outTz":"Australia/Sydney","x":380,"y":1660,"wires":[["94444dfb.cd03f"]]},{"id":"dedc1caf.d3e2c8","type":"function","z":"55b8031d.59ac1c","name":"","func":"msg.payload = new Date().toLocaleString();\nreturn msg;","outputs":1,"noerr":0,"x":200,"y":1660,"wires":[["837e6529.f438f8"]]},{"id":"94444dfb.cd03f","type":"function","z":"55b8031d.59ac1c","name":"","func":"var hour;\nvar minute;\nvar second;\nvar spm;\n\nvar times = msg.payload.split(':');\nhour = times[0];\nhour = hour * 3600;\n//node.warn(\"Hours \" + hour);\n\nminute = times[1];\nminute = minute * 60;\n//node.warn(\"Minute \" + minute);\n\nsecond = times[2];\n//node.warn(\"Second \" + second);\n\nspm = parseInt(hour + minute + second);\n\nmsg.payload = spm;\nmsg.topic = \"local\";\nreturn msg;","outputs":1,"noerr":0,"x":570,"y":1660,"wires":[["c320690c.08a068"]]},{"id":"ff317fc3.ea97b","type":"inject","z":"55b8031d.59ac1c","name":"Foo","topic":"","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":80,"y":1660,"wires":[["dedc1caf.d3e2c8"]]},{"id":"c320690c.08a068","type":"debug","z":"55b8031d.59ac1c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":700,"y":1660,"wires":[]}]

That works.

What was also catching me was that one of the two (software or hardware) time was a string and so I had to add the line ParseInt( ) to the function node to get all things equal.

If I wasn't so stupid I would say there are gremlins that come in and change the code when I am not looking only to spite me.

1 Like