Toggle Switch for x milliseconds

I’ve got a relay set to be controlled via MQTT. It works great!

I’m not trying to setup a button in HA that when clicked, sets the topic to ‘on’ for 200ms and then sends the topic ‘off’. This is to trigger my garage door. If milliseconds is not supported I can probably work with 1 second too.

I have full access to code on the node itself and can totally get it to automatically turn itself off as well, but I’d like HA to do to heavy lifting. Right now all my nodes share the same code so managing them is much easier.


P.S. I’m pretty new to HA so… :smiley:

Hi @kamilon, you can use your switch state for an automation trigger.

- alias: Turn off switch test
    platform: state
    entity_id: switch.test_switch
    to: 'on'
      seconds: 1
    service: switch.turn_off
    entity_id: switch.test_switch

Don`t know if there’s milliseconds for this.

The docs for scripts

describe a “delay” parameter
- delay:
      # supports seconds, milliseconds, minutes, hours
      minutes: 1

Maybe this works also in automations.

Edit 2:
Works with milliseconds to. :slight_smile:

This is working for turning it off automatically, and it even works with milliseconds like you said. However, one big issue is that every time I restart Home Assistant it opens my garage door. How do I make that stop?

The garage node has a state of “Off” when Home Assistant starts but it still triggers the automation.

I know this is an old thread but for the benefit of anyone seaching and finding this thread (I did) the solution to the post above (and anything similar) is to create an input_boolean with initial: ‘on’, put a condition in your garage automation (or where ever you are experiencing this issue and have it check that your boolean is ‘off’ hence your garage automation will never work.
Then you create another auotmation triggered by : -
- platform: homeassistant
event: start
then in it’s actions, put a delay say 1 min (how often do you want to open/close your garage within 1 min of restarting HA ?)
Then, have it set your boolean to ‘off’
Robert is your avuncular relative

Oh ! … And by the way; milliseconds don’t work, at least not as you’d find useful, trigger states and actions are only updated on a 1 second schedule ‘MAX’ (thank for that handy tip)