Trigger an automation when another automation triggers

The tittle says it all. I am trying to figure it out with no success.
Here is what I have:
When motion is detected a script triggers. This script is triggering one notify service, one shell command, one counter and one automation (lets call it A). This automation (A), is to take a camera snapshot. << All this is the standard procedure.

Now, I am trying to send the snapshot on my phone only when I am not home. I created another automation (call it B) that sends me the file with success. The problem is that I cant trigger this automation (B) when (A) triggers.

Can it be done or there is another trickier method?

In the action section of A put

  - service: automation.trigger
    entity_id: automation.B
2 Likes

You could also do an event trigger if you want to segregate the automation:

automation:
  alias: b
  trigger:
    - platform: event
      event_type: call_service
      event_data:
        domain: automation
        service: turn_on
  condition:
    condition: template
    value_template: "{{ trigger.event.as_dict()['data']['service_data']['entity_id'] == 'automation.a' }}"

EDIT: Just to clarify, you may need to turn logging to debug to catch the event and use the proper trigger attributes. the service may not be turn_on and the domain may be different as well. But the platform and event_type are correct.

1 Like

@anon43302295 This was the obvious way to do this and I also thought it would work when I created the automation yesterday. While it triggers the automation, it bypasses the condition (when i am not home) written in automation (B).

@petro Something tells me that your automation will work, but I didnt understand what this is going to do exactly. While I copied your automation, it didnt work but had no errors when I triggered it.

OK… To be more specific I will paste my current configuration here:

scripts.yaml

stairsmotion:
  alias: Stairs Camera Motion Detection ON
  sequence:
  - data:
      data:
        method: alarm
      message: chime
    service: notify.jarvis_lenovo
  - service: shell_command.cctv_kodi_stairs
  - service: counter.increment
    entity_id: counter.motioncounter_stairs
  - service: automation.trigger
    entity_id: automation.sys_stairs_camera_snapshot (this is automation A - it takes the snapshot)

automations.yaml

- id: 'pushsendimagecamerasnapshotfromstairs' <<< (This is Automation B)
  alias: '[PUSH] Stairs Camera snapshot Send'
  trigger: []
  condition:
  - condition: state
    entity_id: group.presence_tracker
    state: home  <<< I have this at "home" state for testing purposes. When it works I will have it "not_home"
  action:
  - delay: 00:00:01
  - service: notify.pushbullet
    data:
      data:
        file: '/home/homeassistant/.homeassistant/www/images/snapshot.jpg'
      title: Stairs Motion Sensor Triggered
      target: device/Argy
      message: Take this snapshot of the stairs.

Yes, I’m not sure if the trigger data is correct. I’d have to see event data in the home assistant logs (with debug on) to correctly write the trigger, which is why I mention that in the bottom of the post.

All automation be is doing is triggering when automation.a is fired. They will be executed at the same time essentially.

The difference between the 2 methods is that @anon43302295’s method will fire the automation sequentially. The method I posted should fire them in parallel.

If you don’t want the headache of dealing with the event_data, go with @anon43302295’s solution.

@petro I cant work because of that. It just triggers the automation no matter if I am home or not.
I still want all the things in the script to work no matter what, but when I am not home, I want this extra automation (send me the file) to trigger.

you could add a service condition to call based on if you are home or not.

1 Like

That’s a sticky situation. Turn on debugging and grab a service call when auotmation a runs and I can help make automation B work.

Move the condition for automation B in to the action block, for example

- alias: automation b
  trigger:
    platform: state 
    entity_id: xxx
  condition:
    condition: state 
    entity_id: device_tracker.whatever
    state: home 
  action:
    - service: homeassistant.turn_on 
      entity_id: light.whatever

Becomes:

- alias: automation b
  trigger:
    platform: state 
    entity_id: xxx
  action:
    - condition: state 
      entity_id: device_tracker.whatever
      state: home 
    - service: homeassistant.turn_on 
      entity_id: light.whatever
``
1 Like

Wouldn’t it never fire because he specifically wants automation b to only fire when he’s not home?

not_home then :stuck_out_tongue:

I had no idea that was possible. Thank you for sharing.

1 Like

@anon43302295 It looks like it didnt work. It did nothing… not even errors. I tried both “home” and “not_home” for testing purposes.

@petro My log is full of this frustrating “Failed to parse headers” thing when you have a generic camera, so I didnt bother searching for the right part to paste.

Anyway I found another workaround.

I created a boolean
booleans.yaml

sendmesnapshots:
  name: When on sends photos of camera
  initial: off

I added 3 extra lines on the previous script where it turns on the boolean for 5 seconds and then turns it off:
scripts.yaml

stairsmotion:
  alias: Stairs Camera Motion Detection ON
  sequence:
  - data:
      data:
        method: alarm
      message: chime
    service: notify.jarvis_lenovo
  - service: shell_command.cctv_kodi_stairs
  - service: counter.increment
    entity_id: counter.motioncounter_stairs
  - service: automation.trigger
    entity_id: automation.sys_stairs_camera_snapshot
  - service: input_boolean.turn_on
    entity_id: input_boolean.sendmesnapshots
  - delay: 00:00:05
  - service: input_boolean.turn_off
    entity_id: input_boolean.sendmesnapshots

