Sunset based conditions

Greetings
Trying to make an automation, which would turn off hallway light. Here is what I want:
2 hours before and after sunset i need the lights to turn off after motion sensor getting of in 45 minutes
other time I need 5 minutes delay

Now I can not understand how to make 2 hours before and after sunset and all other time

Here is what I have now:

- id: '1503948671309'
  alias: Main coridoor off
  trigger:
  - entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      minutes: 5
    platform: state
    to: 'off'
  condition:
  - after: sunset
    after_offset: ?:?:?
    before: sunset
    before_offset: -?:?:?
    condition: sun
  action:
  - entity_id: switch.a12_key
    service: switch.turn_off

- id: '1547529472251'
  alias: Main coridoor off (night)
  trigger:
  - entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      minutes: 45
    platform: state
    to: 'off'
  condition:
  - after: sunset
    after_offset: 02:00:00
    before: sunset
    before_offset: -02:00:00
    condition: sun
  action:
  - entity_id: switch.a12_key
    service: switch.turn_off

As you can see, I have set a condition on “night” one, but it seems not to work

after means everything after that event (sunset) will be true. before means everything before that event (sunset) will be true. the offsets just shift the time, they do not change the true/false value. So when you use after and after_offset. It moves the sunset time by the ammount of the offset. So, you use 2 hours, everything after the sunset time + 2 hours will be true. If you use -2 hours, everything after sunset - 2 hours will be true. That is the scenario you want. All of this is covered in the truth table shown in the documentation:

- id: '1547529472251'
  alias: Main coridoor off (night)
  trigger:
  - entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      minutes: 45
    platform: state
    to: 'off'
  condition:
  - after: sunset #eveything after this time returns true.
    after_offset: -02:00:00 # we are moving the time 2 hours earlier.
    condition: sun
  action:
  - entity_id: switch.a12_key
    service: switch.turn_off

May be I was not really clear.
Here is a “clock”

But I did understand you, I think. Here is how I try now:

- id: '1503948671309'
  alias: Main coridoor off
  trigger:
  - entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      minutes: 5
    platform: state
    to: 'off'
  condition:
  - after: sunset
    after_offset: 02:00:01
    before: sunset
    before_offset: -02:00:01
    condition: sun
  action:
  - entity_id: switch.a12_key
    service: switch.turn_off

- id: '1547529472251'
  alias: Main coridoor off (night)
  trigger:
  - entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      minutes: 45
    platform: state
    to: 'off'
  condition:
  - after: sunset
    after_offset: -02:00:00 
    before: sunset
    before_offset: 02:00:00 
    condition: sun
  action:
  - entity_id: switch.a12_key
    service: switch.turn_off

Ok, I would expect this to work if you had 2 conditions anded together instead of placing them in a single condition. ALso, you need quotes around all times

  condition:
  - condition: sun
    after: sunset
    after_offset: "-02:00:00"
  - condition: sun
    before: sunset
    before_offset: "02:00:00"

conditions listed in this fashion are assumed to be anded together. You don’t need to specifiy condition: and.

EDIT: The top one needs to be or. The bottom automation needs to be and. This should work.

- id: '1503948671309'
  alias: Main coridoor off
  trigger:
  - entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      minutes: 5
    platform: state
    to: 'off'
  condition:
  - condition: or
    conditions:
    - condition: sun
      after: sunset
      after_offset: '02:00:01'
    - condition: sun
      before: sunset
      before_offset: '-02:00:01'
    
  action:
  - entity_id: switch.a12_key
    service: switch.turn_off

- id: '1547529472251'
  alias: Main coridoor off (night)
  trigger:
  - entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
    for:
      minutes: 45
    platform: state
    to: 'off'
  condition:
  - condition: sun
    after: sunset
    after_offset: '-02:00:00'
  - condition: sun
    before: sunset
    before_offset: '02:00:00' 
    
  action:
  - entity_id: switch.a12_key
    service: switch.turn_off
2 Likes

Thank you, will read more and try it out!