Triggering an Automation Request

Hi All

Looking for some guidance for single automation, although I can achieve the same from multiple automations created.

I want to trigger an automation 10 mins after I press a particular button, this automation is for a home security, hence 10 mins give me enough time to leave home and I want the automation to end/disable once I am back at home, without me having to press any buttons.

I can create 3 separate automation to do this but want to perform these functions in 1 automation. Any support is appreciated.

Thanks

Post the three automations and we’ll help you consolidate them.

Thanks for that, do you think that home and away location entities can help trigger the automation better? rather than relying on a presence sensor, which I only have a few? i.e, If I am away, the automation triggers, and if I am the automation turnoff.

Also, how the automation will end once I am back home as I don’t want the alarms to trigger, especially at night.

device trackers don’t have a state of “home” and “away”.

They have a state of “home” and “not_home”.

if you use “away” it won’t work.

I’m not sure what you mean here.

there are no device tracker or location entities that are automatically created by HA.

the user needs to add them via an applicable integration.

Thanks for this, just have a question, how this automation will continuously check my state of home or away?

almost everything you said in that post is wrong.

No. That’s not the way conditions work.

the conditions in the “condition:” section are only checked one time at the moment the automation gets triggered.

The system does not “continuously” check the condition.

It checks the condition once and either does or does not run the actions.

no.

the state of a device_tracker is either “home”, “not_home” or the name of a configured zone.

there is no state that says “away”.

If the OP uses that state as a condition the actions will never run because the condition will never be satisfied.

Absolutely not.

the automation will only run when the trigger becomes true.

it then checks the condition to see if it is true.

if not the automation ends right there.

if the conditions are true then the actions run - it turns on the alarm panel, waits 10 minutes, checks the device tracker for the home state and if true then turns the control panel back off.

If the second condition (the one embedded in the action) is false at that instant in time after the 10 minute delay then the last action is not run and the automation stops.

it will only run again when the trigger goes from false to true.

it will not “continue indefinitely”.

you don’t need to do any of that to stop the automation.

as explained above the automation as it’s written will happily stop itself as soon as it’s triggered because the condition can never be satisfied - because “away” is not a valid state for a device tracker.

and even if the condition could somehow be satisfied then the automation will stop as soon as the appropriate action steps are taken.

It’s disheartening that the OP is being misled by someone who doesn’t understand how the system works themselves.

Okay Guys, this is what I’ve come up with.

As I said earlier, I want to trigger automation by pressing a button 3 times, then waiting 5 mins for me to leave the house and then arm the Xiaomi Gateway and also calling a security Automation, however, I want this automation to end when I am back home. Can someone please help with the below code.

alias: New Automation
description: ""
trigger:
  - platform: device
    device_id: XXXXXXX
    domain: xiaomi_gateXXXX
    type: button
    action: triple
condition:
  - condition: state
    entity_id: device_tracker.XXXXX
    state: not_home
action:
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - device_id: XXXXXX
    domain: alarm_control_panel
    entity_id: alarm_control_panel.XXXXX_alarm
    type: arm_away
  - service: automation.turn_on
    data: {}
    target:
      entity_id: automation.alarm
mode: single

Thanks

That’s the thing.

You seem to have been misled by the other users explanation of how (they erroneously think) automations work.

(Unless you are wanting to cancel the 5 minute delay) the automation doesn’t continue to run after the actions begin.

Here is the sequence of events:

  • the automation gets triggered by the triple click.

  • it then checks the condition to make sure the device tracker is not_home.

  • If that check returns true then the actions begin…

    • 5 minute delay
    • after the delay then arm the alarm
    • then turn on the second automation

as soon as those actions are complete the automation is done and is no longer running.

You haven’t mentioned what the second automation does so it’s possible there could be additional actions still going on in that automation after the first one is complete.

If you want to cancel the 5 minute delay that is possible too. But it doesn’t sound like that’s what you asking for.

And I believe you are not using the service call for the second automation correctly. And that might have been caused by the incorrect explanation above too.

Automations can have two states - on and off. It’s better to think about the state of the automation being enabled (on) or disabled (off).

the default state of an automation once it’s created is on. That means the automation is enabled and waiting for a trigger to occur.

an automation that is off still exists in the system but it will never trigger. It’s still there but it won’t do anything.

the service call that you are using will only change the state of the automation to on. But if the automation is already on (i.e. the automation is enabled) then that service call won’t do anything.

I can’t be sure but I think that you might want to use the automation.trigger service call. that will cause the automation to trigger and begin running it’s actions (I believe that by default it even skips checking the conditions but that is configurable in the service call).

That said if that’s the goal and there are no other triggers of that second automation then using that service to trigger an automation is fairly useless.

you should probably just convert the second automation to script and call the script instead (scripts are just automations without a trigger).

But since you haven’t posted the code for the second automation it’s hard to know for sure.

I’m really sorry that this post seems to be rambling but there are so many unknowns I’m trying to cover them all here.

So lastly are you asking how to disarm the alarm (and/or undo the changes made by the second automation as well) when you come back home?

if so then you need to create another automation to handle that trigger.

You can do it in one but you should do it in two for now until you get a bit more advanced.

Thanks for your response, here is 2nd automation which essentially gets triggered by 1st one and starts monitoring the motion/contact sensors of the house for security purposes.

