Voting Project

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 ???

Hi solomos

you need somthing to delay

perhaps the while like in this example helps,

- platform: template
    name: "$human_devicename "
    optimistic: true
    id: laser_heat
  #  internal: true
    turn_on_action:
    - while:
        condition:
           lambda: 'return true;' 
        then:
        - light.turn_on: <-your output
            id: laserled_light
            transition_length: 3s
        - delay: 100ms <- your delay for putton press
        - light.turn_off: 
            id: laserled_light
            transition_length: 1ms
        - delay: 5min <- your delay wait for next 
    turn_off_action:
      then:
      - light.turn_off:
          id: laserled_light
          transition_length: 100ms

do you read this?

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 ?

binary_sensor:
  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
      inverted: True
    name: "happy"
    
  - platform: gpio
    pin:
      number: D2
      mode: INPUT_PULLUP
      inverted: True
    name: "ok"

    
  - platform: gpio
    pin:
      number: D5
      mode: INPUT_PULLUP
      inverted: True
    name: "not happy"

    
  - platform: gpio
    pin:
      number: D6
      mode: INPUT_PULLUP
      inverted: True
    name: "sad"

it’s not realy code, just brainstorm.

binary_sensor:
  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
      inverted: True
    name: "happy"
    id: id_happy
    internal: true

# Example configuration entry
switch:
  - platform: template
    name: "Happy Switch"
    lambda: |-
      if (id(id_happy).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - while:
        condition:
           lambda: 'return true;' 
        then:
        - logger.log: "Still happy :)"
         - switch.turn_on: templated-switch for HA
         - delay: 5s  <- your delay wait for next
    turn_off_action:
      - switch.turn_off:  templated-switch for HA
      - delay: 20s <- your delay wait for next

not tested

Tried the following :

switch:
  - platform: gpio
    pin: D7
    name: "vote accepted"
    id: vote_accepted

  - platform: template
    name: "Happy Switch"
    lambda: |-
      if (id(id_happy).state) {
        return true;
      } else {
        return false;
      }
    turn_on_action:
      - while:
          condition:
             lambda: return true;
          then:
            - logger.log: "Still happy :)"
            - switch.turn_on: vote_accepted
            - delay: 5s
    turn_off_action:
      - switch.turn_off: vote_accepted
      - delay: 20s


binary_sensor:
  - platform: gpio
    pin:
      number: D1
      mode: INPUT_PULLUP
      inverted: True
    name: "happy"
    id: id_happy
    internal: true


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.