And then I had automation B sending me the snapshot using the boolean as a trigger and condition when I am not home. Automation A remains the same as before, that means always save the snapshot no matter what.

automations.yaml

- id: 'camerasnapshotfromstairs' <<< **This is automation A**
  alias: '[SYS] Stairs Camera snapshot'
  trigger: []
  condition: []
  action:
  - service: camera.snapshot
    data:
      entity_id: camera.stairs
      filename: /home/homeassistant/.homeassistant/www/images/snapshot.jpg

- id: 'pushsendimagecamerasnapshotfromstairs' <<< **This is Automation B**
  alias: '[PUSH] Stairs Camera snapshot Send'
  trigger:
  - platform: state
    entity_id: input_boolean.sendmesnapshots
    to: 'on'
  condition:
  - condition: state
    entity_id: group.presence_tracker
    state: not_home
  action:
  - delay: 00:00:02
  - service: notify.pushbullet
    data:
      data:
        file: '/home/homeassistant/.homeassistant/www/images/snapshot.jpg'
      title: Stairs Motion Sensor Triggered
      target: device/Argy
      message: Take this snapshot of the stairs.

This is working for me, but I feel like it could be much simpler. If you guys have another idea, I am willing to discuss it. Thank you for your suggestions so far!

OK, here’s another suggestion.

Put the action of automation B in a script.

Make automation B call the script.

Make automation A call the script.

Example:

automation:
  - alias: A
    trigger:
      platform: state 
      entity_id: Whatever_triggers_A
    action:
      service: homeassistant.turn_on 
      entity_id: switch.switch_whatever_A

  - alias: B
    trigger:
      platform: state 
      entity_id: Whatever_triggers_B
    action:
      service: homeassistant.turn_on 
      entity_id: switch.switch_whatever_B

Becomes

script:
  that_b_thing:
    sequence:
      - service: homeassistant.turn_on 
        entity_id: switch.switch_whatever_B

automation:
  - alias: A
    trigger:
      platform: state 
      entity_id: Whatever_triggers_A
    action:
      - service: homeassistant.turn_on 
        entity_id: switch.switch_whatever_A
      - service: script.that_b_thing

  - alias: B
    trigger:
      platform: state 
      entity_id: Whatever_triggers_B
    action:
      service: script.that_b_thing

That said, now I think I understand your use case, the second automation is irrelevant. All you need is one automation with a conditional final action…

- id: 'camerasnapshotfromstairs'
  alias: '[SYS] Stairs Camera snapshot'
  trigger: []
  condition: []
  action:
  - service: camera.snapshot
    data:
      entity_id: camera.stairs
      filename: /home/homeassistant/.homeassistant/www/images/snapshot.jpg
  - condition: state
    entity_id: group.presence_tracker
    state: 'not_home'
  - delay: 00:00:02
  - service: notify.pushbullet
    data:
      data:
        file: '/home/homeassistant/.homeassistant/www/images/snapshot.jpg'
      title: Stairs Motion Sensor Triggered
      target: device/Argy
      message: Take this snapshot of the stairs
3 Likes

OK, you have to explain what a condition does when you use it in an action. :sweat_smile:

TRIGGER
ACTION 1
ACTION 2
ACTION 3
CONDITION - nobody home
ACTION 4
CONDITION - bedroom lights are on
ACTION 5

If nobody is home and the bedroom lights are on, all 5 actions run.

If nobody is home and the bedroom light is off, only actions 1 - 4 run.

If somebody is home, only actions 1 - 3 run, the state of the bedroom light is irrelevant because the actions never get that far.

Hope this helps.

4 Likes

I will try this ASAP! You are an eyeopener! I had no idea this could be done! I have seen the documentation for the actions a lot of times (and the page is small with just 2 examples) but I never really saw how important is the second example! I was just not reading it!
Thank you for your suggestions.

1 Like

Does anyone know how to do inline conditions.

I have an automation:


- alias: 0 Close blind at dusk
  condition:
  - condition: state
    entity_id: cover.garden_blinds
    state: open
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 2
  action:
  - service: tts.google_say
    data:
      entity_id: media_player.googlehome5819
      message: Hi All, The sun has set, closing the blinds in 3 minutes, please close
        the back doors
  - delay: 00:03:00
  - service: cover.close_cover
    entity_id: cover.garden_blinds

I would like to only send

please close the back doors

if they are open

Blank lines added just to highlight the changes…

- alias: 0 Close blind at dusk
  condition:
  - condition: state
    entity_id: cover.garden_blinds
    state: open
  trigger:
    platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 2
  action:

  - service: tts.google_say
    data_template:
      entity_id: media_player.googlehome5819
      message: >
        Hi All, The sun has set,
        closing the blinds in 3 minutes.
        {% if is_state('sensor.back_door' , 'open') %} 
          Please close the back doors.
        {% endif %} 

  - delay: 00:03:00
  - service: cover.close_cover
    entity_id: cover.garden_blinds