Toggle Switch Trigger Automation

Hi,
I am trying for a small automation to make my garage door switch to toggle. Automation should turn the switch OFF after 200ms if someone turn it ON. So I did this but it is not working. What I am doing wrong?

trigger:
platform: state
entiry_id: switch.garage_door
from: “OFF”
to: “ON”
for:
milliseconds: 200

action:
service: switch.turn_off
entity_id: switch.garage_door

you need to format your code


trigger:
platform: state
entiry_id: switch.garage_door
from: “OFF”
to: “ON”
for:
milliseconds: 200

action:
service: switch.turn_off
entity_id: switch.garage_door

1st look
you missing the some indenting

trigger:
  - platform: state
    entiry_id: switch.garage_door
    from: "OFF"
    to: "ON"
    for:
      milliseconds: 200

action:
  -  service: switch.turn_off
     entity_id: switch.garage_door

2rd those Quotes are on a mac

3rd look like you missing the spaces after the for: as the milliseconds arepart of the for:

looking again
4th qualify - its what i call it

You want 'on' and 'off' in lower case, and entity_id is mis-spelled in your trigger code. Here’s how I’d format it, with alias, description and id. The id (just a random string: I use UUID4) allows the automation tracer / debugger to work.

- alias: Garage door momentary on
  description: Turns the garage door switch off as soon as it is turned on
  id: 7a66b9b0-0e44-4687-88b2-8b6e02c7edbd

  trigger:
    - platform: state
      entity_id: switch.garage_door
      from: 'off'
      to: 'on'
      for:
        milliseconds: 200

  action:
    - service: switch.turn_off
      entity_id: switch.garage_door

If the 200ms is critical, you may be disappointed: HA isn’t necessarily that “punctual”.

But do please format your code using the </> button. See point 11 here:

1 Like

I would suggest rather using a script with a delay between turning on and off. Also, what you’re describing is called a momentary button.

That’s a good idea if it’s HA that is also turning the switch on. If it’s a physical switch, the automation will be needed.

You’re right but it seems risky to have a button that needs to be controlled from more than one place to which HA will have to reach so quickly.

I was kind of assuming based on this that the existing physical button is already a momentary button (like a remote) and that one wouldn’t use that and e.g. a button in HA at the same time.

Thanks for your help. Issue is fixed after i added the missing - before platform and service.

Thanks for your help! 200ms don’t need to be accurate for my case. I just need automation to turn it OFF after few seconds. It is working great with your script.

1 Like

I have a physical push button. But it doesn’t look like the button and HA are coming each others way.
Script is working great now after I modified it as suggested by myle & Troon above.

1 Like

I Edit Over But Can’t Save This Automation.

alias: 當螢幕開啟-開啟書房燈
description: Turns the garage door switch off as soon as it is turned on
id: 7a66b9b0-0e44-4687-88b2-8b6e02c7edbd
trigger:
  - platform: state
    entity_id: switch.cuco_wp5_4551_switch
    from: 'off'
    to: 'on'
    for:
      milliseconds: 200

action:
  - service: switch.turn_on
    entity_id: switch.3f_shu_fang_deng

Output:

And Not Saved:

How to Fix it ?
Thanks!

If you are using the UI and can use 1s instead of 200ms, use the UI building blocks instead of YAML.

If you must paste or write YAML into the UI, leave out the id: line; and use for: "00:00:01".

Modify to theese.It can work.
But I turn on Mijia Switch.
The Switch Status From Off Change to on,
Trigged about 10s~30s,It;s too slow.
I think this maybe hass-xiaomi-miot Package Issue.
But Thanks Anyway!

alias: 當螢幕開啟-開啟書房燈
description: Turns the garage door switch off as soon as it is turned on
triggers:
  - entity_id:
      - switch.cuco_wp5_4551_switch
    from: "off"
    to: "on"
    trigger: state
    for:
      hours: 0
      minutes: 0
      seconds: 1
actions:
  - entity_id: switch.3f_shu_fang_deng
    action: switch.turn_on