Turn on the light if off and if 45 before sunset

Hi all,

I’m trying to setup a simple basic automation but I can’t understand where’s the error:

automation:
- alias: Accendi Luce Corridoio se rilevato movimento
  trigger:
- platform: state
  entity_id: binary_sensor.motion_sensor_158d00015adc7c
  to: 'on'
- event: sunset
  offset: -00:45:00
  platform: sun  
# - platform: sun
  # event: sunset
  # offset: "-01:00:00"
  condition:
condition: and
conditions:
- condition: state
  entity_id: light.corridoio
  state: 'off'
  action:
- service: homeassistant.turn_on
  entity_id: light.corridoio

as you can see I’ve tried different solution but the condition doesn’t work and the light turns on even during the day.

Can someone please support me?

Thank you

you’re spacing is kind of incorrect, and trigger sun and event are wrong, try this:

automation:
  - alias: Accendi Luce Corridoio se rilevato movimento
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d00015adc7c
        to: 'on'
      - platform: sun
        event: sunset
        offset: -00:45:00
    condition:
      condition: template
      value_template: >
        {{is_state('light.corridoio','off')}}
    action:
      service: homeassistant.turn_on
      entity_id: light.corridoio

New version, still get triggered also with the sun above the horizon:

  - alias: Accendi Luce Corridoio se rilevato movimento
    trigger:
      - platform: state
        entity_id: binary_sensor.motion_sensor_158d00015adc7c
        to: 'on'
      - platform: sun
        event: sunset
        offset: -00:45:00
    condition:
      condition: template
      value_template: >
        {{is_state('light.corridoio','off')}}
    action:
      service: homeassistant.turn_on
      entity_id: light.corridoio

You need to AND the triggers as the default is OR. You could also add the sunset trigger as a condition.

automation:
  - alias: Accendi Luce Corridoio se rilevato movimento
    trigger:
    - platform: state
      entity_id: binary_sensor.motion_sensor_158d00015adc7c
      to: 'on'
    condition:
    - condition: sun
      after: sunset
      after_offset: '00:45:00'
    - condition: state
      entity_id: 'light.corridoio'
      state: 'off'
    action:
      service: homeassistant.turn_on
      entity_id: light.corridoio

That would do the job.
As I understand it, you want the light to turn on when motion is detected but only if it is after sunset, right? I think you would want to turn it off after 5-10 minutes.

I am editing this because of the offset. If you want it AFTER sunset then then you should use the time without the dash. Using the dash (minus) means that you want it 45minutes BEFORE the sunset. So the correct one should be:

after_offset: '00:45:00'
1 Like

Yes you’re right: i wan to turn o AFTER sunset.

Thank you for your support!