How to change the payload data value

I have question how can I change the data value to in sec to hour,min,sec
thank you

sec

Well that appears to be a text string that you are working with there in a JSON data packet.

Assuming that this will always have the same format i would firstly do a drop down in the debug window and get the full path to the status object.

Best bet then would be to read it in to the string node (node-red-contrib-string) and use the inbuilt string functions to strip the 3 characters from the end (sec) and then the Status info from the start

Once there convert it from a string to an integer etc

You could also write a function to the do the same thing depending on if you prefer nodes or functions - but String is your friend either way

Craig

Hi,
you can use the Moment js library that makes date/time calculations easier with its extended library.
the library is already included in Node-red but only for its use in a Change node as a jsonata expression.

In order for the module library to be accesible in your Function nodes you edit the functionGlobalContext section of the Node-red settings.js file in the .node-red folder and restart node red :

 functionGlobalContext: {
           moment:require('moment')
        // os:require('os'),
        // jfive:require("johnny-five"),
        // j5board:require("johnny-five").Board({repl:false})
    },

When you make the change you can use it in a Function, first by filtering out the seconds from the svalue string with a regular expression.

let moment = global.get("moment")

let seconds = msg.payload.svalue.match(/\d+/g)
seconds = Number(seconds[0])
node.warn(seconds)

msg.payload.days = moment.duration(seconds, "seconds").days()
msg.payload.hours = moment.duration(seconds, "seconds").hours()
msg.payload.minutes = moment.duration(seconds, "seconds").minutes()
msg.payload.seconds = moment.duration(seconds, "seconds").seconds()

return msg;

Test Flow example :

[{"id":"7c2b4214.bfc05c","type":"inject","z":"5fffb32f.c79a6c","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"key\":\"udevice\",\"idx\":178,\"svalue\":\"STATUS: 2204sec\"}","payloadType":"json","x":420,"y":380,"wires":[["547c55af.67b58c"]]},{"id":"547c55af.67b58c","type":"function","z":"5fffb32f.c79a6c","name":"","func":"let moment = global.get(\"moment\")\n\nlet seconds = msg.payload.svalue.match(/\\d+/g)\nseconds = Number(seconds[0])\nnode.warn(seconds)\n\nmsg.payload.days = moment.duration(seconds, \"seconds\").days()\nmsg.payload.hours = moment.duration(seconds, \"seconds\").hours()\nmsg.payload.minutes = moment.duration(seconds, \"seconds\").minutes()\nmsg.payload.seconds = moment.duration(seconds, \"seconds\").seconds()\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":570,"y":380,"wires":[["b6aaafbe.79de38"]]},{"id":"b6aaafbe.79de38","type":"debug","z":"5fffb32f.c79a6c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":380,"wires":[]}]

There is also a moment node, but this can be solved in a simpler way.

function:

time = parseInt(msg.payload.split(":")[1])
var minutes = Math.floor(time / 60);
var seconds = time - minutes * 60;
var hours = Math.floor(time / 3600);
formatted = hours+":"+minutes+":"+seconds

return {payload:{formatted:formatted,input:msg.payload}}

Hi

Thank you for the help and the fast response,
Im going to try it out but still learning to use node red.

I think i’m not that great at data types.
Striping of “sec” was easy (I programmed it )
I tried the different methodes here but not getting far.
But managed to create a key
See the picture.

Thats good that you managed to get the seconds and re-structure the message how you want it.
And ? :wink: .. what have you tried for implementing the two suggestions above and applying it to your code ?

I did not use them exactly because it give me some errors.
The problem is not solved yet.
But help Will be appreciated
Some errors i know where the come from.
I have a dishwasser with home Connect (bosch,Siemens) i wantend see how long i have to wait for the programm is to finish.
That information i want to send to my Domoticz system.
There are some bugs like my dishwasser is not on i’m not getting any data or wrong data.
Like a door is open or closed,

OK so now you have your object stripped out and into its own field

You can now reference the value in any of the nodes with data.unit

