Value template doesn’t trigger automation

I created an automation to turn on the lights at sunset +15.
However when the weather conditions are bad the lights should turn on earlier. For that I created an additional template trigger.

It should trigger in a time frame from 15 minutes before sunset and when it is cloudy or raining and my indoor light sensor is below 2lx.

The sunset trigger works fine. The template trigger evaluated to true when the conditions are met, but doesn’t trigger the automation.

trigger:  
- platform: sun    
  event: sunset    
  offset: "15"
- platform: template    
  value_template: >-         
     {{((((as_timestamp(states('sensor.sun_next_setting')) -         
     as_timestamp(now())) /60) | int) < 15) and      
     (states('sensor.buienradar_condition') == 'cloudy' or      
     states('sensor.buienradar_condition') == 'rainy') and      
     ((states('sensor.esp_woonkamer_lichtsterkte') | int) < 2)}}

Are you sure…? According to the docs, the value for offset “needs to be specified in a hh:mm:ss format.” By having it in the wrong format you are likely causing the automation to not be loaded properly (there should be a error in your logs).

Also, the sun_next_setting sensor changes at sunset, so for 15 minutes after sunset, the other conditions may be met, but the template as a whole will give an error.

{% set setting = today_at((states('sensor.sun_next_setting')
|as_datetime|as_local).time()) %}
{{ (now() + timedelta(minutes = 15) >= setting
or  setting <= now() <= setting + timedelta(minutes = 15)) and
states('sensor.buienradar_condition') in ['cloudy', 'rainy'] and      
states('sensor.esp_woonkamer_lichtsterkte') | int < 2}}

I am definitly shure the plain sunset trigger is working.

But by the way: I will try out your suggested template and will let you know if that solves my bad weather early lights on challenge.

Tried out your solution, but also doesn’t trigger the automation. It renders true at the given time and condition, but no trigger. Like my original template.

I have tested the template trigger posted above and it works in my instance.

I have to repeat the fact that a misconfigured Sun trigger may cause the entire automation to not load properly. Try the Template trigger solution I posted along with the Time trigger using an offset in HH:MM:SS format as described in the documentation.

Very strange. I tested your template with the Sun trigger disabled and it didn’t work.

What version of HA do you use? I am running HA2023.4.6 in a containered core implementation on a RPI4.

I used other template triggers, but they are event based and based on a state trigger and they run fine.

I wonder what’s wrong here?

core 2023.5.2
HAOS 10.1
Supervisor 2023.04.1

Set up a test just on the Sun-based part of the template for tomorrow… I’d go ahead a do it for sunrise too so you have twice the chances to find issues.

trigger:
  - platform: template
    value_template: >
      {% set rising = today_at( (states('sensor.sun_next_rising')
      | as_datetime | as_local).time() ) %}
      {{ (now() + timedelta(minutes = 15) >= rising
      or rising <= now() <= rising + timedelta(minutes = 15)) }}
  - platform: template
    value_template: >
      {% set setting = today_at( (states('sensor.sun_next_setting')
      | as_datetime | as_local).time() ) %}
      {{ (now() + timedelta(minutes = 15) >= setting
      or setting <= now() <= setting + timedelta(minutes = 15)) }}
condition: []
action:
  - service: persistent_notification.create
    data:
      title: Testing Sun-based Template trigger
      message: >
        Tijd: {{ now() }} <br> 
        Weer: {{ states('sensor.buienradar_condition') }} <br>
        Lichtsterkte: {{ states('sensor.esp_woonkamer_lichtsterkte')}}

Very good. I implemented the rule in my system, and report the result back. Fingers crossed.

alias: example
trigger:  
  - platform: sun    
    event: sunset    
    offset: "15"
    variables:
      is_true: true
  - platform: sun    
    event: sunset    
    offset: "-15"
    variables:
      is_true: "{{ states('sensor.buienradar_condition') in ['cloudy', 'rainy'] and states('sensor.esp_woonkamer_lichtsterkte') | int(0) < 2 }}"
condition:
  - "{{ is_true }}"
action:
  - service: light.turn_on
    target:
      entity_id:
        - light.your_light1
        - light.your_light2

@123 Thank you for your suggestion. I’ll test that also.

@Didgeridrew and @123 , after some comprehensive testing the trigger finally works.

The result:

trigger:  
  - platform: sun    
    event: sunset    
    offset: "15" 
  - platform: template    
    value_template: >   
      {% set setting = today_at( (states('sensor.sun_next_setting') |      as_datetime | as_local).time() ) %}
      {{ now() + timedelta(minutes = 15) >=   setting }}

works fine. It triggers 15 min after sunset and when the conditions in the value template are true, then is triggers max 15 minutes earlier then sunset.

Al other suggestions resulting in either not trigger the automation or in a trigger time to late.

Thank you all for your corporation.

I find that hard to believe. The example I posted triggers 15 minutes before and 15 minutes after sunset. The two Sunset Triggers are guaranteed to trigger at the scheduled times. Whether they execute the action depends on if they fulfill the condition.

  • The “15 minutes after sunset” trigger always passes the condition so it’s guaranteed to execute the action.

  • The “15 minutes before sunset” trigger will execute the action only if it’s cloudy/rainy and light level is less than 2.

If that’s not how the example worked for you then there may be something different with your version of it. If you used the Visual Automation Editor to compose it, then the editor has a habit of changing a boolean true into a string (thereby breaking the condition’s logic). One way to circumvent the Editor’s quirk, is to replace is_true: true with `is_true: ‘{{ true }}’ so it doesn’t convert the boolean value into it a string.

Anyway, moot point now; you’ve chosen a different solution.