With state memory enabled, you can store values that can be used in successive messages to handle the incoming messages. We will go through a sample code to understand how state memory works while creating a custom function.
- headerMap = header.toMap();
- deviceid = headerMap.get("deviceid");
- receivedtime = headerMap.get("occurred_timestamp")
- topic = headerMap.get("topic");
- communication_status_flag = headerMap.get("data_internal");
- state_memory = headerMap.get("state_memory");
- if(state_memory == null)
{
state_memoryMap = Map();
}
else
{
state_memoryMap = state_memory.toMap();
}
if(communication_status_flag == false && topic == "telemetry")
{
payload = message.toMap();
if(payload.containKey("data"))
{
deviceMessage = payload.get("data");
}
else
{
deviceMessage = payload;
}
}
else if(communication_status_flag == true)
{
deviceMessage = Map();
} - //Logic
current_energy = deviceMessage.get('energy_consumed');
last_month_energy = state_memoryMap.get('previous_energy');
if(state_memoryMap.containKey('previous_energy'))
{
total_energy = current_energy + last_month_energy;
deviceMessage.put("energy_consumed", total_energy);
state_memoryMap.put('previous_energy', total_energy);
}
else
{
state_memoryMap.put('previous_energy', current_energy);
} - //Prepare output
output = Map();
output.put("device_message",deviceMessage);
state_memory_string = state_memoryMap.toString();
output.put("state_memory",state_memory_string);
return output;
This sample code is designed to manage energy data from an energy meter. It operates by storing monthly energy consumption data in state memory. Whenever a new data message is received, the code adds this new consumption value to the existing data stored in state memory. Subsequently, it publishes the updated total and then saves this new total back to the state memory for future use.
We will test functionality of this code under two conditions: with and without a pre-existing value in the state memory.
For these tests, the payload has the key "energy_consumed", and the state memory stores the cumulative energy data under the key "previous_energy".
First, we will test without any value stored in the state memory to assess how the code handles the absence of pre-existing data. This will verify the code’s initialization process and its ability to start accumulating data from scratch.
Click Save and Execute in the top right.
Select Device Message in the Message Type field.
Provide the input {'energy_consumed':100} in the Message field.
Click Save.
On executing without a value for state memory, the output will display "energy_consumed: 100" and "previous_energy: 100." This outcome occurs because, assuming this is the first message, no previous data exists in the state memory. Therefore, the received value is treated as the initial total consumed energy. This value is then updated and stored as "previous_energy" in the state memory, ready to be utilized for calculations with the next incoming message.
Next, we will test the code with a predefined value in the state memory to see how the code performs. This will help us confirm that the state memory value is correctly utilized when the code is executed
Click Save and Execute in the top right.
Select Device Message in the Message Type field.
Provide the input {'energy_consumed':114} in the Message field.
Provide the input {'previous_energy': 233} in the State Memory filed.
Click Save.
On executing the code with an existing state memory value, you will observe that the output displays the total energy consumed as 347. This same value is then updated in the state memory. This output results from the current energy consumed being added to the "previous_energy" value stored in state memory. This test confirms the functionality of the code in accumulating energy usage over time by continuously updating the state memory with the new totals.