Automation condition check if another script is running

Hi,

is it possible to run an automation only if another script is running?

For example this automation

alias: Schlafmodus abbrechen wenn bewegung in anderen Räumen
hide_entity: false
trigger:
  - platform: state
    entity_id: binary_sensor.living_multi_sensor_sensor_20_0
    state: 'on'
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor_4_0
    state: 'on'
  - platform: state
    entity_id: binary_sensor.floor_multi_sensor_sensor_17_0
    state: 'on'
  - platform: state
    entity_id: binary_sensor.bathroom_multi_sensor_sensor_19_0
    state: 'on'
  - platform: state
    entity_id: binary_sensor.bedroom_multi_sensor_sensor_18_0
    state: 'on'
condition:
  condition: state
  entity_id: input_boolean.sleepmode
  state: 'off'
action:
  service: homeassistant.turn_on
  entity_id: script.bedroom_chancel_sleepmode

This automation should only run if input_boolean.sleepmode is off and another script is running.

This script

bedroom_activate_sleepmode:
    alias: "Aktivere nach 10 Minuten Schlafmodus"
    sequence:
      - delay:
          minutes: 10
      - service: switch.turn_on
        data:
          entity_id: input_boolean.sleepmode
      - service: notify.Server
        data:
          message: 'Schlafmodus aktiviert'

Only if it is running it should run the automation. If not i would get a message every time a motion sensor is triggered. :wink:

You can’t check if the other script is running, but you can create a (hidden) ìnput_boolean` “script_is_running” and have “the other script” switch it to “on” when it starts and “off” when it finishes.
All you need to do then, is to check the state of the switch.

Sebastian

ok i will try that, thanks

You can check the state of the script to determine if it is running.

- condition: state
  entity_id: script.timed_movement
  state: 'on'
7 Likes

Cool, I didn’t know this :slight_smile:

Sebastian

Can someone confirm that checking script state works? I’ve seen other threads that suggest it does not.

You can test it yourself. Create two scripts:

  test_delay:
    sequence:
    - service: system_log.write
      data_template:
        message: Start of Delay
        level: warning
    - delay: '00:01:00'
    - service: system_log.write
      data_template:
        message: End of Delay
        level: warning

  test_if_running:
    sequence:
    - service: system_log.write
      data_template:
        message: The test_delay script is {{'running' if is_state('script.test_delay', 'on') else 'not running'}}
        level: warning

To initialize them, either restart Home Assistant or simply execute Reload Scripts.

  • Go to the Services page, select script.test_delay, then click CALL SERVICE.
  • A few seconds later, select script.test_if_running, then click CALL SERVICE.
  • Switch to the About page (dev-info page) and you should see two messages. One indicates the test_delay has started and the second one says test_delay is running.
  • If you wait a full minute, the test_delay script will finish and report it in the log.

Sample log output:
Screenshot%20from%202019-07-05%2021-08-10

It does work. But be aware that this only applies to scripts. Not automations. So you can’t use it to check if an automation is currently running.

For a automation, the on state just tells that the automation is enabled.

1 Like