Automation turn on/off

I find out that it is possible to manage automation similar with scripts by turning them on or off, while this is not described in automation topics (at least I didn’t find), so I mention it here with examples, this may help other people:

- alias: 'test-on'
  trigger:
    platform: state
    entity_id: input_boolean.ack10
    state: 'on'
  action:
    service: automation.turn_on
    entity_id: automation.Automate_1

- alias: 'test-off'
  trigger:
    platform: state
    entity_id: input_boolean.ack10
    state: 'off'
  action:
    service: automation.turn_off
    entity_id: automation.Automate_1
9 Likes

Sure can. I use this in one of my automations now.

# Rain Alert

- alias: 'Raining'
  trigger:
    platform: state
    entity_id: sensor.dark_sky_precip
    from: '0'
  action:
    - service: tts.google_say
      data_template:
        message: 'Excuse me Robert, but is currently {{ states.sensor.dark_sky_precip.state }}ing and it is {{ states.sensor.dark_sky_temperature.state | int }}degrees outside.  Forecast says {{ states.sensor.dark_sky_minutely_summary.state }}.'
        cache: false
    - delay: 00:01:00
    - service: notify.pushbullet
      data_template:
        message: >
          It is currently {{ states.sensor.dark_sky_precip.state }}ing and it is {{ states.sensor.dark_sky_temperature.state | int }}degrees outside.  Forecast says {{ states.sensor.dark_sky_minutely_summary.state }}.
    - service: automation.turn_off
      entity_id: automation.raining
    - delay: 00:30:00
    - service: automation.turn_on
      entity_id: automation.raining
2 Likes

Yes, it works, but is it documented? I think it is a nice and usefull feature and people have to know about it.

Very true. I found another solution to this problem that is more complicated than using automation.turn_on/automation.turn_off. I created an input boolean and created an automation to turn it on and off. Then in my other automation, I made the input boolean a condition. I might have to revisit my automatons and use this instead. Thanks for the post.

It’s just like any other service as shown in the docs:

But if you feel it needs to be documented, you can update the service calls page by clicking on the link to “Edit this page in GitHub” in the upper right hand corner. That would be helpful if you’re willing to do so.

You right, it is a service, but this is not obvious for everybody. I will take a look closer through the pages, I am not sure where is best to mention this, I think somwhere in automation pages could be inserted an action example of script and automation management.
Thank you

2 Likes

I am struggling to get a automation working that I think is similar to yours and wonder if you might have any suggestions. I want to click a button from home assistant that uses my pi’s gpio pins to turn on switch_1, and 2 seconds later turns on switch_2. Switch_1 should stay on until I turn it off or for 10 minutes, whichever comes first. Switch_2 should only stay on for .5 seconds.

I have spent a few hours messing around with gpio switch, and pi covers time_delay without success. Any thoughts on how to configure that?

I am running 0.51.2 on a raspberry pi 3 that I setup using Hassio.

Sorry; I can’t think of anything besides using the delay in the actions as I posted above.

I suggest to do the following:

  • make one automation routine which:
    • triggered by switch1
    • start script1
    • turn on the switch2
    • delay 1s (you can’t less…)
    • turn off the switch2
  • make script1:
    • wait for 10 min
    • turn off switch1
      Switch 1 and 2 may be linked to GPIOs or not, as you wish.

I would do…(in pseudo code)

trigger virtual_button
action
  turn_on: switch.switch1
  turn_on: switch.switch2
  delay (1s) (I think only appdaemon can do less than 1s?)
  turn_off: switch.switch2
  delay (10mins)
  turn_off: switch.switch1

Which I think is pretty much what @rpitera was saying.

And did you succeed?

Thanks for the ideas. I’m not finished making the automation (some of my labels are just dummies), but this is working:
- id: sub
alias: sub
initial_state: true
trigger:
platform: state
entity_id: switch.car_start
to: ‘on’
action:
- service: switch.turn_on
data:
entity_id: switch.car_on
- delay:
seconds: 1
- condition: state
entity_id: switch.car_on
state: ‘on’
- service: switch.turn_on
data:
entity_id: switch.car_lock
- delay:
milliseconds: 500
- service: switch.turn_off
data:
entity_id: switch.car_lock

(I tried using the preformatted text option but it doesn’t look like it worked…)

This will allow me to remotely start my car. I have a relay that turns the car on (like turning the ignition half way), I will then check to make sure the car is on but not running, then I have a relay that cranks the starter for .5 seconds.

I found that you can delay less than 1s. This delays for .5 seconds:
- delay
milliseconds: 500

Good to know, thanks, when I wrote my automation I didn’t see this option available.