Problem with automation and script (Door sensor as a keychain)

Hi community!
I’m going nuts with this. What I’m trying:

  1. When the door sensor 1 is on state “open” and the door sensor 2 is “closed” run the script:

     - alias: Antilost keychain
       trigger:
         platform: state
         entity_id: binary_sensor.door_window_sensor_158d000183fa7a
         from: 'closed'
         to: 'open'
       condition:
         - condition: state
           entity_id: binary_sensor.door_window_sensor_158d00016c40e5
           state: closed
       action:
         - service: script.turn_on
           entity_id: script.flash_hallway
    

Looks like there’s somehting wrong. It’s not working.

  1. This automation calls a script at the end, which makes the switch turn on and off every 2 seconds, but I don’t know how to stop the script when the door sensor 2 is “open”.

     #Starts flashing
     flash_hallway:
       alias: Anti-lost keychain flash
       sequence:
          - alias: Hallway light on
            service: switch.turn_on
           data:
              entity_id: switch.wall_switch_left_158d0001a41acd
          - delay:
              seconds: 1
          - alias: Hallway light off
            service: switch.turn_off
            data:
              entity_id: switch.wall_switch_left_158d0001a41acd
          - alias: flash_looping
            service: script.turn_on
            data:
              entity_id: script.flash_loop
    
     #Starts looping
     flash_loop:
       alias: Anti-lost keychain loop
       sequence:
         - delay:
             seconds: 1
         - service: script.turn_on
           data:
             entity_id: script.flash_hallway
    

    Thank you!

Looks like your 1. is because you are using condition of “closed” not “open” and it’s not in single quotes.

For 2. I would do another automation to turn off the script when door 2 is open. Hope that helps :slight_smile:

1 Like

Thanks. However, my syntax was right, I just made a typo: I wrote “open” instead of "closed. It’s now corrected.

And it all works now! I won’t forget my keys anymore. This is the final code for automation and scripts, just in case anyone wants to do something similar:

Automations:

- alias: Antilost keychain ON
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d000183fa7a
    from: 'off'
    to: 'on'
  condition:
    - condition: state
      entity_id: binary_sensor.door_window_sensor_158d00016c40e5
      state: 'off'
  action:
    - service: script.turn_on
      entity_id: script.flash_hallway

- alias: Antilost keychain OFF
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d00016c40e5
    to: 'on'
  action:
    - service: script.turn_on
      entity_id: script.flash_hallway_cancel

Scripts:

#Starts flashing
flash_hallway:
  alias: Anti-lost keychain flash
  sequence:
    - alias: Hallway light on
      service: switch.turn_on
      data:
        entity_id: switch.wall_switch_left_158d0001a41acd
    - delay:
        seconds: 1
    - alias: Hallway light off
      service: switch.turn_off
      data:
        entity_id: switch.wall_switch_left_158d0001a41acd
    - alias: flash_looping
      service: script.turn_on
      data:
        entity_id: script.flash_loop

#Starts looping
flash_loop:
  alias: Anti-lost keychain loop
  sequence:
    - delay:
        seconds: 1
    - service: script.turn_on
      data:
        entity_id: script.flash_hallway

#Cancel scripts

flash_hallway_cancel:
  alias: Anti-lost keychain cancel
  sequence:
    - service: script.turn_off
      data:
        entity_id: script.flash_hallway
    - service: script.turn_off
      data:
        entity_id: script.flash_loop

And the final and most important part… How I’m going to use the Xiaomi door sensor as a keychain: