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}}
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.
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.
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.
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.