How do I create a condition in an action the easiest way?

I’m going round in circles calling one script to fire another which in turn fires another. There must be an easier way. From the code below, I’m trying to make the - service: script.notify_engine only fire if I’m not home.

automation:
  - alias: 'Notify Pool Gate Open'
    initial_state: true
    trigger:
        platform: state
        entity_id: binary_sensor.door_window_sensor_pool_gate ##
        from: 'off'
        to: 'on'
        for:
          seconds: 02
    action:
      - service: rest_command.move_to_preset_02_07s
      - service: rest_command.trigger_pool_deck_15s ## Records camera for 15s ##
      - delay: '00:00:03'
      - service: camera.snapshot
        data:
            entity_id: camera.pool_deck
            filename: '/config/www/images/pool_gate.jpg'
      - service: script.notify_engine
        data_template:
          message: "Pool Gate was opened"
          file_url: http://10.0.1.200:8123/local/images/pool_gate.jpg
      - service: notify.kodi
        data:
          title: "Home Assistant"
          message: "The Pool Gate opened at {{ states.sensor.time.state }}"
          data:
            displaytime: 5000

I know I can use a service_template to determine if I’m not_home and then fire s script from that but I was getting to four scripts deep to make this happen before I pulled the eject pin.

I want to mostly use the - service: script.notify_engine code in my notifiers but with an easy way of making some of them conditional on my home/away status. At the moment my phone is going crazy all day long as gates are being opened and I only want this happening when I’m away (or possibly when the alarm is set).

Any help would be appreciated.

I’m not too familiar with scripts.
One idea would be to create a second script with the same content but with a condition to only execute the script when you are home. Something like script.notify_engine and script.notify_engine_home and then call the respective script in your automations.
Another idea would be passing a variable to your script and then have a template condition in your script based on the passed variable.

    action:
      - service: rest_command.move_to_preset_02_07s
      - service: rest_command.trigger_pool_deck_15s ## Records camera for 15s ##
      - delay: '00:00:03'
      - service: camera.snapshot
        data:
            entity_id: camera.pool_deck
            filename: '/config/www/images/pool_gate.jpg'

      - condition: state
        entity_id: person.xbmcnut
        state: 'not_home' 

      - service: script.notify_engine
        data_template:
          message: "Pool Gate was opened"
          file_url: http://10.0.1.200:8123/local/images/pool_gate.jpg
      - service: notify.kodi
        data:
          title: "Home Assistant"
          message: "The Pool Gate opened at {{ states.sensor.time.state }}"
          data:
            displaytime: 5000

Blank lines added for effect.

If you still want the Kodi notification (I presumed you didn’t), move it up to above the condition.

1 Like

I’m a dumb dumb. Clearly overthinking it! Thanks for putting me straight. That will suit fine for this application.

1 Like

Be aware tho that nothing past the condition will be executed if the condition returns false. So if necessary you can move the conditional action to the end of the automation with the condition immediately before it.