# Increment/decrement property using core nodes

**URL:** <https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004>\
**Category:** General\
**Created:** [21 January 2022 10:01 UTC](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004 "2022-01-21T10:01:41Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [21 January 2022 15:48 UTC](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004/21 "2022-01-21T15:48:31Z")

</div>

> [@shrickus](#):
>
> There was a recent conversation on slack about adding a `local` context to the `change` node, perfect for things like keeping a counter that can be modified by any expression syntax... perhaps it's time to rethink that as it's a general purpose solution?

It would, at least, be more consistent. It would need adding to JSONata as well though wouldn't it - or is it already accessible there, I can't remember.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [21 January 2022 15:54 UTC](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004/22 "2022-01-21T15:54:31Z")

</div>

not sure how local context would help in this instance as unless the constant to be added/subtracted was sent on the previous message it would then get overwritten by the new message reseting the context. (but maybe again it's Friday and I'm missing something). I think the idea of a separate numbers only Math **s** node is probably the right one.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [21 January 2022 16:13 UTC](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004/23 "2022-01-21T16:13:41Z")

</div>

> [@dceejay](#):
>
> ... oh and then combinations like multiply and add etc

I could see how you could have something like the change node to build up an operation from msg.property/context.var/constant value/list of operations.  
Then ability to add another operation, but this would require some thought about where the ongiong result is held. As after number of operations it could become difficult to follow.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 January 2022 17:15 UTC](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004/24 "2022-01-21T17:15:32Z")

</div>

Provided the stuff you want to run repeatedly does not trash msg properties that do not concern it (which it shouldn't) then you can do it like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/6/064a306c5f57c214e80c8020db5e6360835e9c0c.png)

```auto
[{"id":"c9ef69f62e5ac84b","type":"inject","z":"bdd7be38.d3b55","name":"Go","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":90,"y":2880,"wires":[["56855bac6915ad22"]]},{"id":"56855bac6915ad22","type":"change","z":"bdd7be38.d3b55","name":"Clear msg.count","rules":[{"t":"set","p":"count","pt":"msg","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":260,"y":2880,"wires":[["69479cd5a15f4e13"]]},{"id":"e8e2b9984d4983df","type":"switch","z":"bdd7be38.d3b55","name":">=10?","property":"count","propertyType":"msg","rules":[{"t":"gte","v":"10","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":590,"y":2880,"wires":[["d0d6cf5ab587798c"],["7c136523ddcea5bc"]]},{"id":"7c136523ddcea5bc","type":"change","z":"bdd7be38.d3b55","name":"Increment msg.count","rules":[{"t":"set","p":"count","pt":"msg","to":"count+1","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":2960,"wires":[["dc3b9bf2c42edf87","69479cd5a15f4e13"]]},{"id":"dc3b9bf2c42edf87","type":"debug","z":"bdd7be38.d3b55","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":610,"y":2960,"wires":[]},{"id":"69479cd5a15f4e13","type":"delay","z":"bdd7be38.d3b55","name":"Do stuff","pauseType":"delay","timeout":"1","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":440,"y":2880,"wires":[["e8e2b9984d4983df"]]},{"id":"d0d6cf5ab587798c","type":"debug","z":"bdd7be38.d3b55","name":"DONE","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":740,"y":2880,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [21 January 2022 17:28 UTC](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004/25 "2022-01-21T17:28:23Z")

</div>

🙂  
Although its code - it's pretty much minimal 🙂  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/0/501a237d891f32d4af9de4fa26940164593a0b11.png)

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [21 January 2022 17:34 UTC](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004/26 "2022-01-21T17:34:55Z")

</div>

I suppose you could use a flow variable by doing it like this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1c3b805144c92496c61f3745ffe35ae2b7cc4479.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 March 2022 17:35 UTC](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004/27 "2022-03-22T17:35:41Z")

</div>

Closed on 22 March 2022 17:35 UTC

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

[Previous page](https://discourse.nodered.org/t/increment-decrement-property-using-core-nodes/57004.md?page=1)
