Arseniy Sleptsov
Arseniy Sleptsov

Reputation: 175

Create a json object with properties values different from previous for sending as a message

I am delivering data from PLC Siemens S7 through node-red installed on IoT edge device to the cloud storage.

In the node-red I receive data as a JSON object with several properties every second from PLC node. The JSON object (or data) has the same properties every second.

Some properties change every second, some change in five minute, some remain constant for several hours.

To handle data to the cloud the JSON object is handled to the IoT hub node in the node-red.

I would like to process this JSON object before handling to the IoT hub node in a way that the JSON object has only the properties which changed from previous state (a second ago). It means that I want to compare an input object at the moment t with the object at t-1 and generate a new object with properties of the object at t which differ from those of the object at t-1.

The new object will be handled further. The object processing is expected to be done in the function node in the node-red. From PLC node the JSON object is received as the msg.payload.

Could you please help me to script in (javascript) the process I have described?

msg.payload to the function node is json object:

{"Zone0": 200,
 "Wire_tension": 2.5}

UPD: Since it was not clear what I have tried, here is the code. Sorry for not providing it, I thought it was clear.

var state = {}; // this state will be messaged further

var prev_state = {
    "Zone0": 0,
    "Wire_tension": 0
}; // initially zeros are assigned

var current_state = msg.payload; // has these two properties and updated every second

// comparison of properties
if (current_state.Zone0 != prev_state.Zone0) {
    state["Zone0"] = current_state.Zone0;
    prev_state["Zone0"] = current_state.Zone0;
}

if (current_state.Wire_tension != prev_state.Wire_tension) {
    state["Wire_tension"] = current_state.Wire_tension;
    prev_state["Wire_tension"] = current_state.Wire_tension;
}

msg.payload = state;

return msg;

It looks like when the flow in the node-red starts the function node runs the code every time. Therefore prev_state is assigned every time. I understand that I need to somehow handle initial state which is updated further.

UPD2: I also tried using the following code (also with context feature). The example is with my real data:

var state = {}; // this state will be messaged further

flow.set(["Zone0", "Wire_tension"],[0,0]); // initially zeros are assigned

var current_state = msg.payload; // has these two properties and updated every second

// comparison of properties
if (current_state.Zone0 != flow.get("Zone0")) {
    state["Zone0"] = current_state.Zone0;
    flow.set("Zone0", current_state.Zone0);
}

if (current_state.Wire_tension != flow.get("Wire_tension")) {
    state["Wire_tension"] = current_state.Wire_tension;
    flow.set("Wire_tension", current_state.Wire_tension);
}

msg.payload = state;

return msg;

Upvotes: 0

Views: 831

Answers (2)

Arseniy Sleptsov
Arseniy Sleptsov

Reputation: 175

In the "On Start" tab in properties of function node:

flow.set(["Zone0", "Wire_tension"], [0, 0]);

In the "On Message" tab in the properties of function node:

var state = {}; // this state will be messaged further

var current_state = msg.payload; // has these two properties and updated every second

// comparison of properties
if (current_state.Zone0 != context.get("Zone0")) {
    state["Zone0"] = current_state.Zone0;
    context.set("Zone0", current_state.Zone0);
}

if (current_state.Wire_tension != context.get("Wire_tension")) {
    state["Wire_tension"] = current_state.Wire_tension;
    context.set("Wire_tension", current_state.Wire_tension);
}

msg.payload = state;

return msg;

Upvotes: 0

Immanuel
Immanuel

Reputation: 167

best option is use node-red global context variable

var object_to_build = {}
var key_1_val = global.get("key_1");
if (msg.payload.key_1 != key_1_val) {
    object_tto_build['key_1'] = msg.payload.key_1
    global.set("key_1", msg.payload.key_1);
}

// do whatever you like to do with - object_to_build. I have not validated the code, just the typo

Upvotes: 0

Related Questions