Several actions in same automation using "home" and "not_home"?

Hi there!

I have a camera, which it’s controlled by a RF switch. So, I want to turn on that switch when I’m not home, and turn it off when I’m home.

This is what I have so far:

- alias: Security CAM on when not home
  trigger:
    platform: state
    entity_id: device_tracker.weehente_phone
    from: 'home'
    to: 'not_home'
    for:
      hours: '0'
      minutes: '5'

  action:
     service: switch.turn_on
     entity_id: switch.cam

What I don’t know is how to add another trigger when changing from “not_home” to “home” and another action: switch_turn.off

I know I can create another automation, but I’d rather have it all under the same one.

Thanks!

Example script with multiple actions. Believe you can simply just write multiple actions in this way in your automation. If not then just create a script with all the actions and call it from the automation.

You can add a second trigger using the syntax documented here:

Then you will have to use a template to determine whether to call the turn_on service or the turn_off service
That function is documented here:

Something like…

- alias: Security CAM on when not home
  trigger:
    platform: state
    entity_id: device_tracker.weehente_phone
  action:
     service_template: >-
      {% if is_state ('device_tracker.weehente_phone' , 'home')%}
       switch.turn_off
      {% else %} 
       switch.turn_on
      {% endif %} 
     entity_id: switch.cam
2 Likes

Thank you all! Let me test it and get back to you :slight_smile:

Thought about that approach to, but with a single trigger you can get the “for x minutes” option, so you run the risk of toggling your switch rather rapidly.

With two triggers, each can have the “for x minutes” option.

… Or just put the delay in the action.

Then if you’re using the ‘for’ as an error checker in some circumstances put a condition after the delay to ensure the state is still what you want.