I made an automation that is supposed to keep my blinds closed at night and also while a timer is running and open otherwise:
trigger:
- platform: sun
event: sunrise
offset: 0
- platform: sun
event: sunset
offset: 0
- platform: state
entity_id:
- timer.blinds_timer
from: idle
to: active
- platform: state
entity_id:
- timer.blinds_timer
from: active
to: idle
action:
- service: cover.set_cover_position
metadata: {}
data:
position: >-
{{ 0 if (is_state('sun.sun', 'below_horizon') or
is_state('timer.blinds_timer', 'active')) else 100 }}
target:
entity_id: cover.blinds_living_room
But instead of closing the blinds at sunset, it opens them, and indeed in the trace I can see that the position evaluated to 100 instead of the expected 0:
That seems to indicate that is_state('sun.sun', 'below_horizon') was false when the automation was triggered.
If I evaluate the position template now (during the night) it will evaluate to 0, just as expected:
supposed to keep my blinds closed at night and also while a timer is running
But your image in the template editor says an OR
It seems like you want an OR based on your post but not sure what the timer is for exactly. What does the history log say for the sun the and timer at the time?
I got off HA native automations long time ago and moved to NodeRed before they added the traces …et al.
How does this trace till you it was the sun vs the timer that failed the OR, cause it could have had to been either/both?
That’s just the difference between English and code. I want the blinds closed when the sun is down and I also want the blinds closed when the timer is running. In code that’s “if sun down or timer running then closed”.
How does this trace till you it was the sun vs the timer that failed the OR, cause it could have had to been either/both?
If is_state('sun.sun', 'below_horizon') had worked as expected (i.e. been true), the position would have been 0, no matter what the timer value is. So no, it could not have been the timer value that made this fail.
[2024-04-23 20:01:00 +0200]
triggered by time_pattern
not below_horizon
elevation = 5.77
[2024-04-23 20:43:33 +0200]
triggered by sunset event
not below_horizon
elevation = -0.36
[2024-04-23 21:01:00 +0200]
triggered by time_pattern
below_horizon
elevation = -2.97
When the sunset event fires, the elevation is -0.36, which is not low enough (it would have to be < -0.833) to be considered “below_horizon”.
I’m not sure how the “sunset” event is triggered, I think this is the code that calculates the exact time of the next sunset event, but I don’t know enough Python to understand it. (What does _AstralSunEventCallable do?)
You should just change the automation triggers (above) to:
- platform: state
entity_id: sun.sun
You’re testing the state in the actions, so you should trigger on when the state actually changes, not something that should indicate when the state changes (but actually doesn’t.)