Store a full message and retreive at node-red start time

Hello,

I have a Node-RED function node that outputs a message periodically. I would like to save this message to a file so that if Node-RED restarts, I can retrieve the last saved message but cannot inject it back into the function node when Node-RED starts up again.

The issue I'm running into is that when Node-RED restarts, the previous message in the function node gets cleared out. I need a way to persistently store the message from the function node so that it is not lost on restart.

My thought is to have the function node write its output message to a file periodically. Then on Node-RED startup, I can read the last saved message from that file and inject it into the function node to pick up where it left off before the restart. This would prevent losing the message and state on a restart. But I tried and not working.

Please let me know if you have any suggestions on the best approach to persistently store the message from the function node and re-inject it on startup.

[{"potNumber":137,"anodeNumber":8,"eventBoxes":[{"box_id":"1_137_8","id":1,"event":"CrustBreaker","PotNum":137,"AnodeNum":8,"StartTime":"10:48:38","StartDate":"10/15/2023","StopTime":"10:48:39","StopDate":"10/15/2023","EventDuration":1,"flowId":1},{"box_id":"3_137_8","id":3,"event":"AnodeRemoval","PotNum":137,"AnodeNum":8,"StartTime":"10:48:45","StartDate":"10/15/2023","StopTime":"10:48:49","StopDate":"10/15/2023","EventDuration":4,"flowId":1},{"box_id":"6_137_8","id":6,"event":"CavityScoop","PotNum":137,"AnodeNum":8,"StartTime":"10:48:51","StartDate":"10/15/2023","StopTime":"10:48:53","StopDate":"10/15/2023","EventDuration":2,"flowId":1},{"box_id":"9_137_8","id":9,"event":"NewAnodeInstall","PotNum":137,"AnodeNum":8,"StartTime":"10:49:31","StartDate":"10/15/2023","StopTime":"10:49:32","StopDate":"10/15/2023","EventDuration":1,"flowId":1}],"sequenceComplete":1,"currentSequencePosition":4}]

image

Use persistent context or DIY it with file nodes.

2 Likes

Thank you @Steve-Mcl for the suggestion. I tried using a file node to retrieve the message, but I'm struggling with how to properly inject it back into my function node.

Use an inject node set to fire on startup to trigger the read.

Persistent context would probably be a much simpler solution.

1 Like

Persistent context means adding module in setting.js ?

Have you read the documentation? Take a read of this: https://nodered.org/docs/user-guide/contexttake and then if you don't understand ask.

You can also read this post I made quite awhile ago: (A guide to understanding 'Persistent Context')

3 Likes

Looking at your other post, it seems that the data you want to retrieve at startup originates in a database.

Rather than storing this data in Node-red persistent context or in a file, why can't you recover it from the database?

Thank you @all ,I will use Persistent context

1 Like

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