For the love of gawd can we get a momentary switch already?

Here is a way to create the push button effect by modifying the rpi_rf module:

hope it helps.

Thanks! This is perfect for my garage door (Insteon via ISY)
I looked at a few other script examples, but this one is short, simple and works well.

1 Like

Worked for me.
Will this support milliseconds?

Should do…

script:
  button_test:
    alias: Button 
    sequence:
      - service: homeassistant.turn_on
        entity_id: THING
      - delay:
          milliseconds: 400
      - service: homeassistant.turn_off 
        entity_id: THING
1 Like

Worked great! thanks Marc

1 Like

Hi Guys
I tried this and its not woking for me
what am i missing i get the entity_id is unique to my install but
is there any thing else… alias?, button_test? that is unique.
I’ve tried the Momentary Switch Component in hacs as well and that didn’t work either
Thank you

panel_iframe:
  tasmoadmin:
    title: TasmoAdmin
    icon: mdi:lightbulb-on
    url: http://**.**.**.**
    
script:
  button_test:
    alias: Button
    sequence:
      - service: homeassistant.turn_on
        entity_id: switch.shed_mini
      - delay: 00:00:05
      - service: homeassistant.turn_off
        entity_id: switch.shed_mini

Define ‘not working’?

Keep in mind that HA runs on a clock that only triggers on each second. A delay that is finished prior to then next second will not trigger when the delay is up, it will trigger when the next second happens.

Hi Marc
When I turn on the switch in the Lovelace ui it just stays latched and turns off if I click it again.
I’ve added this to my config.yaml file is that the right file

while all these “work around” are lovely I think the elegant solution would be, when defining an input_boolean and or switches, there should be a setting “type” that has 2 option:

  1. switch, which is the default and like this all existing would continue to work
  2. momentary, which would make lots of people happy
4 Likes

Yeah, you don’t turn on the switch in lovelace, you run the script from lovelace.

Reading this, I feel like there are two different use cases being smashed together that shouldn’t be.

  1. I want to add a button to my HA that when pressed does the action I tell it then resets itself, ready to be pressed again. I should be able to drop that button in the UI as well as trigger that button press from automations. Example use case: Making a universal remote for a device where state can’t be effectively tracked. This would cover every button except the power button.
  2. I want to add a switch to my HA that does not attempt to track the state of its remote device at all, not even ‘assumed state’ (most likely because it can’t). Instead when included in UI it should always present with both an on and an off button and I should be able to specify what action happens when turned on or off (after which it resets itself back to its default state of neither on nor off). As with the button, I should be able to trigger on or off on this switch from UI and automations. Example use cases: Any device with a power button where state can’t be effectively tracked.

#1 Is a script. I will admit its not intuitive to go looking for something scalled script when you think you are looking for something called button but #1 exactly describes a script. It might be worth a cookbook document describing this use case and clearly correlating it to script since enough people need it and are confused by it.

#2 doesn’t exist to my knowledge and I agree it would be handy at times. Technically you can make it work by creating two scripts, one for device_on and one for device_off. But this workaround is clunky and it would be difficult to make UI that looked nice with it. It would be nice if there was a helper specifically for this use case that then came with turn_on and turn_off services and the default UI was a button for on and a button for off.

#1 is a script, which is what we’ve been saying all along

#2 is exactly what the switch component already does for integrations where it is unable to establish the state (such as infrared remote controls)

Both platforms have been doing this since homeassistant was in its infancy, so this situation of needing a momentary switch has basically never existed, the only problem is that for some reason people can’t grasp the concept that a script can be a button on the UI no matter how many times it is explained to them.

I could literally now go to the home assistant core repo, copy the code for the script component exactly, change only its name to ‘momentary_button’ and release it on hacs and people would download it and ask for it to be added to core!

2 Likes

Yea I agree about the script thing. That’s why I said it might be beneficial to just include somewhere in doc that if you want a button you should make a script.

For #2, I do get that it is something switches can do but is there a way to make a generic one which does that? Like what I think people are looking for is essentially an option that works like template switch but doesn’t track state. Where they get full control over the on/off options but there’s just no actual state.

But honestly you might be right. Maybe the missing gap here is simply a cookbook. Something that says “in order to make a stateless power switch and/or universal remote here’s what you do.” Then any time the discussion comes up there is something to point to.

I use custom:button-card for my buttons, with templates for styling, but I have to admit every time I want to change something with custom:button-card, it’s not intuitive to me. And I use NodeRED for most of my automations which is amazingly intuitive.

I was unaware that a script could be inserted into Lovelace, although EXECUTE is probably not what most people want for UI.

What would be the best way to code a momentary button that looks like an entity switch?

I’m guessing: create an input_boolean, have it trigger an automation that calls a script.

Reading this thread I’m scratching my head why folks are trying to solve an event-based problem by using a state-based solution. A ‘momentary switch/button’, isn’t that just another way of referring to an event?

We should be looking at UI elements that fire events which trigger automations to take whatever action is desired. I’m not aware of a Lovelace UI element that can fire custom events, but we could use the Entities card with its call-service to trigger an automation.

A sample Lovelace card…

type: entities
entities:
  - type: call-service
    name: Sample 1
    action_name: Activate
    service: automation.trigger
    service_data:
      entity_id: automation.sample1

…would look like
Annotation 2020-07-07 163206

…with an automation sample:

- id: '1594153495190'
  alias: Sample1
  description: ''
  trigger: []
  condition: []
  action:
  - data: {}
    entity_id: light.front_porch
    service: light.toggle

Because UI matters. I was specifically looking for something that looks like a button rather than a word.

1 Like

Like this?

type: button
entity: automation.sample1
tap_action:
  action: call-service
  service: automation.trigger
  service_data:
    entity_id: automation.sample1
icon: 'mdi:power'
show_name: false

Annotation 2020-07-07 200001

2 Likes

Awesome. Thanks.

Ok thank you Marc great help

1 Like