One way to have Node-RED react to some groov View action like a button press is to read the data from a groov data store tag, and then make a decision based on the value that is returned, for example fetching some information or activating some function.
Here is a simple example that generates a random number when you press an action button in groov View:
These are the steps the flow goes through to process and react to the button press:
- Node-RED reads the “action” button value once every second.
- The value is ignored unless it has changed compared to the last value.
- The value is accepted if it is
true
, so only when the button is turned on. - The button is reset to
false
AND the random number is generated.
A button gadget is used instead of a momentary gadget so that you just press the button once and it latches on until Node-RED starts the action and toggles it back off. If a momentary gadget were used and pressed for less than one second, it’s possible that Node-RED would never read it!
To set this up in groov View, create or select a groov data store to hold the button variable, in this example I have a “NodeRED” data store with an “action” boolean for the button and a “debug” string tag to hold the result.
Outside of the randomizer this is how you would do some generic action, just replace DO ACTION with what ever task you want it to activate:
Here is the flow you can import into Node-RED:
…
[{"id":"73401010.ffcd1","type":"switch","z":"65e0d8c3.e0ef98","name":"is true","property":"payload","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":470,"y":960,"wires":[["1d2c9b69.c355c5","8abf8602.1fb2b8"]]},{"id":"8db3554b.5ce7c8","type":"rbe","z":"65e0d8c3.e0ef98","name":"","func":"rbei","gap":"","start":"","inout":"out","property":"payload","x":350,"y":960,"wires":[["73401010.ffcd1"]]},{"id":"1d2c9b69.c355c5","type":"groov-write-ds","z":"65e0d8c3.e0ef98","dataStore":"894197e9.8e9408","tagName":"action","tableStartIndex":"","value":"false","valueType":"value","name":"","x":610,"y":940,"wires":[[]]},{"id":"9746615d.73346","type":"groov-read-ds","z":"65e0d8c3.e0ef98","dataStore":"894197e9.8e9408","tagName":"action","tableStartIndex":"","tableLength":"","value":"","valueType":"msg.payload","topic":"","topicType":"none","name":"","x":230,"y":960,"wires":[["8db3554b.5ce7c8"]]},{"id":"c207bdc0.3aa0b","type":"inject","z":"65e0d8c3.e0ef98","name":"1s","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":960,"wires":[["9746615d.73346"]]},{"id":"add4b1cb.2b878","type":"groov-write-ds","z":"65e0d8c3.e0ef98","dataStore":"894197e9.8e9408","tagName":"debug","tableStartIndex":"","value":"","valueType":"msg.payload","name":"output","x":790,"y":1000,"wires":[[]]},{"id":"43bb7a59.8cc854","type":"debug","z":"65e0d8c3.e0ef98","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":790,"y":960,"wires":[]},{"id":"8abf8602.1fb2b8","type":"function","z":"65e0d8c3.e0ef98","name":"randomizer","func":"min = 0;\nmax = 100;\nresult = min + (Math.random() * (max-min));\nreturn { payload : result };","outputs":1,"noerr":0,"x":630,"y":980,"wires":[["add4b1cb.2b878","43bb7a59.8cc854"]]},{"id":"894197e9.8e9408","type":"groov-data-store","z":0,"project":"360f71a2.fb70ee","dsName":"NodeRED"},{"id":"360f71a2.fb70ee","type":"groov-project","z":"","address":"localhost"}]
…
Happy coding!