alias: Alarm
description: ""
trigger:
  - type: motion
    platform: device
    device_id: XXXXXXXX
    entity_id: binary_sensor.XXXXXXX_motion
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: XXXXXXXX
    entity_id: binary_sensor.XXXXXXXX_motion
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: XXXXXXXX
    entity_id: binary_sensor.XXXXXXXX_contact
    domain: binary_sensor
  - type: opened
    platform: device
    device_id:XXXXXXXX
    entity_id: binary_sensor.XXXXXXXX_contact
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: XXXXXXXX
    entity_id: binary_sensor.XXXXXXXX_contact
    domain: binary_sensor
condition:
  - condition: state
    entity_id: device_tracker.XXXXXXXX
    state: not_home
    for:
      hours: 0
      minutes: 10
      seconds: 0
action:
  - service: media_player.play_media
    target:
      entity_id: media_player.home_group
    data:
      media_content_id: media-source:/XXXXXXXX
      media_content_type: audio/mpeg
    metadata:
      title: XXXXXXXX
      thumbnail: null
      media_class: music
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://media_source
mode: single

but it doesn’t get triggered at all by the first one.

at most it gets turned on by the first one and then it can be triggered by any of the motion sensors.

But I’m still not sure what you expect the service call that turns on the second automation to do.

Do you normally have the second automation off and then turn it on to start being triggered by the motion sensors?

the default state of an automation is on so unless you have been manually turning it off then the second automation has been on the entire time and triggering and just not running the actions while you are home because the condition is blocking the actions from running.

running the service call to turn the automation on in the first automation is likely completely useless unless you are turning the automation off manually every time.

But seeing the second automation I agree that it needs to be an automation instead of a script because it needs the triggers to run the correct actions.

also you still haven’t said exactly you want the modified first automation to do.

What exactly does this mean?

Here is what I want

I want to press a button and after 5 mins, it triggers a security routine/script to start monitoring my motion sensors and contact sensors and send me a message if it detects a motion. And I want this script/routine to double check that I am away from house and I want this routine to end when I am back at home.

Thanks

The issues I see here are the following:
The device tracker, what type of tracker is it? GPS or router?
Either way the first automation, you triple press and then the condition is that you are not_home, that will be hard to fulfill because I assume the button is at home.

I understand the second automation is some kind of alarm, good.
Now the issue is that it will trigger and sound the alarm when you get home a few times I believe since device trackers, both GPS and router can be slow.

Perhaps you should just create an alarm entity and try ang configure that?
Have a look at everything smarthome with Lewis, he made an alarm video a few weeks ago.

I assume, not trigger.
I believe he is trying to set up a simple alarm using a button for activating and geofence to turn off.

Correct. And I am using my phone GPS as tracker. Button is at home.

Have a look at this video. I believe there is a follow-up video on it also.

If you want to use an automation as you had started then this is what I would do…

use the first automation without the not_home condition and the last service call:

alias: New Automation
description: ""
trigger:
  - platform: device
    device_id: XXXXXXX
    domain: xiaomi_gateXXXX
    type: button
    action: triple
action:
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - device_id: XXXXXX
    domain: alarm_control_panel
    entity_id: alarm_control_panel.XXXXX_alarm
    type: arm_away
mode: single

then in the second automation use the not_home condition in that one. That way you have the 5 minutes to leave (actually it’s 10 minutes due to the condition in this automation) before the motion sensors start triggering the media player

you can also add as a condition that the alarm is armed_away just another check to ensure it doesn’t set off the media player if your device goes to not_home for some reason:

alias: Alarm
description: ""
trigger:
  - type: motion
    platform: device
    device_id: XXXXXXXX
    entity_id: binary_sensor.XXXXXXX_motion
    domain: binary_sensor
  - type: motion
    platform: device
    device_id: XXXXXXXX
    entity_id: binary_sensor.XXXXXXXX_motion
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: XXXXXXXX
    entity_id: binary_sensor.XXXXXXXX_contact
    domain: binary_sensor
  - type: opened
    platform: device
    device_id:XXXXXXXX
    entity_id: binary_sensor.XXXXXXXX_contact
    domain: binary_sensor
  - type: opened
    platform: device
    device_id: XXXXXXXX
    entity_id: binary_sensor.XXXXXXXX_contact
    domain: binary_sensor
condition:
  - condition: state
    entity_id: alarm_control_panel.XXXXX_alarm
    state: arm_away
  - condition: state
    entity_id: device_tracker.XXXXXXXX
    state: not_home
    for:
      hours: 0
      minutes: 10
      seconds: 0
action:
  - service: media_player.play_media
    target:
      entity_id: media_player.home_group
    data:
      media_content_id: media-source:/XXXXXXXX
      media_content_type: audio/mpeg
    metadata:
      title: XXXXXXXX
      thumbnail: null
      media_class: music
      children_media_class: null
      navigateIds:
        - {}
        - media_content_type: app
          media_content_id: media-source://media_source
mode: single

then you can create a third automation to turn off the alarm control panel. And since you are now home then the condition in the second automation prevents the media player from sounding:

alias: Return Home Automation
description: ""
trigger:
  - platform: state
    entity_id: device_tracker.XXXXX
    to: home
action:
  - device_id: XXXXXX
    domain: alarm_control_panel
    entity_id: alarm_control_panel.XXXXX_alarm
    type: disarm
mode: single

Thanks for your response, is there a way to combine all these automation into a single script?

Not easily. There will be a lot of if-else-choose-etc.

there really isn’t an advantage to one automation.