How to create a template that triggers when myscript is running at sunrise?

I’m looking for the yaml that would create an automation that triggers when a myscript is running at sunrise. I though a template would be the way to go, but I cannot find any examples that show how to access the state(?) sunrise in a template. Any help?

What is that?

What sensors does it expose to home assistant?

Assuming you have a sensor that tells you if it is running you don’t need templates.

Sun trigger → sunrise

State condition → myscript is running

I was thinking something like {{ if is_state(‘sun.sun’, ‘sunrise’) and is_state(‘script.myscript’, ‘on’) }}, but I don’t think ‘sunrise’ is a state of ‘sun.sun’.

Oh so “myscript” is an actual HA script. Not a device (a seach turned up an electronic notepad).

As I said you don’t need a template.

trigger:
  - platform: sun
    event: sunrise
condition:
  - condition: state
    entity_id: script.myscript
    state: 'on'
action:
 ...

This will only run the actions if your script is running at sunrise.

Thanks, tom_l! I apologize that I did not give a little more context. This “if my script is running at sunrise” trigger is only one of three or four events that I want to trigger the same action. Here is an example.

trigger:
  - device_id: 6201959885d648bab796c15491213c22
    domain: zha
    platform: device
    type: press
    subtype: Config
  - platform: state
    entity_id:
      - input_button.hallway_arctic_aurora_scene
  - platform: conversation
    command:
      - "[activate | turn on] [the] Arctic Aurora [scene] in [the] Hallway"
      - "[activate | turn on] [the] Hallway Arctic Aurora [scene]"
condition: []
action:
...

Because of this, I don’t think I can use both trigger and condition in the way you’ve shown, to add “if script is running at sunrise,” because the “script is running” condition would apply to all the triggers when I only want it to apply to one - sunrise.

Here’s a hint to get help faster: when you want an answer to a question don’t ask a different only slightly related question. It wastes my time. It wastes your time.

And you still haven’t adequately explained your problem.

Do you want your actions to run when any of those triggers occur and your script is running?

Or all those triggers simultaneously and your script is running?

Start again. Be clear and concise. We aren’t mind readers.

2 Likes

Thanks, tom_l. Sorry if I am omitting any needed details. This is my first time posting in the forum, so I appreciate your suggestions.

The YAML I show is auto-generated from the UI. The trigger is currently working to trigger the action of activating the Arctic Aurora scene in the Hallway by calling a script named Hallway Hue Scenes with some values (scene ‘Arctic aurora’, for one), as expected. The behavior appears to ‘OR’ the three events specified under trigger so that the scene is activated upon any one of the following events occurring:

  • press the Config button on my Inovelli Blue 2-1
  • press the input_button (responds to my Google Home voice command to “activate Hallway Arctic Aurora scene”)
  • speak one of the sentences shown to home assistant voice assist

What I am trying to figure out in this post is how to add a fourth event …

  • if it is sunrise and the Hallway Hue Scenes script is running

So, to answer your question, I only want my actions to run when any of these four triggers occur, where the fourth trigger is “sunrise AND script is running.”

To add a little more, the action I will perform upon this fourth trigger is to restart this running script to boost brightness to 100% (from 50%) for the lights running this scene. Ultimately, I will add a fifth trigger “sunset AND script is running” that will restart this script to dim brightness to 50% (from 100%).

Most scripts are designed to execute promptly (in seconds) so checking to see if they’re on isn’t practical because they’re in that state very briefly.

What is your script doing that allows it to run for so long that you can test if it’s on?

It sounds like you’re using Trigger IDs to run different actions depending on the trigger is that correct? If so, you could add a condition building block, inside of the Sequence instead of the automation’s condition.

In other words if your automation is triggered by the sunrise trigger, then and only then, check to see if your script is running, if it isn’t, stop there, if it is, change brightness to 100%.

