# Automatic fish feeder

**URL:** https://discourse.nodered.org/t/automatic-fish-feeder/8612
**Category:** Forum Help
**Created:** [4 March 2019 20:33 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612 "2019-03-04T20:33:23Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [4 March 2019 20:33 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/1 "2019-03-04T20:33:23Z")

</div>

Hello all,

I need some help I'm new too coding and node red.  
I am basically after advice.

So a little about what I want to achieve the fish feeder has = 250 feeds and each time the msg.payload = true de-increment by 1 but also the 250 will need to be reset so when it gets a signal from a UI button it resets the reduced 250 to 250.

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [4 March 2019 20:39 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/2 "2019-03-04T20:39:07Z")

</div>

Have a look at [flow context](https://nodered.org/docs/user-guide/context) in the docs.  
You use the UI button to change the saved context value to 250, and use the msg.payload to decrement the context value by 1.

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [4 March 2019 20:46 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/3 "2019-03-04T20:46:24Z")

</div>

Thanks, @Paul-Reed so do I need to make a file store to save the context data?

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [4 March 2019 20:52 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/4 "2019-03-04T20:52:23Z")

</div>

That depends on how resilient you want things to be. The docs say  
_"By default, context is stored in memory only"_  
...which means that you would lose context data if you rebooted your system.  
If however you setup a file based store, the context value would be saved to disk, and therefore would survive a reboot.

---

<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: [4 March 2019 21:06 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/5 "2019-03-04T21:06:03Z")

</div>

the latest version of Node-RED let you configure the flow and global contexts to use file or memory storage so you can persist things over a power outage more easily.  
(but maybe just get it working first 🙂

Nice post about it here - [A guide to understanding 'Persistent Context'](https://discourse.nodered.org/t/a-guide-to-understanding-persistent-context/4115)

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [4 March 2019 22:32 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/6 "2019-03-04T22:32:28Z")

</div>

```auto
[{"id":"222545a6.ab604a","type":"debug","z":"1b706be0.aa739c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":740,"y":440,"wires":[]},{"id":"9d4a09ee.86667","type":"inject","z":"1b706be0.aa739c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":300,"y":440,"wires":[["b4090c34.2836a8"]]},{"id":"bd9967b1.9be7f8","type":"inject","z":"1b706be0.aa739c","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":240,"y":560,"wires":[["2b696141.0d93c6"]]},{"id":"b4090c34.2836a8","type":"function","z":"1b706be0.aa739c","name":"","func":"\nflow.set(\"FishMax\", 250, \"FishFeederMax\");\nflow.set(\"FishResult\", 10, \"FishFeederResult\");\nflow.set(\"reduce\", 1, \"FishFoodReduce\");","outputs":1,"noerr":0,"x":500,"y":440,"wires":[["222545a6.ab604a"]]},{"id":"2b696141.0d93c6","type":"function","z":"1b706be0.aa739c","name":"","func":"var mo = flow.get([\"FishMax\", \"FishResult\", \"reduce\"]);\nvar po = flow.get(\"FishMax\" - \"reduce\");\n\nmsg.payload = mo;\n\nreturn msg;","outputs":1,"noerr":0,"x":500,"y":580,"wires":[["82bf53f7.d78c7"]]},{"id":"82bf53f7.d78c7","type":"debug","z":"1b706be0.aa739c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":720,"y":580,"wires":[]}]

```

So in my settings.js i have created a context file save, and the fucntion node returns the values i set into the function with flow.set, so slowly getting there now whats the best way to reduce the intial amount

Flow.get ("1") - Flow.get("2") = Flow.set("3")

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [4 March 2019 22:45 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/7 "2019-03-04T22:45:18Z")

</div>

Don't know why you have 3 flow contexts...  
Why not just have one, and decrement it or reset it to 250?

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [4 March 2019 23:25 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/8 "2019-03-04T23:25:25Z")

</div>

For example;

```auto
[{"id":"37a92ccd.38ce44","type":"inject","z":"644f7043.705ed","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":490,"wires":[["b69eeef6.0e0c3"]]},{"id":"b69eeef6.0e0c3","type":"change","z":"644f7043.705ed","name":"Set to 250","rules":[{"t":"set","p":"fishfood","pt":"flow","to":"250","tot":"num"}],"action":"","property":"","from":"","to":"","reg":false,"x":310,"y":490,"wires":[[]]},{"id":"ab6af0b8.9e76f","type":"inject","z":"644f7043.705ed","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":140,"y":530,"wires":[["6906b630.118378"]]},{"id":"6906b630.118378","type":"function","z":"644f7043.705ed","name":"Decrement by 1","func":"var newValue = (flow.get(\"fishfood\")-1);\nflow.set(\"fishfood\", newValue);\nmsg.payload = newValue;\nreturn msg;","outputs":1,"noerr":0,"x":320,"y":530,"wires":[["482a0e1e.71988"]]},{"id":"482a0e1e.71988","type":"debug","z":"644f7043.705ed","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":520,"y":530,"wires":[]}]

```

---

<div class="post-metadata">

### Author: ![rburton](https://avatars.discourse-cdn.com/v4/letter/r/ec9cab/32.png) [@rburton](https://discourse.nodered.org/u/rburton)
#### Post date: [7 March 2019 22:58 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/9 "2019-03-07T22:58:02Z")

</div>

Excellent @Paul-Reed it's all working as I need, I made a file storage as advised by @dceejay and that's all working too thanks for your time.

I'm looking at using a yf-s201 flow meter it uses a pulse output has anyone ever used one or know of any info on how to set up?

---

<div class="post-metadata">

### Author: ![oncdastan](https://avatars.discourse-cdn.com/v4/letter/o/34f0e0/32.png) [@oncdastan](https://discourse.nodered.org/u/oncdastan)
#### Post date: [24 July 2020 08:56 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/10 "2020-07-24T08:56:42Z")

</div>

Hi. I want to use YF-S201 too. I found this one:

> **[water-flow-sensor](https://www.npmjs.com/package/water-flow-sensor)**
>
> Get flow rate and volume consumption from water flow sensors. Latest version: 1.0.1, last published: 7 years ago. Start using water-flow-sensor in your project by running \`npm i water-flow-sensor\`. There are no other projects in the npm registry...

Were you able find and use one? If so, can you help me on the following post:

> [@YF-S201 Water Flow Sensor - Help with Require](https://discourse.nodered.org/t/yf-s201-water-flow-sensor-help-with-require/30587/4):
>
> Thanks for the reply. Can you still guide me? Maybe it could work. Also, I must use a water flow sensor. Are there any sensors that you would recommend?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 July 2020 09:58 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/11 "2020-07-24T09:58:23Z")

</div>

Please don't post the same question in multiple threads. I'm closing this one since it is a year old

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [24 July 2020 09:58 UTC](https://discourse.nodered.org/t/automatic-fish-feeder/8612/12 "2020-07-24T09:58:28Z")

</div>


