Hall effect sensor counter!

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!

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.

Example flow;

[{"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":[[]]}]
1 Like

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

Thanks for helping out :+1:

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.

Counting the numerical content of the message.
Will dig out a sample :+1:

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?

1 Like

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

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!

Has @bakman2's flow got you going then?

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

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.

1 Like

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.

Hi guys.... So I've added

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!

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.

1 Like

I often just use node-red-contrib-key-value-store when I want only a handful of persistent values.

1 Like

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

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.

1 Like

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

The arduino sketch for my nodeMCU: 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.

1 Like

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