I am going to give you way more context that you probably need to help me because I feel like executing things the way I immagine is kind of dumb for Node-RED and more suitable to a programming language like C.
In my house I have a room where the lights are under a 4 contact step by step relay so that I have 4 "states", all off, all on, half on and other half on.
I have a Sonoff Mini wired so that it simulates a button press. This way I can control it from Home Assistant. The problem is the sonoff has no idea in what states the lights are and neither does HA.
What I wanna achieve is:
- Know which state the lights are in
- Knowing which state the lights are in, asking for a specific state and having Node Red press the button the correct number of times to reach the desired state
My thinking is (right now I am not concerned on how to interface with HA):
I press a button, said button triggers a counter, based on that counter I know what states the lights are in.
Now that I know what state the lights are in, if I have the lights in all off and want half on, node red needs to press the button until the counter is 2.
Problem is I am already stuck, I have a counter, and I know how to reset said counter (node-red-contrib-counter), I want the counter to reset (return to 0) when the button is pressed and when the counter reaches it's limit. (I am trying to use node-red-contrib-bool-gate, but I also tried node-red-contrib-boolean-logic and still didn't manage to use either of them)
While I would like someone to explain how to use the and gate because at this point I spent more time than what I will admit on it, I think this approach is stupid, I feel like I am not taking advantage of node red, since I am passing around messages it probably makes more sense to have all of this informations in a message and not in a counter from where I will need to interpreter, but I have no idea where to start. So, if someone can point me to the right direction it would be very apreciated.
I am including my flow, it basically only has a not working counter on it
[{"id":"a9fbaedc.8f9c1","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"e6bacb66.65bc38","type":"inject","z":"a9fbaedc.8f9c1","name":"Button Press","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":170,"y":240,"wires":[["f21b4f64.9a67e","5beefa9e.b2cb64"]]},{"id":"f21b4f64.9a67e","type":"counter","z":"a9fbaedc.8f9c1","name":"Counter ","init":"0","step":"1","lower":null,"upper":"3","mode":"increment","outputs":"1","x":400,"y":240,"wires":[["d69c6482.e9b818","f207c0c6.c4fd8"]],"info":"The light I am trying to control has 4 states:\n0 : off\n1 : all on\n2 : right half on\n3 : left half on\n"},{"id":"d69c6482.e9b818","type":"debug","z":"a9fbaedc.8f9c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":530,"y":240,"wires":[]},{"id":"f207c0c6.c4fd8","type":"switch","z":"a9fbaedc.8f9c1","name":"","property":"countUpperLimitReached","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":510,"y":320,"wires":[["1e498df9.5ee082","a5d1d3ac.0c282"]]},{"id":"1e498df9.5ee082","type":"debug","z":"a9fbaedc.8f9c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":710,"y":320,"wires":[]},{"id":"f0e8e408.c96148","type":"change","z":"a9fbaedc.8f9c1","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"true","tot":"bool"},{"t":"set","p":"countUpperLimitReached","pt":"msg","to":"false","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":180,"y":440,"wires":[["f21b4f64.9a67e"]]},{"id":"8b583c6.0dfb2c","type":"inject","z":"a9fbaedc.8f9c1","name":"Reset for counter","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":120,"y":620,"wires":[["f0e8e408.c96148"]]},{"id":"5beefa9e.b2cb64","type":"change","z":"a9fbaedc.8f9c1","name":"","rules":[{"t":"set","p":"topic","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":790,"y":480,"wires":[["7c7b8677.862a98","a5d1d3ac.0c282"]]},{"id":"7c7b8677.862a98","type":"debug","z":"a9fbaedc.8f9c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1030,"y":480,"wires":[]},{"id":"a5d1d3ac.0c282","type":"and-gate","z":"a9fbaedc.8f9c1","name":"","rules":[{"t":"true","propertyType":"msg","property":"countUpperLimitReached","topic":""},{"t":"true","propertyType":"msg","property":"topic","topic":""}],"outputTopic":"","gateType":"and","emitOnlyIfTrue":true,"x":520,"y":660,"wires":[["7a24d0e5.a889b"]]},{"id":"7a24d0e5.a889b","type":"debug","z":"a9fbaedc.8f9c1","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":700,"y":660,"wires":[]}]