Debugging a flashing light configuration - help?

Hello all! Long time lurker, first time poster.

I’m working on an automation for my mother-in-law, basically setting up some panic buttons to flash a number of lights in case she’s in trouble. For completeness: My system is Insteon with an ISY acting as the gateway to HA, and I’ve got three RemoteLinc 2 mini-remotes set up into a group for the panic switch. I’m running hass.io on an RPi 3 with HA 0.72.1, supervisor v110 and ResinOS 2.3.0+rev1.

I’ve followed the example for flashing lights on the HA page, specifically creating a pair of scripts that turn on each other to create the loop to blink. The problem I’m running into is turning off this pair of scripts.

All of these examples have the following common groups:

panic_switches:
  entities:
    - light.panic_button_1
    - light.panic_button_2
    - light.panic_button_3

panic_lights:
  entities:
    - light.upper_master_hallway_switch

I started with the example here, but stripped down the system to the following automations and scripts:

automation:
- alias: 'Panic_Start'
  trigger:
    platform: state
    entity_id: group.panic_switches
    to: 'on'
  action:
    # flash lights if panic button is pressed
    service: script.turn_on
    entity_id: script.panic_flash

- alias: 'Panic_Stop'
  trigger:
    platform: state
    entity_id: group.panic_switches
    to: 'off'
  action:
    - service: script.turn_off
      data:
        entity_id: script.panic_flash
    - service: homeassistant.turn_off
      data:
        entity_id: group.panic_lights

script:
  panic_flash:
    alias: 'Flash Lights for Panic'
    sequence:
      - alias: 'Panic Lights on'
        service: homeassistant.turn_on
        data:
          entity_id: group.panic_lights
      - delay:
          # time for flash light on
          seconds: 0.5
      - alias: 'Panic Lights Off'
        service: homeassistant.turn_off
        data:
          entity_id: group.panic_lights
      - alias: 'Trigger Flash Loop'
        service: script.turn_on
        data:
          entity_id: script.flash_loop

  flash_loop:
    alias: 'Flash loop'
    sequence:
      - delay:
        # time for flash light off
        seconds: 0.5
      - alias: 'Panic Loop Return'
        service: script.turn_on
        data:
          entity_id: script.panic_flash

The Panic_Start automation triggers fine, and the light starts flashing, but when I trigger Panic_Stop it would not shut down the script loop. My assumption was that it only tried to turn off the panic_flash script, so I amended the Panic_Stop automation to be this:

- alias: 'Panic_Stop'
  trigger:
    platform: state
    entity_id: group.panic_switches
    to: 'off'
  action:
    - service: script.turn_off
      data:
        entity_id:
          - script.panic_flash
          - script.flash_loop
    - service: homeassistant.turn_off
      data:
        entity_id: group.panic_lights

This is where things get strange (to me). When the Panic_Stop automation is triggered from turning the remote switch off, the scripts will still be executing. However, when I trigger Panic_Stop from within the HA GUI, the scripts will stop.

At this point, my mind started going down the rabbit hole of possible synchronization delays between the ISY and the way HA queues up events and scripts, but I tend to over-complicate the solution of problems like this, and hope there’s an easier way to reliably turn off both flash scripts from the automation. What am I doing wrong?

A couple things to try…

First, it could be a race condition. Try turning off both scripts twice: flash, loop, flash, loop.

Or a more reliable way may be to use an input_boolean. Turn on the input_boolean then start the scripts as you did. Then instead of trying to turn off the scripts, turn off the input_boolean. Then at the beginning of each script add a condition that only allows the script to run if the input_boolean is on. I suppose you could also turn off the scripts after turning off the input_boolean if you want them to stop immediately.

1 Like

The input boolean did the trick. I tried shutting off the scripts twice from within the automation and it didn’t clear them. For future reference, here’s the full snippet to take care of it:

group:
  # Group for all panic function triggers
  panic_switches:
    entities:
      - light.panic_button_1
      - light.panic_button_2
      - light.panic_button_3

  # Group for all actions to flash while in panic
  panic_lights:
    entities:
      - light.upper_master_hallway_switch
      - light.living_room_fan_library
      - light.living_room_fan_switch
      - switch.dining_chandelier
      - switch.kitchen_pendants
      - switch.library
      - switch.main_hallway

input_boolean:
  # Used to safely shut down flash loop
  panic_running:
    name: 'Boolean for Panic Lights'
    initial: off
    icon: mdi:alarm-light

automation:

  # Start flashing panic lights
  alias: 'Panic_Start'
  trigger:
    platform: state
    entity_id: group.panic_switches
    to: 'on'
  action:
    - service: input_boolean.turn_on
      entity_id: input_boolean.panic_running
    - service: script.turn_on
      entity_id: script.panic_flash

  # Stop flashing panic lights
  alias: 'Panic_Stop'
  trigger:
    platform: state
    entity_id: group.panic_switches
    to: 'off'
  action:
    - service: input_boolean.turn_off
      entity_id: input_boolean.panic_running

script:
  panic_flash:
    alias: 'Flash Lights for Panic'
    sequence:
      - condition: state
        entity_id: input_boolean.panic_running
        state: 'on'
      - alias: 'Panic Lights on'
        service: homeassistant.turn_on
        data:
          entity_id: group.panic_lights
      - delay:
          seconds: 0.5
      - alias: 'Panic Lights Off'
        service: homeassistant.turn_off
        data:
          entity_id: group.panic_lights
      - alias: 'Trigger Flash Loop'
        service: script.turn_on
        data:
          entity_id: script.flash_loop

  flash_loop:
    alias: 'Flash loop'
    sequence:
      - delay:
          seconds: 0.5
      - alias: 'Panic Loop Return'
        service: script.turn_on
        data:
          entity_id: script.panic_flash
4 Likes