After sunrise and before sunset condition failed

I have created an automation to open my blind in the bedroom with several conditions (between 7am and 10pm, between sunrise and and sunset, and bed not occupied for 10 minutes).

Today it failed, but I don’t understand why. The trigger was 7am in this case and it failed at the condition for sunrise/sunset.

This is the condition I have configured:

condition: sun
after: sunrise
before: sunset

In the trace log for this execution I see that it evaluated to false, but I can’t understand why, since 7am should be between those values.

trace log:

Executed: 21. November 2021, 07:00:00
Result:
wanted_time_before: '2021-11-21T15:19:19.803585+00:00'
wanted_time_after: '2021-11-21T06:46:39.006507+00:00'
result: false

What time zone did you configure for Home Assistant?

These times are in UTC + 0:

wanted_time_before: '2021-11-21T15:19:19.803585+00:00'
wanted_time_after: '2021-11-21T06:46:39.006507+00:00'

But I’m guessing this is local time:

Executed: 21. November 2021, 07:00:00

Thanks, that’s the issue. I’ve checked and the sunrise trigger started it again at 7:46:39 local time and this time it also passed the condition.

@Shortie I had the same problem but recently solved it. I actually had to edit this post to correct something I previously thought. The short answer is that the “executed” time is displayed in your selected timezone but converted to UTC time when checked against the “wanted time” in the condition. So when trying to use the “trace” function to check it everything seems wrong.

The long answer is, depending on your timezone, the “executed” time could appear to be within the constraints of the “wanted time” but show false. Since I’m in CST, I originally thought I had to use an offset of -06:00:00. When I did that, the “executed” time and “wanted time” appear to be correct but the condition showed false. Then when I removed the offset the same condition appeared to be false but showed true. After trial and error, I realized that for it to make sense when viewing it in the “trace” and possibly other places that you might be seeing it in your time zone or UTC but that on the backend your timezone is changed to UTC when checking it in a condition. The offset would only be used if you want to change the condition to an earlier or later time before or after sunrise or sunset. Hopefully, I did not confuse anyone.

Now the way that I set up my choose conditions was in the order of how I wanted them checked within the same 00:00:00 to 24:59:59 same day time period. For example, sunset does not pass midnight. So when setting up the choose statement always think of it being within the constraints of a single day.

In my example, you can see that the first condition requires it to be both after sunrise AND before sunset for it to be true. When triggered while the sun is up both statements are true and the light turns on to a set kelvin temperature. The second condition handles the rest of the day which is before sunrise OR after sunset and when true the light turns on to a different kelvin temperature.

Hopefully, I explained this right, and hopefully, it helps.

        sequence:
          - choose:
              - conditions:
                  - condition: and
                    conditions:
                      - condition: sun
                        after: sunrise
                        after_offset: '00:00:00'
                      - condition: sun
                        before: sunset
                        before_offset: '00:00:00'
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: '{{ light }}'
                    data:
                      kelvin: 4500
              - conditions:
                  - condition: or
                    conditions:
                      - condition: sun
                        after: sunset
                        after_offset: '00:00:00'
                      - condition: sun
                        before: sunrise
                        before_offset: '00:00:00'
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: '{{ light }}'
                    data:
                      kelvin: 3500
            default: []