# Counter by Topic on a Rolling Window

**URL:** <https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045>\
**Category:** General\
**Created:** [28 August 2022 23:59 UTC](https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045 "2022-08-28T23:59:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![gta\_br](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@gta\_br](https://discourse.nodered.org/u/gta_br)\
**Post date:** [28 August 2022 23:59 UTC](https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045/1 "2022-08-28T23:59:41Z")

</div>

Hi,

Sorry if it was answered before but I'm stucked with a problem.

Is it possible to implement the solution described in topic [Count the messages uuid values](https://discourse.nodered.org/t/count-the-messages-uuid-values/59019), but on a rolling window? I need to count the messages by topic (uuid on the topic example) but on the last N days.

TIA.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [29 August 2022 03:12 UTC](https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045/2 "2022-08-29T03:12:35Z")

</div>

You may need to expand a bit more on what you are wanting to do.

And: why more than `1` day?

You _open_ the window. You _close_ it.

Search how to post messages here so they are correctly formatted and maybe post a message and elaborate (expand) on what you want to do.

---

<div class="post-metadata">

**Author:** ![Andrei](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/andrei/32/10446_2.png) [@Andrei](https://discourse.nodered.org/u/Andrei)\
**Post date:** [29 August 2022 03:53 UTC](https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045/3 "2022-08-29T03:53:24Z")

</div>

I can imagine a solution where you store the incoming data in a CSV file. A specific function would then recover and count topics for the last N days. You could transform the timestamp in the date of the month (at the time you save data to the CSV file) to easy the data recovery (avoiding timestamp calculation for each and every data recovery).

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [29 August 2022 08:01 UTC](https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045/4 "2022-08-29T08:01:57Z")

</div>

You could store the topic message time in an context store object by array. You can then filter the array by a time limit, which would be your rolling window.  
In this example the window is 1 minute, for testing

```auto
[{"id":"1c503a87.b15805","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"one","payload":"test","payloadType":"str","x":140,"y":980,"wires":[["f3477ed6.6151"]]},{"id":"f3477ed6.6151","type":"function","z":"30af2d3e.d94ea2","name":"","func":"const time_window = 60000;\nlet topic_count = flow.get(\"topic_count.\" + msg.topic) || [];\nconst time_now = new Date().valueOf();\ntopic_count = topic_count.filter(time => time > (time_now-time_window));\ntopic_count.push(time_now);\nmsg.payload = topic_count.length;\nflow.set(\"topic_count.\" + msg.topic, topic_count)\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":1000,"wires":[["2dfdae9e.4eab8a"]]},{"id":"39e839d4.2a9bc6","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"two","payload":"another","payloadType":"str","x":150,"y":1040,"wires":[["f3477ed6.6151"]]},{"id":"2dfdae9e.4eab8a","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":1160,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![gta\_br](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@gta\_br](https://discourse.nodered.org/u/gta_br)\
**Post date:** [29 August 2022 09:57 UTC](https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045/5 "2022-08-29T09:57:18Z")

</div>

My gate controller sends a JSON message each time it's activated. The IP is assigned by DHCP and the message format is:

{"gate\_activation":{"ip\_address":"192.168.1.XX","gate":"Car"}}  
{"gate\_activation":{"ip\_address":"192.168.1.YY","gate":"Car"}}  
{"gate\_activation":{"ip\_address":"192.168.1.ZZ","gate":"Car"}}  
{"gate\_activation":{"ip\_address":"192.168.1.XX","gate":"Car"}}  
{"gate\_activation":{"ip\_address":"192.168.1.XX","gate":"Ped"}}

I need to identify the first access of a given IP for the last N days, as he can open the gate on the previous days. The following action would be to send a Pushsafer message.

---

<div class="post-metadata">

**Author:** ![gta\_br](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@gta\_br](https://discourse.nodered.org/u/gta_br)\
**Post date:** [29 August 2022 10:00 UTC](https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045/6 "2022-08-29T10:00:19Z")

</div>

Seems to be the way. Tks.

---

<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:** [12 September 2022 10:01 UTC](https://discourse.nodered.org/t/counter-by-topic-on-a-rolling-window/67045/7 "2022-09-12T10:01:19Z")

</div>

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