Help with script to manual trigger automation!

I want an option in the dashboard to manually trigger automations. I guess I could use virtual switches using input_select for this but I have also seen that scripts get a nice “ACTIVATE” in the interface.

Could someone please help me with an example of a script that runs a specific automation? I’ve read the https://home-assistant.io/components/script/ but don’t quite understand.

I dont know if its been changed its been a while since i did this, but you should be able to see the script to a group…

  • script.scriptname

and it shows up as a button on the top.

1 Like

Yes, scripts seems to show up as entity_id with the name script.scriptname so adding them to a group or so shouldn’t be an issue. What I don’t get is how to create a script that triggers an automation.

Here’s as script that called an automation

test:
  sequence:
    - service: automation.trigger
      entity_id: automation.autoatticfanturnoff
2 Likes

Just out of curiousity…why would you want to call an automation with a script instead of building a script? The essentially do the same thing except automations are designed to run without a user calling it.

Thanks @treno! I’ll check this out. I assume you put it under script: in configuartion.yaml?

@Jer78: I have an automation that runs on a specific time (a night scene that turn off lights, locks the doors and set a virtual switch to “night” which enables automatic night lights if PIR sense movement). Sometimes I want to set the house in night mode before the ordinary time.

I could trigger the automation manually but in the interface you need to press twice, first once on the automation and then on “TRIGGER”.

A script that runs an automation can be triggered manually directly, pressing “ACTIVATE” in the interface.

I don’t know if this is the best way of manually running an automation, so I’m open to suggestions.

Oh ok. I see what you’re doing. I think the better way to do this would be to put all of the actions in the script and then call the script in an automation based on the time. Then also put script.yourscriptname in the groups section so you can see it on your front end. If you need help, post your automation here and I’ll show you.

I’m still trying to understand scripts @Jer78 so I’m not quite sure I understand you but here is the automation I want to be able to run manually without having to click twice in the interface (I’m very lazy…):

  initial_state: True
# Turn off all lights and lock
  trigger:
    platform: state
    entity_id: input_select.time_mode
    from: 'Day'
    to: 'Night'
  condition:
  action:
  - service: automation.trigger
    entity_id: automation.everything_off
  - service: cover.close_cover
  - service: media_player.volume_set
    data:
      entity_id: group.chromecast_audio
      volume_level: 1.00
  - service: tts.google_say
    entity_id: group.chromecast_audio
    data:
      message: 'Good night!!'
  - service: notify.notifications_ios
    data: 
      message: 'Good night!'

No problem. So here’s the example I was explaining.

First this is your automation. I listed 2 different triggers, first triggering at sunset (if you have the sun component and your long lat populated in your config of course) and the second at a specific time of day. Change or delete one of them as required.

alias: 'Turn off all lights and lock'
  initial_state: true
  trigger:
    - platform: sun
      event: sunset
    - platform: time
      at: '22:00:00'
  action:
    - service: script.turn_on
      entity_id: script.everything_off

And this would be the script.

 everything_off:
   sequence:
     - service: cover.close_cover
       entity_id: cover.whateveryourcoverisnamed
     - service: media_player.volume_set
       data:
         entity_id: group.chromecast_audio
         volume_level: 1.00
     - service: tts.google_say
       entity_id: group.chromecast_audio
       data:
         message: 'Good night!!'
     - service: notify.notifications_ios
       data: 
         message: 'Good night!'

So finally put this somewhere in your groups

script.everything_off

So you should now be able to call the script with one click, and it also run at the predetermined time.

I have one small thing to add. Think of scripts as the “action” portion of an automation. A script just executes those actions as soon as you activate it. You can activate it by pressing manually (once!) or by calling it in an automation.

What @Jer78 has described will do what you want perfectly, in the cleanest way I can imagine. It’ll just be one press to manually activate it, and if you want to add additional actions in the future, you’ll only have to update the script, not the automation!

1 Like

Hi, i know its a old topic but i got a question and hope if you could help.

What i would i like is the script Activate button on my frontend like @n0ir. I was thinking about a “bedtime mode” which turns off all the lights in my house at 23.15 hour from Monday till Friday, except for the bedroom. And if the bedroom light is active/on it dims the light to 30%. The thing is sometimes when i have a extra day off i stay up late in my office, i dont want it to turn of the light when im sitting there. I do track my PC trough Ping to see if its Online or Offline so that could be the Yes or No for it to trigger.

I thought about a couple of ways from a automation standpoint:

  • Automation 1 checks PC
  • Automation 2 shutsdown Lights at 23.15 from ma to fr except bedroom
  • Automation 3 Dims bedroom light to 30% (or activates ‘30% scene’)

Now im stuck in which way would be the best approach.:expressionless:

Thanks in advance!

You can actually put conditions inside automation actions as well. So you could have one automation that triggers at 23:15, turns off all lights except bedroom and office, checks if PC is active, if yes stops, if no turns off office lights.

Look at the last automation on this page for an example of a condition in an automation.

Once the condition fails the automation stops, so you either need a separate one to dim your bedroom, or, you could get fancy and use templating to turn off the bedroom light if it’s off, or turn on the bedroom light to 30% if it’s on. If you do use templating, it will probably be easiest to just use a template for the office light too because it will basically be copy>paste but with a different entity id.