Reset counter - function flow

I have project, but when I do deployed counter reset to 0, but timer switch not reset.

  • I would like when I do deployed value counter not change
  • timer switch reset counter per times
[{"id":"7f5ecbe2.d53fe4","type":"comment","z":"38efc9c1.772896","name":"Licznik T-element","info":"","x":380,"y":240,"wires":[]},{"id":"e09a7ab7.3a8b48","type":"Datahub Subscribe","z":"38efc9c1.772896","name":"T_element","natc":"747c3a22.736474","topicname":"devicehub.raw.80E89505-B9A2-433B-9E6B-F68AA6BE933A.C6824DD5-A222-4218-B2CD-36F92E41403E","x":300,"y":340,"wires":[["feb21a3d.c68468"]]},{"id":"4d294fa6.8e39","type":"inject","z":"38efc9c1.772896","name":"Power on reset","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":1100,"y":280,"wires":[["59309fe9.aec7c","83fd1130.4599e"]]},{"id":"59309fe9.aec7c","type":"function","z":"38efc9c1.772896","name":"Reset Variable","func":"flow.set('count', function(err, count) {\n    if (err) {\n        node.error(err, msg);\n    } else {\n        // initialise the counter to 0 if it doesn't exist already\n        count = count || 0;\n        count += 1;\n        // store the value back\n        context.set('count',count, function(err) {\n            if (err) {\n                node.error(err, msg);\n            } else {\n                // make it part of the outgoing msg object\n                msg.count = count;\n                // send the message\n                node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n                return msg;\n            }\n        });\n    }\n});","outputs":1,"noerr":0,"x":1225,"y":220,"wires":[[]]},{"id":"83fd1130.4599e","type":"ui_gauge","z":"38efc9c1.772896","name":"KIL1","group":"17f4b089.05562f","order":1,"width":"0","height":"0","gtype":"gage","title":"","label":"people","format":"{{value}}","min":0,"max":"1000","colors":["#ff0000","#ffff00","#008000"],"seg1":"500","seg2":"800","x":1240,"y":340,"wires":[]},{"id":"b40856dd.d76258","type":"timerswitch","z":"38efc9c1.772896","name":"","ontopic":"","offtopic":"","onpayload":"0","offpayload":"1","disabled":false,"schedules":[{"on_h":"13","on_m":"00","on_s":"00","off_h":"13","off_m":"00","off_s":"01","valid":true},{"on_h":"21","on_m":"00","on_s":"00","off_h":"21","off_m":"00","off_s":"01","valid":true},{"on_h":"05","on_m":"00","on_s":"00","off_h":"05","off_m":"00","off_s":"01","valid":true}],"x":1100,"y":180,"wires":[["59309fe9.aec7c"]]},{"id":"e77a21a1.3780e","type":"json","z":"38efc9c1.772896","name":"JSON to Object","property":"payload","action":"obj","pretty":false,"x":460,"y":340,"wires":[["f64e53f7.cb911","d700ad09.0ee73"]]},{"id":"d700ad09.0ee73","type":"function","z":"38efc9c1.772896","name":"NewName_SL","func":" const myObj = msg.payload;\n const text = myObj.tagName;\n    \n\t\tif(text.startsWith(\"0#\") == true){\n\t\t\n\t\t\t//dla wartości 0#\n\t\t\t    const restText = text.substr(\"0#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n        else if(text.startsWith(\"1#\") == true){\n\t\t\n\t\t\t//dla wartości 1# - dzielenie wartości przez 10\n\t\t    \tconst restText = text.substr(\"1#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/10;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"2#\") == true){\n\t\t\n\t\t\t//dla wartości 2# - dzielenie wartości przez 100\n\t\t    \tconst restText = text.substr(\"2#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/100;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"3#\") == true){\n\t\t\n\t\t\t//dla wartości 3# - dzielenie wartości przez 1000\n\t\t    \tconst restText = text.substr(\"3#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1000;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"#\", 1 ) == false){\n\t\t\n\t\t\t//brak przedrostka\n\t\t\tmsg.payload = myObj;\n\t\t\treturn msg;\n\t\t    }","outputs":1,"noerr":0,"x":585,"y":340,"wires":[["a658672c.9f8ea8","4608966f.5ac0f8"]]},{"id":"a658672c.9f8ea8","type":"function","z":"38efc9c1.772896","name":"TagTab","func":"const myObj = msg.payload;\nconst val = JSON.parse(myObj.value);\nconst top = myObj.tagName;\n\n    msg.topic = \"KIL1/SK\";\n    msg.payload = val;\n    return msg;","outputs":1,"noerr":0,"x":705,"y":340,"wires":[["c4c99983.720748"]]},{"id":"4608966f.5ac0f8","type":"debug","z":"38efc9c1.772896","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":780,"y":260,"wires":[]},{"id":"feb21a3d.c68468","type":"rbe","z":"38efc9c1.772896","name":"","func":"rbei","gap":"","start":"","inout":"out","property":"payload","x":385,"y":340,"wires":[["e77a21a1.3780e"]]},{"id":"f31ca097.59c4a","type":"function","z":"38efc9c1.772896","name":"counter_MQTT","func":"const myObj = msg.payload;\nconst tmp = \"Licznik_T_element\";\n\n    //aby wyswietlac dane tylko z danego sterownika do MQTT\nmsg.topic = \"KIL1/SK\";\n\nlet text = {};\nlet text1 = {};\n\n\n        let mykey = tmp; \n        text[mykey] = myObj;\n            \n                    text1 = JSON.stringify([text]);\n                   let obiekt = JSON.parse(text1);\n        \n    msg.payload = obiekt;\n    // send the message\n                node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n    return msg;","outputs":1,"noerr":0,"x":885,"y":180,"wires":[["2c57d3c9.96387c"]]},{"id":"f64e53f7.cb911","type":"debug","z":"38efc9c1.772896","name":"","active":false,"console":"false","complete":"false","x":560,"y":260,"wires":[]},{"id":"c4c99983.720748","type":"function","z":"38efc9c1.772896","name":"contex count","func":"flow.get('count', function(err, count) {\n    if (err) {\n        node.error(err, msg);\n    } else {\n        // initialise the counter to 0 if it doesn't exist already\n        count = flow.get(\"count\") || 0;\n        count += msg.payload;\n        count = Math.max(count,0);\n        // store the value back\n        flow.set(\"count\", count);\n        context.set('count',count, function(err) {\n            if (err) {\n                node.error(err, msg);\n            } else {\n                // make it part of the outgoing msg object\n                msg.payload=count;\n                // send the message\n                node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n                //return msg;\n            }\n        });\n    }\n});\n\n\n//msg.payload;\n//return msg;","outputs":1,"noerr":0,"x":805,"y":340,"wires":[["f31ca097.59c4a","83fd1130.4599e"]]},{"id":"cff667d.e46b898","type":"debug","z":"38efc9c1.772896","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1060,"y":100,"wires":[]},{"id":"2c57d3c9.96387c","type":"rbe","z":"38efc9c1.772896","name":"","func":"rbei","gap":"","start":"","inout":"out","property":"payload","x":965,"y":180,"wires":[["cff667d.e46b898"]]},{"id":"747c3a22.736474","type":"Datahub Connect","z":"","ipadd":"127.0.0.1","port":"4222"},{"id":"17f4b089.05562f","type":"ui_group","z":"","name":"Shot","tab":"4249a244.3fe8ac","order":4,"disp":true,"width":"6","collapse":false},{"id":"4249a244.3fe8ac","type":"ui_tab","z":"","name":"HOME","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Hello. It is good practice to properly format flows and code in the forum... using the </> icon

See #3 - Get involved : Node-RED

@cysoz It would be helpful if you could provide a simple example showing the issue.

I prepare signal 1 and 0, counter go up, but I do deploy - counter reset
I would like value not reset deploy only timer

[{"id":"322e1397.3f7a2c","type":"comment","z":"77c1d04c.07025","name":"Licznik T-element","info":"","x":320,"y":100,"wires":[]},{"id":"8519564a.e86438","type":"inject","z":"77c1d04c.07025","name":"Power on reset","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":0.1,"x":1040,"y":220,"wires":[["4d298426.ace5bc","e465d7b3.e8e3c8"]]},{"id":"4d298426.ace5bc","type":"function","z":"77c1d04c.07025","name":"Reset Variable","func":"flow.set('count', function(err, count) {\n    if (err) {\n        node.error(err, msg);\n    } else {\n        // initialise the counter to 0 if it doesn't exist already\n        count = count || 0;\n        count += 1;\n        // store the value back\n        context.set('count',count, function(err) {\n            if (err) {\n                node.error(err, msg);\n            } else {\n                // make it part of the outgoing msg object\n                msg.count = count;\n                // send the message\n                node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n                return msg;\n            }\n        });\n    }\n});","outputs":1,"noerr":0,"x":1165,"y":160,"wires":[[]]},{"id":"e465d7b3.e8e3c8","type":"ui_gauge","z":"77c1d04c.07025","name":"KIL1","group":"17f4b089.05562f","order":1,"width":"0","height":"0","gtype":"gage","title":"","label":"people","format":"{{value}}","min":0,"max":"1000","colors":["#ff0000","#ffff00","#008000"],"seg1":"500","seg2":"800","x":1180,"y":280,"wires":[]},{"id":"ca085cc6.beaf3","type":"timerswitch","z":"77c1d04c.07025","name":"","ontopic":"","offtopic":"","onpayload":"0","offpayload":"1","disabled":false,"schedules":[{"on_h":"13","on_m":"00","on_s":"00","off_h":"13","off_m":"00","off_s":"01","valid":true},{"on_h":"21","on_m":"00","on_s":"00","off_h":"21","off_m":"00","off_s":"01","valid":true},{"on_h":"05","on_m":"00","on_s":"00","off_h":"05","off_m":"00","off_s":"01","valid":true}],"x":1040,"y":120,"wires":[["4d298426.ace5bc"]]},{"id":"74e874b5.96e90c","type":"json","z":"77c1d04c.07025","name":"JSON to Object","property":"payload","action":"obj","pretty":false,"x":400,"y":280,"wires":[["9552ded5.2aaeb","2f7c8d24.0a77e2"]]},{"id":"2f7c8d24.0a77e2","type":"function","z":"77c1d04c.07025","name":"NewName_SL","func":" const myObj = msg.payload;\n const text = myObj.tagName;\n    \n\t\tif(text.startsWith(\"0#\") == true){\n\t\t\n\t\t\t//dla wartości 0#\n\t\t\t    const restText = text.substr(\"0#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n        else if(text.startsWith(\"1#\") == true){\n\t\t\n\t\t\t//dla wartości 1# - dzielenie wartości przez 10\n\t\t    \tconst restText = text.substr(\"1#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/10;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"2#\") == true){\n\t\t\n\t\t\t//dla wartości 2# - dzielenie wartości przez 100\n\t\t    \tconst restText = text.substr(\"2#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/100;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"3#\") == true){\n\t\t\n\t\t\t//dla wartości 3# - dzielenie wartości przez 1000\n\t\t    \tconst restText = text.substr(\"3#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1000;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"#\", 1 ) == false){\n\t\t\n\t\t\t//brak przedrostka\n\t\t\tmsg.payload = myObj;\n\t\t\treturn msg;\n\t\t    }","outputs":1,"noerr":0,"x":525,"y":280,"wires":[["2e5dc2ec.63cfae","b5e2cf69.d11da"]]},{"id":"2e5dc2ec.63cfae","type":"function","z":"77c1d04c.07025","name":"TagTab","func":"const myObj = msg.payload;\nconst val = JSON.parse(myObj.value);\nconst top = myObj.tagName;\n\n    msg.topic = \"KIL1/SK\";\n    msg.payload = val;\n    return msg;","outputs":1,"noerr":0,"x":645,"y":280,"wires":[["ccf2dd52.f9151"]]},{"id":"b5e2cf69.d11da","type":"debug","z":"77c1d04c.07025","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":720,"y":200,"wires":[]},{"id":"f0c33295.afa48","type":"function","z":"77c1d04c.07025","name":"counter_MQTT","func":"const myObj = msg.payload;\nconst tmp = \"Licznik_T_element\";\n\n    //aby wyswietlac dane tylko z danego sterownika do MQTT\nmsg.topic = \"KIL1/SK\";\n\nlet text = {};\nlet text1 = {};\n\n\n        let mykey = tmp; \n        text[mykey] = myObj;\n            \n                    text1 = JSON.stringify([text]);\n                   let obiekt = JSON.parse(text1);\n        \n    msg.payload = obiekt;\n    // send the message\n                node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n    return msg;","outputs":1,"noerr":0,"x":825,"y":120,"wires":[["a7d880d3.f6cff"]]},{"id":"9552ded5.2aaeb","type":"debug","z":"77c1d04c.07025","name":"","active":false,"console":"false","complete":"false","x":500,"y":200,"wires":[]},{"id":"ccf2dd52.f9151","type":"function","z":"77c1d04c.07025","name":"contex count","func":"flow.get('count', function(err, count) {\n    if (err) {\n        node.error(err, msg);\n    } else {\n        // initialise the counter to 0 if it doesn't exist already\n        count = flow.get(\"count\") || 0;\n        count += msg.payload;\n        count = Math.max(count,0);\n        // store the value back\n        flow.set(\"count\", count);\n        context.set('count',count, function(err) {\n            if (err) {\n                node.error(err, msg);\n            } else {\n                // make it part of the outgoing msg object\n                msg.payload=count;\n                // send the message\n                node.status({fill:\"blue\",shape:\"dot\",text: msg.payload});\n                //return msg;\n            }\n        });\n    }\n});\n\n\n//msg.payload;\n//return msg;","outputs":1,"noerr":0,"x":745,"y":280,"wires":[["f0c33295.afa48","e465d7b3.e8e3c8"]]},{"id":"2e56eb7.879e314","type":"debug","z":"77c1d04c.07025","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1000,"y":40,"wires":[]},{"id":"a7d880d3.f6cff","type":"rbe","z":"77c1d04c.07025","name":"","func":"rbei","gap":"","start":"","inout":"out","property":"payload","x":905,"y":120,"wires":[["2e56eb7.879e314"]]},{"id":"96b372c7.98a78","type":"inject","z":"77c1d04c.07025","name":"1","topic":"","payload":"{\"deviceName\": \"KIL1_Kinematic\", \"tagName\": \"0#T_Element_Detection\", \"deviceID\": \"80E89505-B9A2-433B-9E6B-F68AA6BE933A\", \"success\": true, \"datatype\": \"bit\", \"timestamp\": 1632895661475, \"value\": 1, \"registerId\": \"C6824DD5-A222-4218-B2CD-36F92E41403E\", \"description\": \"T-element pulse Reference\"}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":120,"y":240,"wires":[["74e874b5.96e90c"]]},{"id":"62b312c2.af46cc","type":"inject","z":"77c1d04c.07025","name":"0","topic":"","payload":"{\"deviceName\": \"KIL1_Kinematic\", \"tagName\": \"0#T_Element_Detection\", \"deviceID\": \"80E89505-B9A2-433B-9E6B-F68AA6BE933A\", \"success\": true, \"datatype\": \"bit\", \"timestamp\": 1632895661472, \"value\": 0, \"registerId\": \"C6824DD5-A222-4218-B2CD-36F92E41403E\", \"description\": \"T-element pulse Reference\"}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":340,"wires":[["74e874b5.96e90c"]]},{"id":"17f4b089.05562f","type":"ui_group","z":"","name":"Shot","tab":"4249a244.3fe8ac","order":4,"disp":true,"width":"6","collapse":false},{"id":"4249a244.3fe8ac","type":"ui_tab","z":"","name":"HOME","icon":"dashboard","order":1,"disabled":false,"hidden":false}]

Do you do full deploy, modified flows or modified nodes?.

Full deploy will reinitialise every node

Flow deploy will reinitialise any modified nodes and connected nodes.

Node deploy will only reinitialise the modified nodes.

See this video for a demonstration: Deploying flows - Node-RED Essentials - YouTube

Thank you,

  • I used full deploy, when I changed modified nodes is good.

secound problem is why timer switch not reset counter?

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