Loop script not looking

I have a sutomation that calls this script:

alexa_alarm:
  sequence:
    - condition: state
      entity_id: alarm_control_panel.alarm
      state: 'triggered'
    - service: media_player.alexa_tts
      entity_id: media_player.dot_2
      data:
        message: "Alarm  activated"
    - service: script.alarm_loop
alarm_loop:
  sequence:
    - condition: state
      entity_id: alarm_control_panel.alarm
      state: 'triggered'
    - delay:
        seconds: 5
    - service: script.alexa_alarm

I thought it would loop as long as “alarm_control_panel.alarm” state is “triggered”?

It loops twice.

It looks like it should work. What do you see in home-assistant.log? The details of what’s happening should be in there. E.g., when a script gets to a condition step, you should see something like this in the log:

2019-02-15 15:59:57 INFO (MainThread) [homeassistant.helpers.script] Script Blah, blah: Running script
2019-02-15 15:59:57 INFO (MainThread) [homeassistant.helpers.script] Script Blah, blah: Executing step call service
2019-02-15 15:59:57 INFO (MainThread) [homeassistant.helpers.script] Script Blah, blah: Test condition state: False
2019-02-16 20:00:58 INFO (MainThread) [homeassistant.helpers.script] Script alexa_alarm: Running script
2019-02-16 20:00:58 INFO (MainThread) [homeassistant.helpers.script] Script alexa_alarm: Test condition state: True
2019-02-16 20:00:58 INFO (MainThread) [homeassistant.helpers.script] Script alexa_alarm: Executing step call service
2019-02-16 20:00:58 INFO (MainThread) [homeassistant.helpers.script] Script alexa_alarm: Executing step call service
2019-02-16 20:00:58 INFO (MainThread) [homeassistant.helpers.script] Script alarm_loop: Running script
2019-02-16 20:00:58 INFO (MainThread) [homeassistant.helpers.script] Script alarm_loop: Test condition state: True
2019-02-16 20:00:58 INFO (MainThread) [homeassistant.helpers.script] Script alarm_loop: Executing step delay 0:00:05
2019-02-16 20:01:04 INFO (MainThread) [homeassistant.helpers.script] Script alarm_loop: Executing step call service
2019-02-16 20:01:04 INFO (MainThread) [homeassistant.helpers.script] Script alexa_alarm: Running script
2019-02-16 20:01:04 INFO (MainThread) [homeassistant.helpers.script] Script alexa_alarm: Test condition state: True
2019-02-16 20:01:04 INFO (MainThread) [homeassistant.helpers.script] Script alexa_alarm: Executing step call service
2019-02-16 20:01:04 INFO (MainThread) [homeassistant.helpers.script] Script alexa_alarm: Executing step call service
2019-02-16 20:01:04 WARNING (MainThread) [homeassistant.components.script] Script script.alarm_loop already running.

looks like there’s a message that says its(loop) already running…can i stop one beore going to the next?

The problem is when a script is finished, it actually takes a small amount of time for its state to change from on to off. Unfortunately, during that period if it is attempted to be started again, it will fail. The way your two scripts are currently written, script.alexa_alarm has time for its state to change to off before script.alarm_loop attempts to call it again. But the same is not true the other way around. I.e., when script.alarm_loop starts script.alexa_alarm, script.alexa_alarm will get to the end and try to start script.alarm_loop before its state changes to off.

Anyway, the way to fix it is to add a wait_template to the first script:

alexa_alarm:
  sequence:
    - condition: state
      entity_id: alarm_control_panel.alarm
      state: 'triggered'
    - service: media_player.alexa_tts
      entity_id: media_player.dot_2
      data:
        message: "Alarm  activated"
    - wait_template: "{{ is_state('script.alarm_loop', 'off') }}"
    - service: script.alarm_loop
3 Likes