Thanks Taras. The blueprint is from Nils (Dynamic, Hue-like Color Scenes with Randomly Distributed Colors). Using it, I create a script called ‘Hallway Hue scenes’ that applies this blueprint to my Hallway Hue Overhead lights. In a nutshell, this blueprint has a dictionary of Hue-like scenes, where each scene consists of a color palette (usually five colors). From this palette, the blueprint randomly selects colors to assign to the supported lights in the specified area. In addition to scene, brightness, and transition, the blueprint offers the ability to repeat this random color assignment every 00:00:00 seconds, providing a subtle yet dynamic lighting scene (very nice!). I have this repeat occurring every 10 seconds in most of my calls to ‘Hallway Hue scenes’. Since I often have the hallway running a scene morning through evening (with the colors being randomly reassigned from the palette every 10 seconds), my goal is to use sunrise/sunset to brighten/dim the running script (scene) appropriately. Does that help?

Here is an example of a call to script ‘Hallway Hue scenes’. Below that, is my use of blueprint Hue-like scenes in my ‘Hallway Hue scenes’ script.

  - service: script.hallway_hue_scenes
    data:
      scene: Arctic aurora
      repeat_delay: 00:00:10
      onlyonlights: false
      brightness: 50
      transition: 5
alias: Hallway Hue scenes
use_blueprint:
  path: nilsreiter/ha-scenes.yaml
  input:
    target:
      area_id: hallway
icon: mdi:lightbulb-group

Yes. I see that it can perform a transition lasting up to 30 seconds (user configurable) per light.

I imagine you know that restarting Home Assistant terminates all automations and scripts that are in-progress. That leaves devices controlled by the terminated automations/scripts in the state they were in at the moment of termination. Unless there’s a ‘restore sanity’ function on startup, the devices will remain in that state. That’s why it’s generally advised not to linger within an automation/script for any extended period of time.

Anyways, is_state('script.foo', 'on') will report true if the script is on. You can selectively apply that test based on which trigger was responsible for triggering the automation. There are several ways to do that but in your case the easiest would be by checking the trigger’s platform.

For example (assuming you use a Sun Trigger):

  - if: "{{ trigger.platform == 'sun' and is_state('script.foo', 'on') }}"
    then:
      ... your actions ...
    else:
      ... your actions ...

That’s not 100% considering they want to have a sunrise and sunset trigger, and they would perform different actions. I think the easiest way would be to add a trigger ID to a sunrise trigger and sunset trigger that way they can differentiate between the two.

You can check which one using trigger.event.

@mark2vir

What are the actions you want your automation to perform based on which trigger was responsible for triggering it.

That’s right, would you recommend that over a trigger ID though?

It depends on the desired actions.

Merely changing brightness can be templated within a single light.turn_on service call.

Thanks Jbautista13. I agree that the using that automation’s condition is not appropriate for the desired set of four triggers.
However, I think you point out something simple that I may have overlooked in my original thought process on this. Whereas I didn’t want to execute my action unless both “sunrise” and “script running” were true, I don’t see why I can’t simply reduce this trigger to “sunrise” then check for “script running” in my action block using a condition building block, like you describe. I am already using a condition block inside my action block to determine if any of the other three triggers is occurring in the daytime or the nighttime (to appropriately set the brightness when one of these original three triggers fires). Perhaps all I need is to bound this daytime/nighttime determination within another condition block that decides whether the script is running:

  • execute sunrise/sunset brightness adjustment when script is running
  • execute daytime/nighttime brightness determination when script not running

Let me give this a try and see if it really is that simple, and I’ll update the post. It’s amazing the simple things I overlook when my HA newbie mind is cluttered with all the understandings required to implement a reasonable automation :slight_smile:.

It can also be reduced to a single State Trigger that detects both sunrise and sunset. Which is the best strategy all depends on the actions you want your automation to perform. That’s why we need to see the balance of your automation (because the example you posted above lacks actions).