Momentary switch without using script

I assume what I am trying to achieve is very common. I have a number of momentary switches for garage and gates. I have created them by using a script which turns it off after 1 second. I am trying to a number of things however where using a script rather than a switch is awkward or requires many lines of yaml (i’m not so confident with Yaml).
Surely there must be a way of achieving a switch with a on/off delay?
Appreciate advice.

Please check out this custom componet.

THanks - Will give that a try.

Where are the momentary switches defined? Mine are through esphome and I can use a template switch like so. It will ring open for the set time. I believe you can also use this in ha configuration.

  - platform: template
    name: "buzzer"
    icon: mdi:door
    - switch.turn_on: relay
    - delay: 1750ms
    - switch.turn_off: relay

So just to be clear in my case I’ve used the below, the entity id is switch.main_gate. If I understand correctly my entity is now converted to a momentry switch turning on for one second?

  - platform: momentary
    name: switch.main_gate
    mode: on
    toggle_for: 1
    cancellable: True

Not sure if I need cancellable?
Thanks for help

That didnt work. My script that works says switch turn on, delay for 1 second then switch turn off. Is the above yaml different?

instead of entity_id, try with the name of the switch in the configuration. Also I hope you have added momentary: to your config other than this configuration. Cancellable wont be of any use to you as the toggle off time is very less.

Sorry what does it mean “where is the switch defined” - Are you saying that if I follow your example dont need to use twrecked/hass-momentary?

I use esphome to create DIY physical switches, it has a separate config file that allows the code I posted. I was wrong to say that it works in HA. The momentary component sheminasalam linked to should work.

Towards the top of configuration.yaml after default_config: you have to add momentary: with nothing else in order to load the custom component. Then define your switch under switch:

Thanks for advice - I think i’m hitting my head against brick wall - because so much going on in my install. Yes I did add the momentary line into yaml. It should work as its added the .momentary into the name and there is a witch alongside that stays on for one sec then goes off. I’ve tried just using the name but nothing. This is an MQTT switch, does that make a difference. Sorry but i’m not too familiar with YAML, etc.

I don’t think that “momentary” will do what you want.

It looks like from the docs that the integration creates a totally new “switch” (more like an input_boolean) that is toggled rather turning an existing regular switch into a momentary switch.

I don’t know of any other way to get the desired result other than using a script/automation as you are already doing.

I think your right. It looks like “Momentary” is doing what its supposed to but doesn’t fit my application use. Seems strange there’s no other way than a script though.

Here is what I use to turn any switch into a momentary switch, for a button in Lovelace,

type: button
  action: toggle
entity: switch.pool_pump

Then I have a single automation that watches for any switches that I want to be momentary to turn on, and switch them off after .5sec, like this, in automation.yaml

  alias: Momentary  switch
  description: 'Turn any switch into a momentary switch'
  - platform: state
    entity_id: switch.pool_pump, switch.rain_delay2
    from: 'off'
    to: 'on'
    for: '00:00:00.5'
  condition: []
  - service: switch.turn_off
    data: {}
    entity_id: switch.pool_pump, switch.rain_delay2
  mode: single

You can add as many as you want, and can do this from the automation UI, don’t need yaml, just add each entity as a csv. Note you will need to add another trigger if you want some switches to have different on time. This works for what I need, YMMV


Appreciate all help above. Thanks for your time in responding
@Kdem that’s really helpful and will resolve one of the issues I have. Appreciated.

1 Like

Maybe make it a little bit easier, a group can be made like: group.momentary
If you have any new switch what you want to make as momentary, you can add to the group. And the group should be automated:

With this, you don’t have to touch the automation anymore, just add the new switch to the group.


Thx for that! It seems that you have to use homeassistant.turn_off instead of group.turn_off refering to the docs. With that adaptation ur example worked like a charm!


I actually tried this method having just thought of it on my own. Tried it and it didn’t work. So took to Googling for a solution since I thought I was wrong, and found you did the exact same thing. And it worked for you. So I tried it again…no luck. I did test by running the automation from the Automation management screen and it works. It just won’t fire from my button. Any thoughts???

My example does not use group.turn_off, someone else modified it…and it appears to quote me…FWIW

In case anyone finds this, this is how i got mine working as of 2023. Groups are no longer labeled as group.group_name, you can label them as an entity type now - so like switch.momentary for the group name.

my automation modified from the one from @FaBRiK

alias: Momentary switches
description: Turn any switch into a momentary switch
  - platform: state
      - switch.momentary
    from: "off"
    to: "on"
      hours: 0
      minutes: 0
      seconds: 0.5
condition: []
  - service: switch.turn_off
    data: {}
      entity_id: switch.momentary
mode: single

Then here’s my group config (2 screenshots)

Group Here:

I can confirm this works and the wife is thoroughly annoyed at the table lamp turning on for a half second and then off. Cheers

1 Like

Good update!
Have to say that the ‘Group’ domain still exists, but it needs set up in yaml not in UI. The downside of the groups created in a Helper (with UI) that it can contain only one domain (e.g. only switch, only light…).
But this topic is about momentary switch so your solution is perfect since the entities are switches.
So Group domain still exists if anyone wants to mix switches, light and other domain in one group.
Also consider to use homeassistant.turn_off service instead or group.turn_off or switch.turn_off