Best approach for creating automations?

What is the best approach for an automation like “Keep plug on while sun is down” ?
Create two:

  1. When sun is set → turn on the plug
  2. When sun rise → turn of the plug

or create a single one like:

  1. When sun is set
    • turn on then plug
    • repeat until:
      • sunrise → turn off the plug

Is a long running automation with a repeat action type cause high cpu utilization and (maybe) overheat the raspberry? thanks!

There is no reason to use a repeat to continuously call the turn on service. Is something else turning the plug off repeatedly?

Create two or consolidate them into one … and by “one” I don’t mean what you suggested that (incorrectly) uses a repeat.

Hmm…probably I’ve express it in a wrong way…im looking to implement a “wait until” like:

  1. When sun is set
    turn on then plug
    wait until:
    sunrise → turn off the plug

I guess that “wait for template” action will to the trick with a template like :

{% if is_state(“sun.sun”, “above_horizon”) -%}
true
{%- endif %}

There’s no need for it to ‘wait until’.

alias: example
trigger:
  - id: 'off'
    platform: sun
    event: sunrise
  - id: 'on'
    platform: sun
    event: sunset
condition: []
action:
  - service: 'switch.turn_{{ trigger.id }}'
    target:
      entity_id: switch.whatever
1 Like

Very nice approach, never considered using the id’s as a parameter! Thanks a lot for expanding my automation horizons! :slight_smile:

For future reference, that example doesn’t wait for sun.sun to change state to above_horizon. It evaluates the template immediately and produces a result.

wait_template does wait for its template to evaluate to true. However, anything that causes an automation’s actions to wait (like delay, wait_template, wait_for_trigger, repeat, etc) is subject to being terminated if Home Assistant is restarted or Reload Automations is executed.

In other words, if an automation is busy doing something within its action section, it’s vulnerable to being cancelled by a restart/reload. That’s why it’s not a good practice to create an automation that spends many hours (or tens of minutes) within its action section.

1 Like

Got it…i’ve used similar templates on triggers but yes of course with this syntax will evaluate it immediately. In any case you need to be creative. Thanks mate!

1 Like

Any idea why i keep getting this rendering error? I’ve tried specifying the triggers and the service on quotes, no quotes, double quotes but always the same error.

Executed: September 3, 2022 at 21:42:19
Error: Template rendered invalid service: switch.turn_

alias: "PLUG: test"
description: ""
trigger:
  - id: off
    platform: sun
    event: sunrise
  - id: on
    platform: sun
    event: sunset
 action:
  - service: switch.turn_{{ trigger.id }}
    data: {}
    target:
      entity_id: switch.plugsonoff_1_on_off
mode: single

Missing quotes around the service … see the example above. Not:

switch.turn_{{ trigger.id }}

Instead:

'switch.turn_{{ trigger.id }}'

and likely the values for id also need quotes

In addition to kbrown’s answer, both on and off in your triggers should be in quotes as shown in 123’s original answer. Those terms have special meanings/functions so the quote marks are important.

1 Like

Add all the quotes shown in my example and don’t try testing the automation by clicking the Run Actions button. It only runs the actions therefore the trigger variable will be undefined (trigger.id will not exist).

I’m pretty sure that you don’t need the quotes around the service template unless the entire service is a template.

I have that same syntax in three automations and they all work fine with no errors.

that is probably the error.

Thank you all for your help…I’m getting familiarized! Is there any way to debug sun.sun triggers? Setting the sun state to “below_horizon” or “above_horizon” doesn’t trigger the automation.