Turn off the light an hour after sunrise

Hi. Tell me how to turn off the light an hour after sunrise and turn it on an hour before sunset?
sun Text Sensor has the number I need but how to make minus one hour.
In the sun of automation there is only elevation. And it’s not clear how I can use it.
I do not understand.

Sun wiki

Sorry for my English. (translate.google)

You can easily do this with the Automations UI:
Here is a sample of the automation to switch off 1h after sunset


If you have a switch:

Or if you have it set up as a light / device:

You need to set two automations, one as above to turn off the light after sunset and another one with -01:00:00 sunrise offset, to do the actions you want before sunrise.

All in the docs: Automation Trigger - Home Assistant

1 Like

I can do that. It is important for me to do this in an autonomous system. Since there may be no connection to the server

Does it work standalone on ESPHome?

So you want to do it locally on ESPhome, you have set up the automation there but dont know how to add a delay? (or time offset)

Sorry I didn’t know you were talking about esphome.

Yes. I can turn on the lights at dawn and turn off the lights at sunset, but I cannot do this an hour after sunrise and an hour before sunset.

  • elevation: 5° it does not suit me.
sun:
  latitude: 59.235372°
  longitude: 20.802865°
  on_sunrise:
    - elevation: 5°
      then:
        - switch.turn_off: light
  on_sunset:
    - elevation: -5°
      then:
         - switch.turn_on: light

You can add a delay for the after sunset, but not sure how to do the before sunset

Actually, looking at it more, to use the sun feature in esphome it requires a time source so would need connection to a server

I’m just dealing with this myself. I didn’t play with delays yet, but if i understand correct then it goes like this: for, say, one hour BEFORE sunset you have to enter positive elevation, while for delay one hour AFTER sunset you have to enter negative elevation.
I didn’ find any information how to user hours or minutes, though. But you can observe current elevation in degrees and define elevation to use from this (there’s an example for showing current elevation on esphome sun page). But i’m not sure if this elevation angle stays the same the whole year -i think it changes with summer/winter time…

Could you use the esphome sun text sensor to get the time of the next sunset and subtract an hour and use that as a trigger ?

The earth turns 360deg in 24 hours or 0.25deg per minute.

You want to try the ESPHome discord, they are very helpfull and this sounds like something they would know. There is a time component in there (text_sensor) that gives the sunset and sunrise exact time to HA. If you could acces that info it would be simple to subtract or add a hour to that in an if statement.

That’s true, but the fact is that sun is higher on the sky in the summer than in the winter, so i think elevation is different, because it’s calculated regarding to your location. THIS calculator shows quite differnt maximum daily elevation now and in the April or May.
Currently in my location appr. 0.7 degrees means around 5 minutes.

OK take a look at that chart you pointed to, or to this web page https://www.suncalc.org/

You will find that no matter what location you choose outside the Arctic circles the elevation an hour after sunset only varies by about 2 degrees, in the case of the default suncalc position of Madrid, it varies between about 10 and 12 degrees. So if you set the lights to go on when the sun is at elevation of 11 degrees, you will be very close. It won’t be seconds (or even minutes) accurate, but it will be close.

You see the same at the beginning of the day.

sun:
  latitude: 59.235372°
  longitude: 20.802865°
  on_sunrise:
    - elevation: 5°
      then:
        - switch.turn_off: light
  on_sunset:
    - elevation: -5°
      then:
         - delay: 1h
         - switch.turn_on: light

use delay

The question I think is whether you you can define something to happen an hour before sunrise.

a text sensor and, based on it, make an automation esphome.

You’re probably right. I did some test calculation on that page i noticed that now (autmn) highest sun is appr. 40deg, while in high summer it will be at good 60 deg. But, as you said, at morning and evening there shouldn’t be much difference. I really don’t care much if lights start to turn on somewhat early and stop a bit later when it’s already day, so elevation is more than enough accurate.

@lordzid: that is an option, sure, but not if you want to make HA indepentent automation. I tried with HA automations first and it works perfectly, but at the end i rather decided to control light from esphome module only. Just in case if server goes down….etc… lights will still turn on at movement. Although in appr. one year of running HA i didn’t experience problem yet. Knocking on wood….