# Hall effect sensor counter!

**URL:** <https://discourse.nodered.org/t/hall-effect-sensor-counter/27199>\
**Category:** General\
**Created:** [24 May 2020 15:57 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199 "2020-05-24T15:57:37Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [24 May 2020 15:57 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/1 "2020-05-24T15:57:37Z")

</div>

Hello folks!  
Hope all are well.

Node Red has basically taken over and I’m using it for most things in my home automation system! Loving it.

What I’m looking for, is the ability to count a bunch of pulse results from a Hall effect sensor (water use encoder). Adding all payloads and then resetting maybe once a week or month or so.

This node red counter needs to persist thru re-deploys and reboots.

Anyone point me in a direction to start?

All the best!

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [24 May 2020 16:45 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/2 "2020-05-24T16:45:14Z")

</div>

Depending on "what" you are counting, you can keep track using context.  
To survive a reboot you will need to [enable persistence in settings.js](https://nodered.org/docs/user-guide/context).

Example flow;

```auto
[{"id":"ced84e05.c6a64","type":"inject","z":"cd26bb43.030ba8","name":"pulse","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":218,"y":216,"wires":[["6a7d4f4a.2cb5d"]]},{"id":"7ab0062b.031708","type":"change","z":"cd26bb43.030ba8","name":"","rules":[{"t":"set","p":"pulse","pt":"flow","to":"$flowContext(\"pulse\")+1\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":192,"wires":[[]]},{"id":"40bf9aeb.8594cc","type":"inject","z":"cd26bb43.030ba8","name":"","topic":"","payload":"pulse","payloadType":"flow","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":228,"y":312,"wires":[["845c2c98.a5e4c"]]},{"id":"845c2c98.a5e4c","type":"debug","z":"cd26bb43.030ba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":396,"y":312,"wires":[]},{"id":"6a7d4f4a.2cb5d","type":"switch","z":"cd26bb43.030ba8","name":"","property":"pulse","propertyType":"flow","rules":[{"t":"nnull"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":362,"y":216,"wires":[["7ab0062b.031708"],["19107ea0.321241"]]},{"id":"19107ea0.321241","type":"change","z":"cd26bb43.030ba8","name":"set 0 if flow.pulse does not exist","rules":[{"t":"set","p":"pulse","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":240,"wires":[[]]}]

```

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [24 May 2020 17:16 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/3 "2020-05-24T17:16:23Z")

</div>

It’s just counting a bunch of pulse numerical results.  
Will take a look at that flow!

Thanks for helping out 👍

---

<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:** [24 May 2020 19:33 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/4 "2020-05-24T19:33:16Z")

</div>

> [@Mobile\_NR\_Env](#):
>
> counting a bunch of pulse numerical results.

Do you mean it is counting the number of messages received, or has it got to accumulate the 'numerical results', or something else? Show us some sample data to help us understand.

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [24 May 2020 20:18 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/5 "2020-05-24T20:18:03Z")

</div>

Counting the numerical content of the message.  
Will dig out a sample 👍

---

<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:** [24 May 2020 20:46 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/6 "2020-05-24T20:46:52Z")

</div>

I think you had better do so. Counting the numerical content doesn't mean anything. Do you mean adding them all up, so if the payload values of successive messages are 10,20,30 then the answer is 60?

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [25 May 2020 13:01 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/7 "2020-05-25T13:01:30Z")

</div>

So basically the hall effect sensor sends pulses as water flows.  
The Wemos d1 collects these pulse readings (applies a formula) and returns a numeric value in litres to MQTT topic (every 60 seconds).

MQTT result ie: 77.02 the next minute it may only return 2.01 next minute 21.22 and so on.

So the increasing result would be 100.25.... when no water is used it returns 0.00

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [25 May 2020 13:25 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/8 "2020-05-25T13:25:00Z")

</div>

> [@bakman2](#):
>
> `[{"id":"ced84e05.c6a64","type":"inject","z":"cd26bb43.030ba8","name":"pulse","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":218,"y":216,"wires":[["6a7d4f4a.2cb5d"]]},{"id":"7ab0062b.031708","type":"change","z":"cd26bb43.030ba8","name":"","rules":[{"t":"set","p":"pulse","pt":"flow","to":"$flowContext(\"pulse\")+1\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":560,"y":192,"wires":[[]]},{"id":"40bf9aeb.8594cc","type":"inject","z":"cd26bb43.030ba8","name":"","topic":"","payload":"pulse","payloadType":"flow","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":228,"y":312,"wires":[["845c2c98.a5e4c"]]},{"id":"845c2c98.a5e4c","type":"debug","z":"cd26bb43.030ba8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":396,"y":312,"wires":[]},{"id":"6a7d4f4a.2cb5d","type":"switch","z":"cd26bb43.030ba8","name":"","property":"pulse","propertyType":"flow","rules":[{"t":"nnull"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":362,"y":216,"wires":[["7ab0062b.031708"],["19107ea0.321241"]]},{"id":"19107ea0.321241","type":"change","z":"cd26bb43.030ba8","name":"set 0 if flow.pulse does not exist","rules":[{"t":"set","p":"pulse","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":240,"wires":[[]]}]`

Great bit of node work there! Very useful.  
As I was rather vague at the start. There's a bit more info on the process below.  
Thanks for writing this flow. It will be useful in the future!

---

<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:** [25 May 2020 13:33 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/9 "2020-05-25T13:33:23Z")

</div>

Has @bakman2's flow got you going then?

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [25 May 2020 14:25 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/10 "2020-05-25T14:25:17Z")

</div>

For this use. I don’t think so. Will have to work out how’s its doing what its doing! Still have lots to learn

---

<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:** [25 May 2020 14:43 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/11 "2020-05-25T14:43:59Z")

</div>

Have a look at the node red docs page Writing Functions, that tells you how to save a value in the node context. Then each time a new one arrives you can add the new value to the saved value, so building up your accumulated total.

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [25 May 2020 14:58 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/12 "2020-05-25T14:58:08Z")

</div>

> [@Colin](#):
>
> Has @bakman2's flow got you going then?

Correction. Yes it has! Have made a small mod to his code.  
From:

`$flowContext("pulse")+1`

To:  
`$flowContext("waterpulse")+payload`

Now have a manual reset of the context stored using a change node.

Thanks guys.

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [25 May 2020 19:08 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/13 "2020-05-25T19:08:15Z")

</div>

> [@bakman2](#):
>
> To survive a reboot you will need to [enable persistence in settings.js](https://nodered.org/docs/user-guide/context)

Hi guys.... So I've added

```auto
contextStorage: {
   default: {
       module: "localfilesystem"
   }
}

```

To bottom of my settings.js file and rebooted node-red.

The data stored in the flow is still reset on reboot. Leaving the count back at 0.  
Any tips?

Heres the flow so far:

`[{"id":"64d121c6.a5d788","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"eac55f79.66c4c8","type":"change","z":"64d121c6.a5d788","name":"Delete Context Store","rules":[{"t":"delete","p":"waterpulse","pt":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":460,"wires":[[]]},{"id":"e92d8702.0a56","type":"inject","z":"64d121c6.a5d788","name":"Reset the store","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":460,"wires":[["eac55f79.66c4c8"]]},{"id":"4137d385.337b24","type":"debug","z":"64d121c6.a5d788","name":"Set","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":790,"y":180,"wires":[]},{"id":"d7f692eb.5c92e8","type":"inject","z":"64d121c6.a5d788","name":"Inject pulse at boot","topic":"","payload":"0","payloadType":"num","repeat":"","crontab":"","once":true,"onceDelay":"11","x":170,"y":240,"wires":[["b9debb4a.a3cc4"]]},{"id":"575c606f.1ad49","type":"change","z":"64d121c6.a5d788","name":"","rules":[{"t":"set","p":"waterpulse","pt":"flow","to":"$flowContext(\"waterpulse\")+payload\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":610,"y":180,"wires":[["4137d385.337b24"]]},{"id":"b9debb4a.a3cc4","type":"switch","z":"64d121c6.a5d788","name":"","property":"waterpulse","propertyType":"flow","rules":[{"t":"nnull"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":390,"y":180,"wires":[["575c606f.1ad49"],["9ba93b30.489928"]]},{"id":"9ba93b30.489928","type":"change","z":"64d121c6.a5d788","name":"set 0 if flow.pulse does not exist","rules":[{"t":"set","p":"waterpulse","pt":"flow","to":"0","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":650,"y":240,"wires":[[]]},{"id":"4a23c523.06fb2c","type":"inject","z":"64d121c6.a5d788","name":"60 Sec inject","topic":"","payload":"waterpulse","payloadType":"flow","repeat":"60","crontab":"","once":true,"onceDelay":0.1,"x":160,"y":340,"wires":[["24a7a485.310394","c0d6b244.dde638"]]},{"id":"24a7a485.310394","type":"debug","z":"64d121c6.a5d788","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":438.5714416503906,"y":388.571533203125,"wires":[]},{"id":"8a9d435a.fdb878","type":"link out","z":"64d121c6.a5d788","name":"","links":["5e0c1af.06430e4"],"x":623.5714416503906,"y":328.571533203125,"wires":[]},{"id":"f9f5110a.f4fb28","type":"change","z":"64d121c6.a5d788","name":"Publish","rules":[{"t":"set","p":"topic","pt":"msg","to":"sensor/water_meter_encoder/stored","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":528.5714416503906,"y":328.571533203125,"wires":[["8a9d435a.fdb878"]]},{"id":"c0d6b244.dde638","type":"function","z":"64d121c6.a5d788","name":"Convert","func":"msg.payload = ((msg.payload)).toFixed(2);\nreturn msg;\n","outputs":1,"noerr":0,"x":388.5714416503906,"y":328.571533203125,"wires":[["f9f5110a.f4fb28"]]},{"id":"3eebf212.83aa66","type":"comment","z":"64d121c6.a5d788","name":"Publish totals to MQTT","info":"","x":160,"y":300,"wires":[]},{"id":"621a998d.6e6c68","type":"comment","z":"64d121c6.a5d788","name":"Reset the counter","info":"","x":150,"y":420,"wires":[]},{"id":"1e4a0142.545cd7","type":"comment","z":"64d121c6.a5d788","name":"MQTT Liters into counter","info":"","x":170,"y":140,"wires":[]},{"id":"14c03aa4.94bf35","type":"inject","z":"64d121c6.a5d788","name":"MQTT Input","topic":"","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":150,"y":180,"wires":[["b9debb4a.a3cc4"]]}]`

Cheers!

---

<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:** [25 May 2020 19:57 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/14 "2020-05-25T19:57:49Z")

</div>

> [@Mobile\_NR\_Env](#):
>
> To bottom of my settings.js file

Hopefully not right at the bottom, you should edit the existing contextStorage definition the way you want. Check the log when you restart node-red to make sure it is showing the correct storage option.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [25 May 2020 21:05 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/15 "2020-05-25T21:05:33Z")

</div>

I often just use [node-red-contrib-key-value-store](https://flows.nodered.org/node/node-red-contrib-key-value-store) when I want only a handful of persistent values.

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [26 May 2020 11:02 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/16 "2020-05-26T11:02:41Z")

</div>

Looks like a good node Steve. Definitely one to have a play with!

Turns out I’ve a “settings.js” in root/.~node-red folder and home/user/.~node-red as the installation process I used put the systemd startup to run from the latter folder.

All working on the flow above👍

Thanks again chaps

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [26 May 2020 11:56 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/17 "2020-05-26T11:56:31Z")

</div>

> [@Mobile\_NR\_Env](#):
>
> So basically the hall effect sensor sends pulses as water flows.  
> The Wemos d1 collects these pulse readings (applies a formula) and returns a numeric value in litres to MQTT topic (every 60 seconds).
> 
> MQTT result ie: 77.02 the next minute it may only return 2.01 next minute 21.22 and so on.
> 
> So the increasing result would be 100.25.... when no water is used it returns 0.00

I have also deployed a hall effect sensor connected to a nodeMCU to measure the pulses as water flows, but in my case I am not publishing every minute the number of pulses (or litres) that are consumed in the last 60 seconds. Instead I am publishing the **total** pulse count (or litres) it has measured (this number becomes resetted to 0 when the nodeMCU is restarted).

I think this is a better approach as in case of network problems or your node-red application went down you might miss some mqtt messages which makes that you are no longer able to exactly estimate your total water consumption.

You can also easily detect a reset of the nodeMCU as in that case the pulse count won't increase but suddenly drops.

---

<div class="post-metadata">

**Author:** ![Mobile\_NR\_Env](https://avatars.discourse-cdn.com/v4/letter/m/8dc957/32.png) [@Mobile\_NR\_Env](https://discourse.nodered.org/u/Mobile_NR_Env)\
**Post date:** [27 May 2020 14:17 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/18 "2020-05-27T14:17:14Z")

</div>

> [@janvda](#):
>
> Instead I am publishing the **total** pulse count

Sounds interesting! Whats your wemos node config code for achieving the total storage in the wemos?

---

<div class="post-metadata">

**Author:** ![janvda](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/janvda/32/234_2.png) [@janvda](https://discourse.nodered.org/u/janvda)\
**Post date:** [27 May 2020 16:12 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/19 "2020-05-27T16:12:58Z")

</div>

The arduino sketch for my nodeMCU: [https://github.com/janvda/esp8266\_water\_meter](https://github.com/janvda/esp8266_water_meter)

It does some software filtering to get rid of duplicate pulses - but I admit this is not working perfectly.

---

<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:** [26 July 2020 16:13 UTC](https://discourse.nodered.org/t/hall-effect-sensor-counter/27199/20 "2020-07-26T16:13:05Z")

</div>

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