So in a change node you could move

msg.payload.data.unit to msg.payload

You then have the number of seconds in the msg.payload.

If you now feed that to the switch node you can do something like

if msg.payload < 10 - output 1 (send it to here if the dishwasher has run for less than 10 seconds)
if msg.payload > 60 and <3600 - output 2 (send if here if the dishwasher has run for more than a minute and less than an hour)

etc etc

Try that and then post up your test flow and the debug output

Craig

so here is my progress.

and here is the code

[{"id":"647405ad.e1bbfc","type":"tab","label":"Flow 7","disabled":false,"info":""},{"id":"674dead0.2f8754","type":"inject","z":"647405ad.e1bbfc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":80,"y":160,"wires":[["73d3d8e2.609828"]]},{"id":"73d3d8e2.609828","type":"home-connect-request","z":"647405ad.e1bbfc","auth":"810e5012.f7f76","name":"","tag":"programs","operationId":"get_active_program_option","haid":"SIEMENS-HCS02DWH1-B23AB67AFFCD","body":"","optionkey":"BSH.Common.Option.RemainingProgramTime","programkey":"","settingkey":"","statuskey":"","imagekey":"","x":300,"y":160,"wires":[["430a40af.e5e76","16c78a4d.254d66","9fe3f049.6024e"]]},{"id":"430a40af.e5e76","type":"debug","z":"647405ad.e1bbfc","name":"payload1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":160,"wires":[]},{"id":"16c78a4d.254d66","type":"function","z":"647405ad.e1bbfc","name":"","func":"msg.payload = {\"command\":\"udevice\",\"idx\":178,\"nvalue\":0,\"svalue\":\"STATUS: \"+ msg.payload.data.value.toString()};\nreturn msg\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":540,"y":260,"wires":[["ada423f1.25e3c"]]},{"id":"ada423f1.25e3c","type":"debug","z":"647405ad.e1bbfc","name":"payload2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":260,"wires":[]},{"id":"9fe3f049.6024e","type":"function","z":"647405ad.e1bbfc","name":"","func":"//p=JSON.parse(msg.payload);\np=msg.payload;\nnode.log(typeof p);\nmsg.payload=p.data.value;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":200,"y":320,"wires":[["1e085c59.812564"]]},{"id":"1e085c59.812564","type":"function","z":"647405ad.e1bbfc","name":"","func":"msg.payload = msg.payload.toString();\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":380,"y":320,"wires":[["de90d18.f7dae3"]]},{"id":"de90d18.f7dae3","type":"debug","z":"647405ad.e1bbfc","name":"payload3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":880,"y":320,"wires":[]},{"id":"810e5012.f7f76","type":"home-connect-auth","name":"sim","simulation_mode":true,"scope":"IdentifyAppliance Dishwasher","callback_url":"http://10.0.0.100:1880/homeconnect/auth/callback"}]

i was just doing some testing.
With the solution from bakman 2.

But there is something wring with the timing sometimes missing a 0

[{"id":"f4f60883.c57268","type":"tab","label":"Flow 5","disabled":false,"info":""},{"id":"570161fc.82b5b","type":"inject","z":"f4f60883.c57268","name":"data","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2222","payloadType":"num","x":170,"y":100,"wires":[["1458d4b7.e99fdb"]]},{"id":"18276e83.af3a91","type":"debug","z":"f4f60883.c57268","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":830,"y":120,"wires":[]},{"id":"1458d4b7.e99fdb","type":"function","z":"f4f60883.c57268","name":"","func":"time = parseInt(msg.payload)\nvar minutes = Math.floor(time / 60);\nvar seconds = time - minutes * 60;\nvar hours = Math.floor(time / 3600);\nformatted = hours+\":\"+minutes+\":\"+seconds\n\nreturn {payload:{formatted:formatted,input:msg.payload}}","outputs":1,"noerr":0,"initialize":"","finalize":"","x":500,"y":120,"wires":[["18276e83.af3a91"]]}]

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