Trigger template on reboot

Hi, a quick question:
I created an automation with a trigger based on a template:

{{ state_attr('sun.sun', 'elevation') < -3 }}

why if I restart the core and the condition has already occurred does the automation not start?

I beleive the automation will only trigger once when the elevation goes from above 3 to below 3. If it is already below, it won’t trigger.

Imagine if it triggered for every state update where the elevation is below 3, it would trigger basically all evening and overnight until after sunrise.

Because to trigger the automation the template has to go from evaluating false to true. If it’s already true it won’t trigger.

very right observations! :smile:
I have to find a solution …

You can trigger on Home Assistant start, and use your template as a condition. Not sure if that is the result you are looking for but you can use multiple triggers in the same automation.

Yeah, more or less I did this … creating a new automation for starting HA

`- id: '1234567890123'
  alias: Light Random Restart
  description: Light Random on Home Assistant Start
  trigger:
  - platform: homeassistant
    event: start
  condition: []
  action:
  - service: automation.trigger
    target:
      entity_id: automation.light_random_on
  mode: single`

It seems to work!
Thanks, guys … what a nice Home Assistant!

Except it may not be working for the reasons you think it does.

When you call the automation.trigger service, it will skip the trigger of automation.light_random_on and simply execute its action. In other words, if will not evaluate your Template Trigger. If your automation has a condition then that will be skipped as well.

You can prove it to yourself by going to Developer Tools > Services and executing this in broad daylight (and the automation’s action will be executed regardless of the fact the sun is not below the horizon):

Yes, you are perfectly right.
But I linked the automation to a script and a boolean input.
In short, I want the terrace two lamps to turn on (random) automatically after sunset and until just before dawn. With the boolean input I can disable everything and turn on the lamps manually.

FWIW, this combination of triggers ensures the action is executed when the sun’s elevation is below -3 degrees, even when Home Assistant restarts and the sun is already below -3 degrees.

- alias: Example
  trigger:
    - platform: homeassistant
      event: start
    - platform: template
      value_template: "{{ state_attr('sun.sun', 'elevation') < -3 }}"
  condition:
    - condition: template
      value_template: "{{ state_attr('sun.sun', 'elevation') < -3 }}"
  action:
 # your action goes here
1 Like

Perfect, thanks so much!
:smile:

1 Like

You’re welcome! Glad to hear it now works the way you want.

Please consider marking my post (above) with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has an accepted solution. It will also place a link below your first post that leads to the Solution. All of this helps users find answers to similar questions.

can you do something like this but with a template switch? so i can re-run the turn_on/off actions at reboot?