Conditionally disabling a switch or script

I have a zigbee switch that I use with a script as a garage door remote. The script turns the switch on for 400ms then off simulating the button push.

I also have a Zwave door lock and I am trying to figure out how to disable the switch when the door is locked and re-enable when the door is unlocked without triggering the open command

The below “Locked” automation does not prevent the script from running if locked and the “Unlock” automation fires the script to open the door

Maybe I need to disable the script when locked and re-enable when unlocked or add a condition to the script so it only fires if the door is unlocked?

I’m not sure if these would be as secure as disabling the switch and I DEFINATLY not sure what that automation might look like

#Script to simulate button press
alias: Garage Remote
sequence:
  - service: homeassistant.turn_on
    entity_id: switch.garage_remote
  - delay:
      milliseconds: 400
  - service: homeassistant.turn_off
    entity_id: switch.garage_remote
mode: single

alias: Garage Night Locked
description: ''
trigger:
  - platform: state
    entity_id: lock.key_free_push_button_deadbolt_current_lock_mode
    to: locked
condition: []
action:
  - type: turn_off
    device_id: f53020810e4ab48ce4d4f8e335683d23
    entity_id: switch.garage_remote
    domain: switch
mode: single

alias: Garage Night Unlocked
description: ''
trigger:
  - platform: state
    entity_id: lock.key_free_push_button_deadbolt_current_lock_mode
    to: unlocked
condition: []
action:
  - type: turn_on
    device_id: f53020810e4ab48ce4d4f8e335683d23
    entity_id: switch.garage_remote
    domain: switch
mode: single

You can’t “disable” a script - if you call it, it’s going to run - but you can add a condition that stops execution if the condition evaluates to “false”.

https://www.home-assistant.io/docs/scripts/#test-a-condition

In your case, I believe that would simply be a state condition that only proceeds if the lock is unlocked:

 - condition: state
   entity_id: lock.key_free_push_button_deadbolt_current_lock_mode
   state: "unlocked"
1 Like

Thanks Rob,
That worked!
I needed to move the condition to the top of the sequence but works great

Thanks for the help

alias: Garage Remote
sequence:
  - condition: state
    entity_id: lock.key_free_push_button_deadbolt_current_lock_mode
    state: unlocked
  - service: homeassistant.turn_on
    entity_id: switch.garage_remote
  - delay:
      milliseconds: 400
  - service: homeassistant.turn_off
    entity_id: switch.garage_remote
mode: single