Make PULSE button to ON GPIO23 and automatic OFF after 1sec. Check if already pushed

hi all,
I hope someone can help me and that is it possible to do what I want:

I need to make a PULSE button that:

  1. CHECK if the button has been already pushed

  2. IF NOT, activate Raspberry GPIO23 just for 2 seconds, then TURN OFF GPIO.

  3. IF YES; DO NOTHING! and say “Device already ON”

  4. Make a .TXT file, or other way to make a “memory” that the button has been pushed once.

  5. Make ANOTHER button to RESET the memory, delete the file or other way!

Please if you are so kindle to interests to my problem please give me the complete code I have to test…

Many thanks in advance.

First you need to setup HA to know which pin to use.

Then you need an automation to activate the pin, a condition to check if it has already been pushed, some templates and a counter to know how many times it has been pushed and also reset it.

Here is the component that you will need:

And you will need a boolean too:

And this is for the counter:

Ok, lets start (hopefully it will be correct):

Configuration.yaml:

input_boolean:
  button_is_pressed:
    initial: off

binary_switch:
  - platform: rpi_gpio
    ports:
      23: Button

counter:
  button:
    step: 1

Now you have your button and your counter setup into home assistant. Restart HA.

Now you need an automation. You said you want to check if the button has been pushed. Do you mean pushed during the day? It will help if you can be more specific with what you want to achieve.
Anyway, try this and tell me

Now create an automation to turn on the boolean when the button is pressed:

automations.yaml:

- id: 'somethingrandom' //change this
  alias: 'button boolean turn on'
  trigger:
  - platform: state
    entity_id: binary_switch.button
    to: 'on'
  condition: []
  action:
    service: input_boolean.turn_on
    entity_id: input_boolean.button_is_pressed

And one more to turn off the boolean at midnight everyday.

- id: 'somethingrandom' //change this
  alias: 'button boolean turn off'
  trigger:
  - platform: time
    at: '00:00:00'
  condition: []
  action:
    service: input_boolean.turn_off
    entity_id: input_boolean.button_is_pressed

Now this automation will check if the boolean is off and will turn on the button for 2secs and then will turn off:

- id: 'somethingrandom' //change this
  alias: 'button boolean turn on'
  trigger:
  - platform: state
    entity_id: binary_sensor.button
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.button
    state: 'off'
  action:
  - service: counter.increment // this will tell you how many times the button has been pressed
    entity_id: counter.button
  - service: switch.turn_on
      entity_id: switch.button
  - delay: '00:00:02'
  - service: switch.turn_off
		entity_id: switch.button

This automation will check if the boolean is off, and will tell you “the device is already on”:

- id: 'somethingrandom' //change this
  alias: 'button boolean turn on'
  trigger:
  - platform: state
    entity_id: binary_sensor.button
    to: 'on'
  condition:
  - condition: state
    entity_id: input_boolean.button
    state: 'on'
  action:
  - service: counter.increment // this will tell you how many times the button has been pressed
    entity_id: counter.button
  - service: I cant complete this because you didnt say HOW you want to be notified.

Now, go to Home assistant side panel> configuration> General> reload automations

Pheeww That was a lot to do. I hope I didnt forget anything. I am not at home and cant check any of the above. You just have to try and tell me if any errors come up, or if something is not working.

1 Like