Please check my template code

I would like to write an automation to turn on a switch between 18:30 and 19:45; but if sunset is later than 18:30, at sunset but still by 19:45. Is the following correct:

triggers:
  - trigger: template
    value_template: "{{ sun.sunset and now() > today_at('18:30') }}"
  - trigger: time
    at: "19:45:00"

If not, would someone please show me the proper code. Note that it doesn’t matter if the switch gets turned on twice.Thanks.

Try:

{{ states("sun.sun") == "below_horizon" and now() > today_at('18:30') }}

Just to clarify… the actions should be executed:

  • At sunset, if sunset is between 18:30 and 19:45.
  • At 18:30 if sunset is earlier than that, i.e. the sun is already below the horizon.
  • At 19:45 if sunset if later than that, i.e. the sun is still above the horizon.
triggers:
  - trigger: sun
    event: sunset
  - trigger: time
    at: "19:45:00"
    id: above
  - trigger: time
    at: "18:30:00"
    id: below
conditions:
  - condition: template
    value_template: |
      {% if trigger.platform == 'sun' %}
        {{ today_at('18:30') <= now() <= today_at('19:45') }}
      {% else %}
        {{ is_state('sun.sun', trigger.id~'_horizon') }}
      {% endif %}
actions:
....

Here is the yaml of my automation that is not working. It should trigger, these days, at 6:30 pm, but the light does not turn on.

 alias: Kitchen Sink Light On
 triggers:
   - trigger: template
     value_template: "{{ sun.sunset and now() > today_at('18:30') }}"
   - trigger: time
     at: "19:40:00"
 conditions: []
 actions:
   - type: turn_on
     domain: switch

It was suggested that the first value template be replaced with this:

"{{ states("sun.sun") == "below_horizon" and now() > today_at('18:30') }}"

I tried this, however, when I click the Save button, I am informed:

Error in parsing YAML: bad indentation of a mapping entry (line: 5, column: 33)

which seems to be the beginning of sun.sun

One thing I noticed while looking at various postings, etc., is that some of them are formatted as

trigger:
  - alias: <some name>
    platform: template
    value_template: >- 
      {<template>}

Does this format make a difference?
Does using one or two curly brackets matter?

I have also see this:

trigger:
  - platform: sun
    event: sunrise

but have no idea how to combine this format with the now() in a template.

Please offer some guidance.

Change your double quotes inside your template to single quotes:

"{{ states('sun.sun') == 'below_horizon' and now() > today_at('18:30') }}"

When in doubt, check the docs: https://www.home-assistant.io/docs/automation/trigger/#template-trigger

FWIW, I wouldn’t use this Template Trigger (even after its syntax error is corrected) because it’s less efficient than Didgeridrew’s suggestion.

Your template contains now() and today_at() so Home Assistant will evaluate the template every minute (because the values of now() and today_at() change at 1-minute intervals when used in templates). In contrast, Didgeridrew’s version is evaluated just three times a day (at sunset and at two scheduled times).

1 Like

That worked - thanks!