How to make a push button

How can I define a push button, a momentary switch that gets back to off state after X seconds?

I have an Arduino integration that works and looks like this (in configuration.yaml):

switch:
  - platform: arduino
    pins:
      2:
        name: auto_gate

The only issue I have is that I’d need this switch to get back to off state after 2-3 seconds.

I found some posts related to this and I’ve tried adding a momentary attribute to PIN 2, but it’s not a recognized attribute.

I am a noob. Can you please explain step by step what and where to add.

I’m using hassio, default UI and I’m editing files through web File editor add-on.

Thank you

So, I assume that this can only be achieved through actions, scripts or some plugin.

Would this feature make sense to be added to the Home Assistant core?

Essentially, your arduino code needs to to that.

Arduino only has FirmataStandard, as advised in the integration docs.

From my understanding this is doable via automations, right?

I might look at the core code, this is why I am asking if it makes sense to have push buttons in the core with a configurable delay of X seconds before reverting back to off state.

Below is what I use for a momentary push button:

 - id: '1551029125083'
   alias: South Shop Door
   trigger:                                            
     - event_data: {}                                      
       event_type: ''
       platform: event                                  
   condition: []                                        
   action:
   - data:                                                 
        entity_id: switch.ch1                             
    service: switch.turn_on                           
   - delay:
        milliseconds: 500                               
   - data:
       entity_id: switch.ch1
    service: switch.turn_off

Not sure in which file and under which yaml node to put that.

All I have now as added config is:

switch:
  - platform: arduino
    pins:
      2:
        name: auto_gate

in configuration.yaml.

Which file to edit in the web File editor?

Thanks

I think your best bet would to be bring your push button into home assistant as a binary_sensor. Then you can use that as a trigger for an automation to turn on an input_boolean for however long you require and then turn it off again.

1 Like

You can either put in automations.yaml or create an automation through the web editor.

1 Like

Take a look here: Simple push button in dashboard?

Seems possible to add a momentary parameter to a switch.
Not tested by me, only read fast :slight_smile:

I needed a UI button that I could use to trigger a ESP32-Cam through MQTT to take a photo. Below are the steps I followed to make a momentary switch/button, and below that the related automation in yaml to make it work.

In summary, I created a input_boolean Helper, made it visible on the dashboard as a Button, and used an Automation to change the state back to off after a short delay.

  1. Create a input_boolean in Configuration -> Helpers. (To allow you to follow the example below, mine is called “Take Gate Photo”, and the entity_id then translates to “input_boolean.takegatephoto”).
  2. In Lovelace, edit the dashboard (three vertical dots top right), add an appropriate card to your liking, and set the entity to the newly created input_boolean. (I added a button, with Tap Action set to Toggle (default) ).
  3. Add something similar to the example below to your automations.yaml, customized based on your situation.
  4. In Configuration -> Server Controls, check the configuration to make sure it is valid.
  5. Do Reload Automations to implement the changes, and test to see if it works.
# Automatically reset button state back to "off" after short timeout
  - alias: Take Gate Photo
    trigger:
        platform: state
        entity_id: input_boolean.takegatephoto
        to: "on"
    condition: []
    action:
      - delay: 0:0.9
      - service: input_boolean.turn_off
        entity_id: input_boolean.takegatephoto
2 Likes

The way I’ve done a pushbutton is to use a cover with MQTT and a button card. Don’t specify any device class, specify the open and close payload the same. On the Lovelace button card specify the entity ID of the cover (e.g. cover.garage_door1_light), a name (Garage Light) and whatever icon you like (hass:lightbulb), set the action to toggle. In this example I don’t have a state sent back so I leave the Show State toggle off so it doesn’t always show “unknown”. My hardware is custom and responds to the MQTT topic (garage/door1/control) and command (toggle), but you can also use Node Red to subscribe to the topic to have it act on the press based on the topic included in a payload.

cover:
  - platform: mqtt
    name: "GarageLight1"
    command_topic: "garage/door1/control"
    payload_open: "toggle"
    payload_close: "toggle"
    retain: false
    value_template: "{{ value }}"
    qos: 0
    unique_id: "cover.garage_door1_light"

how to make to button card that always holds the button pushed for xy milisecond?
Reason is i want drive an alarm panel that needs 2 second long push signal. (no less than 2sec and no point for longer push)