Compose msg.payload with msg.topic

Hello all,

I would like to calculate the timestamp that is in the payload here by a date/time formatter minus one hour and then reconnect it to the value that is in the topic.
Can someone help me quickly?

Here is my flow:

{"id":"cc226534.335518","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Pumpe 1","measurement":"Ergebnis1_Global_REAL","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1120,"y":240,"wires":[]},{"id":"1f8d1bb3.f5d694","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Pumpe 2","measurement":"Ergebnis2_Global_REAL","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1120,"y":280,"wires":[]},{"id":"e30ca4b8.7166b","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Pumpe 3","measurement":"Ergebnis3_Global_REAL","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1120,"y":320,"wires":[]},{"id":"4199b242.f445a4","type":"inject","z":"4966f2f7.bdcd5c","name":"Pumpe 4 OPC-UA","props":[{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_REAL","x":250,"y":400,"wires":[["6260bc62.42f9d4"]]},{"id":"6260bc62.42f9d4","type":"OpcUa-Client","z":"4966f2f7.bdcd5c","endpoint":"dab8ddcf.a3caf8","action":"read","deadbandtype":"a","deadbandvalue":1,"time":10,"timeUnit":"s","certificate":"n","localfile":"","localkeyfile":"","securitymode":"None","securitypolicy":"None","name":"Technikerprojekt OPC-UA Client","x":570,"y":400,"wires":[["7b640529.a5ad1c"]]},{"id":"4b076e1a.7f7628","type":"inject","z":"4966f2f7.bdcd5c","name":"Füllstand Auffangbecken OPC-UA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis1_Global_INT","payload":"","payloadType":"date","x":200,"y":440,"wires":[["6260bc62.42f9d4"]]},{"id":"7b640529.a5ad1c","type":"function","z":"4966f2f7.bdcd5c","name":"","func":"//add more tags as required\nvar tags = {\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis1_Global_REAL\": 0,\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis2_Global_REAL\": 1,\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis3_Global_REAL\": 2,\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_REAL\": 3,\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis1_Global_INT\": 4,\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis2_Global_INT\": 5,\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis3_Global_INT\": 6,\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_INT\": 7,\n    \"ns=5;s=Arp.Plc.Eclr/Ergebnis5_Global_INT\": 8,\n}\nvar length = Object.keys(tags).length;\nvar result = Array(length);\nvar tagIndex = tags[msg.topic]; //lookup the tag, get index\nif(tagIndex==null){\n    node.warn(\"Unknown TAG: \" + msg.topic)\n    return null; //dont send anything\n}\nresult[tagIndex] = msg;\nreturn result;","outputs":9,"noerr":0,"initialize":"","finalize":"","x":820,"y":540,"wires":[["cc226534.335518","d245da5b.b9cd68"],["1f8d1bb3.f5d694","4af7f3cf.743c0c"],["e30ca4b8.7166b","212b9d58.7f147a"],["312c61d8.472346","4609154c.565344"],["21702d4d.35908a","592dc150.47f7a8"],["8658aab3.a3d768","18150b97.97e80c"],["ea7819f5.3dc118","4e39f37e.8ac594"],["c7dfdf3e.33cdd","aeea89d3.49b6b"],["e1c8ec7c.ec5f7","674d2eb4.c50158"]]},{"id":"312c61d8.472346","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Pumpe 4","measurement":"Ergebnis4_Global_REAL","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1120,"y":360,"wires":[]},{"id":"21702d4d.35908a","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Füllstand Auffangbecken","measurement":"Ergebnis1_Global_INT","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1170,"y":400,"wires":[]},{"id":"46e8fa7c.876c04","type":"inject","z":"4966f2f7.bdcd5c","name":"Pumpe 3 OPC-UA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis3_Global_REAL","payload":"","payloadType":"date","x":250,"y":360,"wires":[["6260bc62.42f9d4"]]},{"id":"27b3144.4adf5ec","type":"inject","z":"4966f2f7.bdcd5c","name":"Pumpe 2 OPC-UA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis2_Global_REAL","payload":"","payloadType":"date","x":250,"y":320,"wires":[["6260bc62.42f9d4"]]},{"id":"7a7901c7.3eba68","type":"inject","z":"4966f2f7.bdcd5c","name":"Pumpe 1 OPC-UA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis1_Global_REAL","payload":"","payloadType":"date","x":250,"y":280,"wires":[["e08f24eb.3bc97"]]},{"id":"1ebc1204.f4bd76","type":"inject","z":"4966f2f7.bdcd5c","name":"Füllstand Brunnen 1 OPC-UA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis2_Global_INT","payload":"","payloadType":"date","x":210,"y":480,"wires":[["6260bc62.42f9d4"]]},{"id":"793f6362.3984c4","type":"inject","z":"4966f2f7.bdcd5c","name":"Füllstandbrunnen 2 OPC-UA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis3_Global_INT","payload":"","payloadType":"date","x":210,"y":520,"wires":[["6260bc62.42f9d4"]]},{"id":"16233006.092c08","type":"inject","z":"4966f2f7.bdcd5c","name":"Füllstand Brunnen 3 OPC-UA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis4_Global_INT","payload":"","payloadType":"date","x":210,"y":560,"wires":[["6260bc62.42f9d4"]]},{"id":"4a0aa97b.8b3048","type":"inject","z":"4966f2f7.bdcd5c","name":"Füllstand Brunnen 4 OPC-UA","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"5","crontab":"","once":false,"onceDelay":0.1,"topic":"ns=5;s=Arp.Plc.Eclr/Ergebnis5_Global_INT","payload":"","payloadType":"date","x":210,"y":600,"wires":[["6260bc62.42f9d4"]]},{"id":"8658aab3.a3d768","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Füllstand Brunnen 1","measurement":"Ergebnis2_Global_INT","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1160,"y":440,"wires":[ ]},{"id":"ea7819f5.3dc118","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Füllstand Brunnen 2","measurement":"Ergebnis3_Global_INT","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1160,"y":480,"wires":[]},{"id":"c7dfdf3e.33cdd","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Füllstand Brunnen 3","measurement":"Ergebnis4_Global_INT","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1160,"y":520,"wires":[]},{"id":"e1c8ec7c.ec5f7","type":"influxdb out","z":"4966f2f7.bdcd5c","influxdb":"ddd10b41.3fd178","name":"Füllstand Brunnen 4","measurement":"Ergebnis5_Global_INT","precision":"","retentionPolicy":"","database":"database","precisionV18FluxV20":"ms","retentionPolicyV18Flux":"","org":"organisation","bucket":"bucket","x":1160,"y":560,"wires":[]},{"id":"d245da5b.b9cd68","type":"debug","z":"4966f2f7.bdcd5c","name":"Pumpe 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":680,"wires":[]},{"id":"4af7f3cf.743c0c","type":"debug","z":"4966f2f7.bdcd5c","name":"Pumpe 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":720,"wires":[]},{"id":"4e39f37e.8ac594","type":"debug","z":"4966f2f7.bdcd5c","name":"Brunnen 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":920,"wires":[]},{"id":"aeea89d3.49b6b","type":"debug","z":"4966f2f7.bdcd5c","name":"Brunnen 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":960,"wires":[]},{"id":"212b9d58.7f147a","type":"debug","z":"4966f2f7.bdcd5c","name":"Pumpe 3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":760,"wires":[]},{"id":"4609154c.565344","type":"debug","z":"4966f2f7.bdcd5c","name":"Pumpe 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1100,"y":800,"wires":[]},{"id":"592dc150.47f7a8","type":"debug","z":"4966f2f7.bdcd5c","name":"Auffangbecken","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1120,"y":840,"wires":[]},{"id":"18150b97.97e80c","type":"debug","z":"4966f2f7.bdcd5c","name":"Brunnen 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":880,"wires":[]},{"id":"674d2eb4.c50158","type":"debug","z":"4966f2f7.bdcd5c","name":"Brunnen 4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":1000,"wires":[]},{"id":"9aab4c7f.e27ef8","type":"debug","z":"4966f2f7.bdcd5c","name":"Zeitstempel 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":760,"y":220,"wires":[]},{"id":"e08f24eb.3bc97","type":"date-converter","z":"4966f2f7.bdcd5c","name":"","topic":"","input":"payload","inputType":"msg","inTz":"","adjAmount":"1","adjType":"hours","adjDir":"subtract","format":"","locale":"","output":"payload","outputType":"msg","outTz":"","x":520,"y":240,"wires":[["9aab4c7f.e27ef8"],[]]},{"id":"ddd10b41.3fd178","type":"influxdb","hostname":"192.168.100.110","port":"8086","protocol":"http","database":"Technikerprojekt","name":"","usetls":false,"tls":"","influxdbVersion":"1.x","url":"http://localhost:8086","rejectUnauthorized":true},{"id":"dab8ddcf.a3caf8","type":"OpcUa-Endpoint","endpoint":"opc.tcp://192.168.100.110:4840","secpol":"Basic256Sha256","secmode":"SignAndEncrypt","login":true}]

Hi, in order to make code more readable and importable it is important to surround your code with three backticks
```
like this
```

You can edit and correct your post by clicking the pencil icon.

See this post for more details - How to share code or flow json

first things first. Put a debug here and show us the output of that OPCUA node.
image





Secondly,

This...
image


can be converted to this...
image
... if you set msg.measurement in the function.


function code...
//use the last part of the topic (AFTER / ) as the measurement
msg.measurement = msg.topic.split("/").pop();
return msg;

Okay perfect, thanks a lot!

we are UTC +2, but we need UTC +1

Did you click the timestamp (the payload) to change its format? Can you click it see if it changes to a number - then copy paste the numbers?

Also, what timezone is set on the node-red server?

Also, what timezone (if any) is set on the PLC?)

Also, what timezone (if any) is set on the OPC Server?

Deducting 1hr is a HACK & will bite you later when DST happens on one or more of the machines.

Hello, we have now found and fixed the error, on the PLC Next an incorrect UCT Time was set, therefore the time stamp in our time zone was always shifted forward by one hour. Now everything works.
Thank you very much for your help.

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