Getting confused with reacting to sunset

Hi all,

I use a motion sensor to switch on a light, but this should happen corresponding to the environment and the time. So during the evening/morning hours, it should turn on at 100% and during the night the brightness is reduced.
So it has been set to start at 20:00h for a while, but during summer I had to adjust the starting time like two or three times, simply because the sun was still there and switching on the lights was not needed.

So basically, I just wanted to have the motion sensor to turn on the light between 30 mins before the sun sets until 2:00h during the night.
In order to get that, I removed the condition time 20h-02h and just entered the condition sun. But this did not work, because I guess the condition was not met. It was not always 30 mins before sunset.

  action:
  - choose:
    - conditions:
      - condition: time
        after: '20:00'
        before: 02:00
      - condition: sun
        before: sunset
        before_offset: 00:30:00

What do I miss? Can I specify a negative offset like:

      - condition: sun
        after: sunset
        before_offset: 00:30:00

That would mean, focus on the time after sunset, but start already 30mins before. Does it work or is there a better way to do this?

Thanks,
Fridolin

What is your trigger?
Please post the full automation.

I think the problem is that all you conditions are AND’d by default and can never all be true at the same time, you probably need to make the first condition an OR then list your individual conditions as follows:

  condition:
  - condition: or
    conditions:
    - condition: time
      after: 20:00:00
    - condition: time
      before: 02:00:00      
    - condition: sun
      before: sunset
      before_offset: 00:30:00

Hi and thank you for your comments. I guess I’ve described my issue is a misleading way. So, the complete automation looks like this:

- id: '1617041401373'
  alias: Hallway - Light on - Summer
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.0x00158d000450bc35_occupancy
    to: 'on'
  condition: []
  action:
  - choose:
    - conditions:
      - condition: time
        after: '20:00'
        before: 02:00
      sequence:
      - service: homeassistant.turn_on
        data:
          transition: 2
          brightness_pct: 100
          entity_id: light.flurlicht
      - service: notify.tplogger
        data:
          message: '{{now().timestamp() | timestamp_custom("%Y-%m-%d %H:%M:%S")}}
            -- Hallway: Light on - {{states(''sensor.0x00158d000450bc35_illuminance_lux'')}}
            - lx between 20:00 and 2:00'
          title: Hallway says
    - conditions:
      - condition: time
        after: 02:00
        before: 05:30
      sequence:
      - service: homeassistant.turn_on
        data:
          transition: 2
          brightness_pct: 15
          entity_id: light.flurlicht
      - service: notify.tplogger
        data:
          message: '{{now().timestamp() | timestamp_custom("%Y-%m-%d %H:%M:%S")}}
            -- {{now().timestamp() | timestamp_custom("%Y-%m-%d %H:%M:%S")}} -- Hallway: Light on - {{states(''sensor.0x00158d000450bc35_illuminance_lux'')}}
            - lx in the middle of the night'
          title: Hallway says
    - conditions:
      - condition: time
        after: 05:30
        before: 09:00
      - condition: numeric_state
        entity_id: sensor.0x00158d000450bc35_illuminance_lux
        below: '5'
      sequence:
      - service: homeassistant.turn_on
        data:
          transition: 2
          brightness_pct: 100
          entity_id: light.flurlicht
      - service: notify.tplogger
        data:
          message: '{{now().timestamp() | timestamp_custom("%Y-%m-%d %H:%M:%S")}}
            -- {{now().timestamp() | timestamp_custom("%Y-%m-%d %H:%M:%S")}} -- Hallway:
            Light on - {{states(''sensor.0x00158d000450bc35_illuminance_lux'')}}
            - lx (below 5) before 9:00h'
          title: Hallway says
    default:
    - service: homeassistant.turn_off
      entity_id: light.flurlicht
      data:
        transition: 2
  mode: single

The trigger is the motion sensor, but the action should consider all circumstances.

So, I’d like to replace:

      - condition: time
        after: '20:00'
        before: 02:00

with something adapting itself dynamically like:

      - condition: sun
        after: sunset
        before_offset: 00:30:00

This is just to avoid modifying the automation every two weeks, because the sunset is sometimes at 5h or at 10h in the evening.

So if the motion sensor is triggered, it should switch the light on between 30 mins before the sun sets until 2h in the night. Best would be if the motion sensor would be more sensitive concerning the currently available light, but it isn’t. So I need to somehow do this manually.

You actually need

      - condition: sun
        after: sunset
        after_offset: "-00:30:00"

Also, see below for the notice regarding after/before. if you specify only “after”, the condition will only be met until midnight.

1 Like

Thanks a lot. This documentation is what I was looking for. I’ll change my automation accordingly and we’ll see later today, whether it worked out nor not.

For future reference, this:

{{now().timestamp() | timestamp_custom("%Y-%m-%d %H:%M:%S")}}

can be reduced to this:

{{now().timestamp() | timestamp_local}}
1 Like

Until now, it worked nicely. I changed it like this:

  condition: []
  action:
  - choose:
    - conditions:
      - condition: or
        conditions:
          - condition: time
            after: '23:00'
            before: 02:00 
          - condition: sun
            after: sunset
            after_offset: "-00:30:00"     
      sequence:
      - service: homeassistant.turn_on
      ...

The aspect of the new day is covered with the second (or here first) or condition.

Additionally I’ve adapted another one and used this as trigger:

  trigger:
  - platform: sun
    event: sunset
    offset: "-00:30:00"

Worked like a charm …so thank you again.

@123 Thanks - those hints are always welcome