Sunset, Sunrise condiiton

Hello!
I have a problem with sun as condition. I just want to make a automation where a light will be turned on 1 hour before sunrise and one hour after sunset. For example if sunrise is on 7:00 and sunset is on 15:00 i Would like to turn on light between 6:00 and 16:00. Could you post me a solution?

Thanks you in advance

1 Like
automation:
  trigger:
    - platform: sun
      event: sunset
      offset: '+01:00:00'
    - platform: sun
      event: sunrise
      offset: '-01:00:00'
  action:
    service: light.toggle
    entity_id: YOUR _LIGHT

here you go. Lights on an hour before sunrise and lights off an hour after sunset. Set your action to use your lights/devices.

- alias: Lights on before Sunrise
  trigger:
    platform: sun
    event: sunrise
    offset: '-01:00:00'
  action:
    - service: scene.turn_on
      entity_id: scene.morning_sunrise

- alias: Lights off after sunset
  trigger:
    platform: sun
    event: sunset
    offset: '+01:00:00'
  action:
    - service: scene.turn_on
      entity_id: scene.morning_sunrise

edit: offsets were backwards

I think offsets should be opposite …

i thought they were… they’re corrected now

I would like to have it as condition, not as trigger, i just want to use it in existing automation :slight_smile: Can u make it as condition? I have sth like this actually, First automation: if 2 movement sensor are on-> turn light on, and second automation: If for 1 minutes there is not a movement on 2 movement sensor turn off the lights. And now i want First automation like this: if 2 movement sensor are on and it is 1h before sunset and 1h after sunrise (i mean it is dark outside) turn light on. Dont change second automation.

Off topic (TS wanted a condition, not a automation):

As an automation your solution is not sufficient! Always use two seperate automations. If, for some reason, your lights are off before sunrise, this automation will turn them on when the sun rises. @forsquirel’s solution is the way to do this right.

OT - it was a basic example for the OP to work from as he didn’t provide any code himself. That code would work fine in isolation and caters for exactly what the OP asked for…

Saying “always use two separate automations” is utter garbage. One automation with a templated action is the way to do this correctly with ‘error handling’.

But before the OP gets that far, he needs to understand what he’s doing, hence the basic-ness of my original reply. An easy way to tell if my replies are ‘this is as basic as it gets’ is when I just dump some code in with no explanations.

I did not put any code because I could not do it a few hours ago - i was outside my home :slight_smile:
Truth is i write a wrong info on topic so i need to correct that. I want to turn on light when its dark outside (that mean its 30 min before sunset to 30min after sunrise) but only when 2 motion sensor will be turned on. In my config i have condition that wont turn up if other light switch is on. Im uploading code of my automations.yaml beacuse it is not formatting well here. There is no any errors so trully i dont know how to make it right.

Secondly I dont care about a sunset/sunrise in switching off the light, I just wanna make it go off when movement is detected for 1min atleast. But it is not working too. Edit: Ok i fixed this :smiley:

Could you help me on my example?

automations.yaml (1.9 KB)

So the motion sensors are the triggers and the sun state is the condition eh?
https://community.home-assistant.io/t/trigger-between-sunset-and-sunrise/750/12?u=sholofly

But is this wrong? Should I change it for sun state as trigger, mtion sensors as conditions?

No, not wrong at all!
Sounds logical to me :slight_smile:

So thank you for all your help :slight_smile: I am testing this solution with “OR” instead “AND” in condition and its working well but i need more time to be 100% sure its all ok :slight_smile:

1 Like