Sunset for time-condition

Hello folks,

I’d like to have the sunset-relation-time used for the “before”-tag in a time-condition and don’t know how to do it.

Can somebody pleas help?

Thanks a lot

Did you mean this?

condition:
  - condition: sun
    before: sunset
    before_offset: '00:45:00'

The formatting is quite similar to this-

No, I want an automation between 11:00 and sunset.

      - condition: time
        after: '11:00:00'
        before: '18:00:00'

something like

      - condition: time
        after: '11:00:00'
        before: '18:00:00'  <-- 'sunset'

Separate it like below. By default, conditions are AND. So both must be satisfied in order for an automation to proceed.

condition:
  - condition: sun
    before: sunset
  - condition: time
    after: '11:00:00'

should it be that easy?

I’m afarid this doesn’t work

Here ar my conditions:

condition: and
conditions:
  - condition: numeric_state
    entity_id: weather.home
    attribute: wind_speed
    below: '25'
  - condition: numeric_state
    entity_id: weather.home
    attribute: temperature
    above: '20'
  - condition: time
    after: '11:00:00'
  - condition: sun
    before: sunset
    before_offset: '-00:30:00'

An here is the result:

Executed: 2. September 2021, 17:47:37
Result:
result: true
conditions/0
Executed: 2. September 2021, 17:47:37
Result:
result: true
conditions/0/entity_id/0
Executed: 2. September 2021, 17:47:37
Result:
result: true
state: 13.3
conditions/1
Executed: 2. September 2021, 17:47:37
Result:
result: true
conditions/1/entity_id/0
Executed: 2. September 2021, 17:47:37
Result:
result: true
state: 23.6
conditions/2
Executed: 2. September 2021, 17:47:37
Result:
after:
  __type: <class 'datetime.time'>
  repr: datetime.time(11, 0)
now_time:
  __type: <class 'datetime.time'>
  repr: datetime.time(17, 47, 37, 474379)
before:
  __type: <class 'datetime.time'>
  repr: datetime.time(23, 59, 59, 999999)
result: true
conditions/3
Executed: 2. September 2021, 17:47:37
Result:
wanted_time_before: '2021-09-02T17:37:26.370657+00:00'
result: true

Please have a look at condition 3
The ‘wanted time before’ is 10 minutes earlier than the executiontime and the result is still true…

1 Like

This is reporting your local time:

 Executed: 2. September 2021, 17:47:37

This is reporting UTC time:

wanted_time_before: '2021-09-02T17:37:26.370657+00:00'

What is the offset between your local timezone and UTC?

Apply that offset to the UTC time and it should explain why the Sun Condition evaluated to true.

@Baujahr70 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: []

If that’s happening to you then your system clock/timezone isn’t configured correctly.

Based on observations over the past three years on this forum, the majority of issues experiencing a time offset issue were due to incorrect system clock/timezone configuration.

All time-based triggers in Home Assistant are based on UTC time adjusted for your local timezone (and DST). Period.

@123 I actually just edited my reply. My timezone was correct but when I looked at the trace of the automation or script it appeared to be wrong. Here’s what I edited my post to which I think is a correct explanation.

When viewing the trace of an automation or script 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.

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: []

@123 I have not tried the condition as you suggested but yes that would be a lot easier than the way that myself and everyone else are attempting to do it. Using before sunset, after sunset, before sunrise, and after sunrise can lead to many results and it definitely overcomplicates everything. Using below_horizon or above_horizon as I did below instead of the way I had it definitely makes it a lot easier to understand and to use.

I have a question, would below_horizon and above_horizon be considered next_dawn, next_dusk, next_midnight, next_noon, next_rising, next_setting, or none of them.? Is there an exact time associated with it.? Also, could it be offset sunrise and sunset can be?

          - choose:
              - conditions:
                  - condition: state
                    entity_id: sun.sun
                    state: above_horizon
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: '{{ light }}'
                    data:
                      kelvin: 4500
              - conditions:
                  - condition: state
                    entity_id: sun.sun
                    state: below_horizon
                sequence:
                  - service: light.turn_on
                    target:
                      entity_id: '{{ light }}'
                    data:
                      kelvin: 3500

I don’t recall making any suggestions for a condition in this thread.

I was reading multiple posts and I think on another post you suggested using above and below horizon instead as an easier way to accomplish this. That’s what I was referring to and then I misworded what I wrote. It was a long day.

Is this the correct format if I want these two lights to go ON at 30 mins before Sunset and go OFF at 10 pm ?

I KNOW that I am using the wrong format code even though I used ~ in front and at the end of the code. I would appreciate guidance on how to add code correctly in this forum.
~
~alias: “#SUNBLUE
description: “”
trigger:

  • platform: sun
    event: sunset
    offset: “-00:30:00”
    condition:
  • condition: state
    entity_id: calendar.18_trips
    state: “off”
    action:
  • service: light.turn_on
    data:
    color_name: blue
    target:
    entity_id:
    - light.ha_salon_lamp_1_kasa_2
    - light.ha_salon_lamp_2_kasa
  • service: light.turn_off
    data: {}
    target:
    entity_id:
    - light.ha_salon_lamp_1_kasa_2
    - light.ha_salon_lamp_2_kasa
  • wait_for_trigger:
    • platform: time
      at: “22:00:00”
      mode: single~
      ~

Why do you think ~ would do it? Either use the </> button or surround the code block with three backticks (```).

Your code looks like it turns them on then immediately off again as the wait_for_trigger is at the end. Try this (assuming your action works):

alias: Salon lamp manager
id: 941299a1-d523-458b-9bc8-2e74f038f8a9
trigger:
  - platform: sun
    event: sunset
    offset: "-00:30:00"
  - platform: time
    at: "22:00:00"
condition:
  - condition: state
    entity_id: calendar.18_trips
    state: off
action:
  - service: light.turn_{{ 'on' if trigger.platform == 'sun' else 'off' }}
    data:
      color_name: blue
    target:
      entity_id:
        - light.ha_salon_lamp_1_kasa_2
        - light.ha_salon_lamp_2_kasa

If sunset is ever at 21:30 or later, weird things might happen.

It’s good practice to avoid having long waits in automations, like your prior attempt was trying to do. My version triggers off either thing that you care about, does its job and finishes straight away.

I’ve given it a random unique id (from here) so that you can use the Trace feature — remove that line if you’re editing via the UI.

1 Like

Thanks !!! I am going to try this out tonight. I see what I did wrong copying the code. :grinning: