Help with context data

hi
I'm trying to figure out context store , I've read the docs and watched to video but don't understand any of it . :scream:
I've managed to change the js file thingy to

contextStorage: {
default: {
module:"localfilesystem"
},
},
and restarted node red, is there anything else i have to do ?
here is my chart flow

[{"id":"5fd99dd03d10f4a1","type":"tab","label":"new daily high/low","disabled":false,"info":"","env":[]},{"id":"b40f8bf37be7f2e3","type":"group","z":"5fd99dd03d10f4a1","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["9a8e6fed441c2029","4adc3c0e52c64503","2bfd8488040e2b7e","92c301603ab079c2","3f346011cacd22ca","cf1f054e8484dfbc","8611a498ddeac1fc","6d97f9a67c7bb40b","94146c0df761d3e5","f0267c8bebc47f17","1bf4760a84134919","8a863d12861905bd","2bc58a155b1d4d84","96593419eb4ceb39","ac960bd9f783d609","79ed2f986274d790","26f285d907cdc405","73a1c8201bdeb09b","9476cf9ce6f94391"],"x":14,"y":19,"w":772,"h":307},{"id":"79ed2f986274d790","type":"junction","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","x":160,"y":100,"wires":[["3f346011cacd22ca","cf1f054e8484dfbc"]]},{"id":"26f285d907cdc405","type":"junction","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","x":380,"y":100,"wires":[["8611a498ddeac1fc","6d97f9a67c7bb40b"]]},{"id":"73a1c8201bdeb09b","type":"junction","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","x":340,"y":300,"wires":[["1bf4760a84134919","8a863d12861905bd"]]},{"id":"9476cf9ce6f94391","type":"junction","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","x":760,"y":180,"wires":[["96593419eb4ceb39","ac960bd9f783d609"]]},{"id":"9a8e6fed441c2029","type":"function","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"function 21","func":"// Look in On Start\nlet msg1 = {}\nlet msg2 = {}\nlet msg3 = {}\nlet old = context.get('Day')\nlet day = new Date().getDay()\n\nif (old != day) {\n    context.set('Day', day)\n    context.set(\"Max\", -100)\n    context.set(\"Min\", 100)\n}\n\nlet max = context.get('Max')\nlet min = context.get('Min')\n\nconst Tmp = msg.payload\n\nif (Tmp > max) {\n    max = Tmp\n    context.set('Max', Tmp)\n}\n\nif (Tmp < min)  {\n    min = Tmp\n    context.set('Min', Tmp)\n}\n\nmsg1.payload = Tmp\nmsg1.topic = 'Tmp'\nmsg2.payload = max\nmsg2.topic = 'Max'\nmsg3.payload = min\nmsg3.topic = 'Min'\nreturn [msg1, msg2, msg3]\n","outputs":3,"timeout":0,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\n\ncontext.set(\"Day\", -1)\n ","finalize":"","libs":[],"x":270,"y":180,"wires":[["79ed2f986274d790"],["26f285d907cdc405","2bfd8488040e2b7e"],["92c301603ab079c2","73a1c8201bdeb09b"]]},{"id":"4adc3c0e52c64503","type":"mqtt in","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"Temperature","topic":"zone0/text","qos":"2","datatype":"auto-detect","broker":"b8758158381423f0","nl":false,"rap":true,"rh":0,"inputs":0,"x":110,"y":180,"wires":[["9a8e6fed441c2029"]]},{"id":"2bfd8488040e2b7e","type":"bar-chart-data2","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"bar-chart-data2","x_interval":"days","x_size":24,"unit":"","precision":"3","is_meter_reading":"False","agg_by":"max","x":480,"y":160,"wires":[["f0267c8bebc47f17"]]},{"id":"92c301603ab079c2","type":"bar-chart-data2","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"bar-chart-data2","x_interval":"days","x_size":24,"unit":"","precision":"3","is_meter_reading":"False","agg_by":"min","x":480,"y":200,"wires":[["f0267c8bebc47f17"]]},{"id":"3f346011cacd22ca","type":"ui_text","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","group":"e2740f14149858bd","order":1,"width":"18","height":1,"name":"","label":"Today's Temp","format":"{{msg.payload}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":280,"y":80,"wires":[]},{"id":"cf1f054e8484dfbc","type":"debug","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"tmp","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"true","targetType":"full","statusVal":"payload","statusType":"auto","x":250,"y":120,"wires":[]},{"id":"8611a498ddeac1fc","type":"ui_text","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","group":"e2740f14149858bd","order":3,"width":"18","height":1,"name":"","label":"Today's Max","format":"{{msg.payload}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":490,"y":80,"wires":[]},{"id":"6d97f9a67c7bb40b","type":"debug","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"max","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":470,"y":120,"wires":[]},{"id":"94146c0df761d3e5","type":"inject","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"clear","payloadType":"str","x":250,"y":240,"wires":[["2bfd8488040e2b7e","92c301603ab079c2"]]},{"id":"f0267c8bebc47f17","type":"function","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"","func":"let msg1 = {}\nlet invar = msg.payload\n\nif (msg.topics == undefined) { return }\nlet topic = msg.topics[0]\n\ncontext.data = context.data ?? {}\n\nswitch (topic) {\n  case 'Max':\n    context.data.max = invar\n  break\n \n  case 'Min':\n    context.data.min = invar\n  break\n}\n\nlet max = {}\nlet min = {}\nlet mm = {}\n\nif (context.data.max != null && context.data.min != null) {\n\n  max.labels = context.data.max[0].labels\n  max.series = [context.data.max[0].series[0]]\n  max.data = [context.data.max[0].data[0]]\n\n  min.labels = context.data.min[0].labels\n  min.series = [context.data.min[0].series[0]]\n  min.data = [context.data.min[0].data[0]]\n\n  mm.labels = min.labels\n  mm.series = [max.series[0], min.series[0]]\n  mm.data = [max.data[0], min.data[0]]\n    \n  msg1.payload = [mm]\n  return msg1\n}\n","outputs":1,"timeout":"","noerr":0,"initialize":"","finalize":"","libs":[],"x":640,"y":180,"wires":[["9476cf9ce6f94391"]]},{"id":"1bf4760a84134919","type":"debug","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"min","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":430,"y":280,"wires":[]},{"id":"8a863d12861905bd","type":"ui_text","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","group":"e2740f14149858bd","order":5,"width":"18","height":1,"name":"","label":"Today's Min","format":"{{msg.payload}}","layout":"row-spread","className":"","style":false,"font":"","fontSize":16,"color":"#000000","x":450,"y":240,"wires":[]},{"id":"2bc58a155b1d4d84","type":"ui_button","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"","group":"e2740f14149858bd","order":8,"width":"18","height":"1","passthru":false,"label":"Clear All","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"[]","payloadType":"json","topic":"","topicType":"str","x":640,"y":220,"wires":[["9476cf9ce6f94391"]]},{"id":"96593419eb4ceb39","type":"ui_chart","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"","group":"e2740f14149858bd","order":7,"width":"18","height":"5","label":"","chartType":"bar","legend":"false","xformat":"HH:mm:ss","interpolate":"linear","nodata":"","dot":false,"ymin":"","ymax":"","removeOlder":"30","removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"useUTC":false,"colors":["#1f77b4","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#d62728","#ff9896","#9467bd","#c5b0d5"],"outputs":1,"useDifferentColor":false,"className":"","x":630,"y":120,"wires":[[]]},{"id":"ac960bd9f783d609","type":"debug","z":"5fd99dd03d10f4a1","g":"b40f8bf37be7f2e3","name":"debug 63","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":60,"wires":[]},{"id":"b8758158381423f0","type":"mqtt-broker","name":"Local Broker","broker":"localhost","port":1883,"clientid":"","autoConnect":true,"usetls":false,"protocolVersion":4,"keepalive":60,"cleansession":true,"autoUnsubscribe":true,"birthTopic":"","birthQos":"0","birthRetain":"false","birthPayload":"","birthMsg":{},"closeTopic":"","closeQos":"0","closeRetain":"false","closePayload":"","closeMsg":{},"willTopic":"","willQos":"0","willRetain":"false","willPayload":"","willMsg":{},"userProps":"","sessionExpiry":""},{"id":"e2740f14149858bd","type":"ui_group","name":"bar-chart","tab":"957fd14a.a1277","order":1,"disp":true,"width":24,"collapse":false,"className":""},{"id":"957fd14a.a1277","type":"ui_tab","name":"bar-chart","icon":"dashboard","disabled":false,"hidden":false},{"id":"fcf20927f72f182b","type":"global-config","env":[],"modules":{"node-red-contrib-dashboard-bar-chart-data2":"0.9.101","node-red-dashboard":"3.6.6"}}]


I just want my charts not to forget all the info when i update node red or something .
any help would be great
thanks
martin

In your first function node you have

let old = context.get('Day')
let day = new Date().getDay()

if (old != day) {
    context.set('Day', day)
    context.set("Max", -100)
    context.set("Min", 100)
}

let max = context.get('Max')
let min = context.get('Min')

This creates three discrete context variables Day, Min and Max.
All seems good.

In your second function node this code

context.data = context.data ?? {}

switch (topic) {
  case 'Max':
    context.data.max = invar
  break
 
  case 'Min':
    context.data.min = invar
  break
}

seems to be attempting to refer to the same variables as properties of an object data.
You are using different, invalid syntax in this second function.
Also I think that context variable names are case sensitive.

Sorry, not trying to be pedantic.

CONTEXT is a bit vague.

If you are using context in a function node, the second one won't see it.
(Or am I wrong?)

You need flow context.
I haven't looked at the code itself.

But just in passing thought that needs clarifying.

2 Likes

here's the documentation --> Writing Functions : Node-RED

context

    context.get(..) : get a node-scoped context property
    context.set(..) : set a node-scoped context property
    context.keys(..) : return a list of all node-scoped context property keys
    context.flow : same as flow
    context.global : same as global

flow

    flow.get(..) : get a flow-scoped context property
    flow.set(..) : set a flow-scoped context property
    flow.keys(..) : return a list of all flow-scoped context property keys

global

    global.get(..) : get a global-scoped context property
    global.set(..) : set a global-scoped context property
    global.keys(..) : return a list of all global-scoped context property keys

hi i just want my charts not to loose their data when node red is restarted ,
this is what i meant by context,

Yes, but - as said - context is a bit vague.

There are three types.
Nodal - only in that node
Flow - accessible to all nodes in that flow
Global - accessible to ALL nodes on ALL flows

Also - and again - I didn't load your code.
But reading that one function node does something and you want that available to another function node, AND the chart: context is NOT exactly the right term.

@jbudd mentioned a bit of your code:

let old = context.get('Day')
let day = new Date().getDay()

if (old != day) {
    context.set('Day', day)
    context.set("Max", -100)
    context.set("Min", 100)
}

let max = context.get('Max')
let min = context.get('Min')

The code has CONTEXT.SET - which is fine in some cases.
But I suspect NOT in yours.

You need FLOW.SET (excusing it being in capitols)

Then the other nodes will have access to that data too.

I've been wrong in the past and will be again. But I am sure this is the cause of a lot of your pain just now.