I’ am trying to build a small voting system with two buttons attached to two GPIO pins.
What I want to achieve is someone to be able to vote once and then be able to vote again after some time (e.g. 5 mins) . This is because I count the votes at HA.
I can easily do it with HA but I was trying to do it with ESP8266 too.
So I was thinking instead of exposing the GPIO pins directly, create a template sensor just like I do with home assistant .
This sensor will publish an ON state if the button is pressed and then publish nothing within 5 minutes.
If the button is pressed again after 5 minutes then publish again an ON State. (etc etc)
Can someone please help me with the lamba functions as I am not very familiar ???
thank you very much for your reply and your time to write this sketch.
Would you please adjust your code at my sensors as it’s really hard for me to figure out the adjustments myself ?
First of all, it does work.
But not as expected. It gives constantly switch on/off
[00:21:39][D][binary_sensor:036]: 'happy': Sending state ON
[00:21:39][D][switch:037]: 'Happy Switch': Sending state ON
[00:21:40][D][binary_sensor:036]: 'happy': Sending state OFF
[00:21:40][D][switch:037]: 'Happy Switch': Sending state OFF
[00:21:41][D][binary_sensor:036]: 'happy': Sending state ON
[00:21:41][D][switch:037]: 'Happy Switch': Sending state ON
[00:21:41][D][binary_sensor:036]: 'happy': Sending state OFF
[00:21:41][D][switch:037]: 'Happy Switch': Sending state OFF
This is not the expected behavior …
At 00:21:39 when the binary sensor is on, then the switch is on too, which is correct.
At 00:21:40 when the binary sensor is off, then the switch is off too, which is correct.
At 00:21:41 when the binary sensor is on again, then the switch should stay off for 20s but it doesn’t , which is wrong.
When an ON is received for 2nd time, wait for the specified time period(20s) until publishing an ON state. If an OFF value is received while waiting, the ON action should be discarded.