Script only runs once

Hi there,
I’m trying to get my head around scripting and am stumped by an issue.

I have a script that I want to notify when a certain thing changes state.
I have this working for the most part. In so much as when the thing changes state, from either on to off or vice versa, it will trigger the script, notifications go out and are received etc, so all that is working.

However, on a subsequent event, nothing happens. The script seems to stop working. I’ve tried various modes like restart, queued, parralel etc, but all the same outcome.

If I happen to be in the Scripts screen on that script, i even see a banner appear saying “triggered” when the event happens, yet nothing is actioned.

The script is below:

pool_pump_listener:
  alias: Pool Pump Listener
  sequence:
  - wait_for_trigger:
    - platform: event
      event_type: state_changed
      event_data:
        entity_id: light.fastled_ws2811_light
  - service: notify.smtp_notifier
    data:
      title: 'Notifier: Pool Pump'
      message: Pool Pump has switched to {{ states.light.fastled_ws2811_light.state
        }}
  - service: notify.mobile_app_pixel_4_xl
    data:
      title: Notifier Pool Pump
      message: Pool Pump has switched to {{ states.light.fastled_ws2811_light.state
        }}
  mode: queued
  icon: mdi:alarm-light

note: I’m just using the state of an LED for now to save turning a pool pump on/off constantly whilst testing this, just in case you are wondering what this is for - eventually I’ll use the switch.pool_pump entity that is actually what I’ll be watching.

After the script does it’s initial successful run, the Trace Timeline shows it as “Wait for Event Trigger”, however the time stamp on Step Details never updates again after subsequent triggers.

If I manually re-start the script it works again - once.

Here is the Step Details of a successful run:

Executed: July 27, 2022 at 2:07:52 PM
Result:

wait:
  remaining: null
  trigger:
    id: '0'
    idx: '0'
    platform: event
    event:
      event_type: state_changed
      data:
        entity_id: light.fastled_ws2811_light
        old_state:
          entity_id: light.fastled_ws2811_light
          state: 'off'
          attributes:
            supported_color_modes:
              - rgb
            friendly_name: FastLED WS2811 Light
            supported_features: 40
          last_changed: '2022-07-27T04:06:35.767526+00:00'
          last_updated: '2022-07-27T04:06:35.767526+00:00'
          context:
            id: 01G8YXBGXTAP28F81D9B4TWSP6
            parent_id: null
            user_id: 946f3721c4e74ac5bbf3260a49b8db0e
        new_state:
          entity_id: light.fastled_ws2811_light
          state: 'on'
          attributes:
            supported_color_modes:
              - rgb
            color_mode: rgb
            brightness: 255
            hs_color:
              - 120
              - 100
            rgb_color:
              - 0
              - 255
              - 0
            xy_color:
              - 0.172
              - 0.747
            friendly_name: FastLED WS2811 Light
            supported_features: 40
          last_changed: '2022-07-27T04:07:55.239644+00:00'
          last_updated: '2022-07-27T04:07:55.239644+00:00'
          context:
            id: 01G8YXDYHKKERYQ2WX9PT16AN7
            parent_id: null
            user_id: 946f3721c4e74ac5bbf3260a49b8db0e
      origin: LOCAL
      time_fired: '2022-07-27T04:07:55.239644+00:00'
      context:
        id: 01G8YXDYHKKERYQ2WX9PT16AN7
        parent_id: null
        user_id: 946f3721c4e74ac5bbf3260a49b8db0e
    description: event 'state_changed'

Am I doing something wrong?

If you want something to trigger an action use an automation. Not a script.

OK, happy to do that, but just for learning purposes - should the script not just keep running though?

No. You call the script and it runs through the sequence of actions until your wait for trigger. When that occurs it moves on to the next action, reaches the end of the sequence and finishes.

There are options to repeat a loop while or until some state occurs but this would not be as efficient as triggering an automation when your event occurs then performing your actions.

ahh, OK. makes sense now.

I’ve now got it set up exactly the same, but in an Automation, and yeah, it’s working 100% now.

Thanks! I’m only just starting out, so this is all valuable!

Cheers

1 Like