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

Thanks. Yeah I’m not sure I’m clear about it yet, but I can try the script (which I’ve never done before) and see if it does what I think I’m looking for :slight_smile:

I’ll reword the issue just in case, and use my physical example.
I have a couple of door and window RF transmitter and want to fit many more, they communicate to HA via an 433Mhz RF hub. The RF hub only sends an ‘on’ to HA for each entity, and never an ‘off’ (because these RF transmitters have only one code). HA hassio GUI shows the door opened forever unless I manually ‘off’ it in the hassio gui. Then it’s ready to be tripped again, and do automations. I’d like either the RF hub or HA to ‘reset’ the entity back to ‘off’.
I’ve just reflashed my Sonoff RF bridge to Tasmota FW (from Espurna FW) and I’ll see if I can recreate the entities and try the script on them. I also have a DIY RFLink hub which I’m playing with.

I would start your own topic. What you are looking for is a binary sensor to automatically switch back to off. This topic is more about momentary switches.

What your are looking to do can most definitely be done. Tag me in your new topic and we can have a look.

Hi, I’ve started a new topic, but I did not see where to add member tags. So the link is below.

Hi,

I hope it is still relevant - here is my solution for push button using appdaemon app:

import appdaemon.plugins.hass.hassapi as hass

class door_command(hass.Hass):
    
    def initialize (self):
        self.listen_state(self.callback, "input_boolean.push_button")

    def callback (self, entity, attribute, old, new_state, kwargs):
        if new_state == 'off':
                self.turn_on(entity_id = "input_boolean.push_button")
        else:
                # do your stuff

I 100% support this, earlier when I used Domoticz we head something called “Push on/off button” that just sendt on or off commands, it was super useful and something I miss in HA.

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.