# How to change the payload data value

**URL:** https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055
**Category:** General
**Created:** [26 December 2020 23:31 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055 "2020-12-26T23:31:27Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![marconr9](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marconr9/32/34116_2.png) [@marconr9](https://discourse.nodered.org/u/marconr9)
#### Post date: [26 December 2020 23:31 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/1 "2020-12-26T23:31:27Z")

</div>

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

![sec](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/d/cdd94d3d14586fb9e695156342b8622ca06a8f0d.jpeg)

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [27 December 2020 00:45 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/2 "2020-12-27T00:45:40Z")

</div>

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

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [27 December 2020 02:07 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/3 "2020-12-27T02:07:04Z")

</div>

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 :

```auto
 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.

```auto
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 :**

```auto
[{"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":[]}]

```

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [27 December 2020 07:34 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/4 "2020-12-27T07:34:58Z")

</div>

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

 ![Screenshot 2020-12-27 at 08.37.03](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/e/4e2099721b906aa6058a48c26b8212fc6c3af10d.jpeg)

function:

```auto
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}}

```

---

<div class="post-metadata">

### Author: ![marconr9](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marconr9/32/34116_2.png) [@marconr9](https://discourse.nodered.org/u/marconr9)
#### Post date: [27 December 2020 10:20 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/5 "2020-12-27T10:20:41Z")

</div>

Hi

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

---

<div class="post-metadata">

### Author: ![marconr9](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marconr9/32/34116_2.png) [@marconr9](https://discourse.nodered.org/u/marconr9)
#### Post date: [27 December 2020 13:57 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/6 "2020-12-27T13:57:32Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/9/09c5a45457f2966e381d6a13218bff22992141c7.jpeg)

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [28 December 2020 01:26 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/7 "2020-12-28T01:26:17Z")

</div>

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

---

<div class="post-metadata">

### Author: ![marconr9](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marconr9/32/34116_2.png) [@marconr9](https://discourse.nodered.org/u/marconr9)
#### Post date: [28 December 2020 12:11 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/8 "2020-12-28T12:11:50Z")

</div>

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,

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [29 December 2020 23:26 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/9 "2020-12-29T23:26:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![marconr9](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marconr9/32/34116_2.png) [@marconr9](https://discourse.nodered.org/u/marconr9)
#### Post date: [30 December 2020 21:33 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/10 "2020-12-30T21:33:45Z")

</div>

so here is my progress.

 ![string](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/6/36cfea04605da964de4fafa5d95d67cbf6f379be.jpeg)

and here is the code

```auto
[{"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"}]

```

---

<div class="post-metadata">

### Author: ![marconr9](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marconr9/32/34116_2.png) [@marconr9](https://discourse.nodered.org/u/marconr9)
#### Post date: [31 December 2020 15:33 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/11 "2020-12-31T15:33:30Z")

</div>

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

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

 ![time](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9fe24035bb5b762b08e8ddfcf04b4fec993a49db.jpeg)

```auto
[{"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"]]}]

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 March 2021 15:33 UTC](https://discourse.nodered.org/t/how-to-change-the-payload-data-value/38055/12 "2021-03-01T15:33:32Z")

</div>

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