ESPHOME and Sun Components

Hi.

I can to close a door 1h after the sunset like that:

sun:
  latitude: 43.5491
  longitude: 5.2811
  on_sunset:
    - then:
        - delay: 01:00:00
        - cover.close: 

How to do the same 1h before sunrise?

Thanks in advance.

I try

  on_sunrise:
    - then:
    # Optional time offset. This example will trigger 45 minutes before sunrise.
        - offset: "-00:45:00"
        - cover.open: 

but it doesn’t work :frowning:

I can’t find anything in the ESPHome documentation that suggests it can do what you want.

thanks, I did not find it either. I am a beginner in home automation and English is not my mother tongue so I thought I missed the solution

My solution.

substitutions:
  #Shift sunset and sunrisen on minute
  shift_sunset: '-22' 
  shift_sunrise: '17' 

time:
  - platform: sntp
    id: sntp_time
    timezone: Europe/Prague

sun:
  latitude: !secret latitude_data
  longitude: !secret longitude_data
  id: sun_sun
  
switch:
  - platform: gpio
    pin: D4
    name: "Relay"
    id: relay
    inverted: yes

text_sensor:
  - platform: template
    name: "Sunset"
    id: sunset_shift
    lambda: |-
      auto sunset = id(sun_sun).sunset();
      auto time = id(sntp_time).now();
      if (!sunset.has_value())
        return {"NaN"};
      int sunset_min = sunset.value().hour * 60 + sunset.value().minute + ${shift_sunset};
      int sunset_hour = int(sunset_min / 60);
      int sunset_minute = sunset_min % 60;
      ESP_LOGD("sunset_shift", "sunset.value.()hour is %i , sunset.value().minute is %i ", sunset.value().hour , sunset.value().minute);
      ESP_LOGD("sunset_shift", "sunset_min is %i , sunset_hour is %i ,sunset_minute is %i ", sunset_min , sunset_hour , sunset_minute);
      ESP_LOGD("sunset_shift", "state relay is %i  ", id(relay).state );
      if((sunset_hour == time.hour) and (sunset_minute == time.minute))
        if(!id(relay).state)
         id(relay).turn_on();
         ESP_LOGD("sunset_shift", "change relay is ON" );
      char buffer[6];
      sprintf(buffer,"%02i:%02i", sunset_hour , sunset_minute);
      return {buffer};
      
  - platform: template
    name: "Sunrise"
    id: sunrise_shift
    lambda: |-
      auto sunrise = id(sun_sun).sunrise();
      auto time = id(sntp_time).now();
      if (!sunrise.has_value())
        return {"NaN"};
      int sunrise_min = sunrise.value().hour * 60 + sunrise.value().minute + ${shift_sunrise} ;
      int sunrise_hour = int(sunrise_min / 60);
      int sunrise_minute = sunrise_min % 60;
      ESP_LOGD("sunrise_shift", "sunrise.value.()hour is %i , sunrise.value().minute is %i ", sunrise.value().hour , sunrise.value().minute);
      ESP_LOGD("sunrise_shift", "sunrise_min is %i , sunrise_hour is %i ,sunrise_minute is %i ", sunrise_min , sunrise_hour , sunrise_minute);
      ESP_LOGD("sunset_shift", "state relay is %i  ", id(relay).state );
      if((sunrise_hour == time.hour) and (sunrise_minute == time.minute))
        if(id(relay).state)
         id(relay).turn_off();
         ESP_LOGD("sunset_shift", "change relay is OFF" );
      char buffer[6];
      sprintf(buffer,"%02i:%02i", sunrise_hour , sunrise_minute);
      return {buffer};

[quote=“Miki_Spetik, post:5, topic:255633”]
id(sun_sun).sunset()
[/quote] doesn’t seem to work anymore. Have you updated your code by any chance?

I’m not sure but this might help:

auto sunset = id(sun_sun).sunset(id(sun_sun).elevation ());
auto sunrise = id(sun_sun).sunrise(id(sun_sun).elevation ());

Fixed working version

substitutions:
  #Shift sunset and sunrise on minutes
  shift_sunset: '-20' 
  shift_sunrise: '10' 

time:
  - platform: sntp
    id: sntp_time
    timezone: Europe/Prague

sun:
  latitude: !secret conf_latitude_data
  longitude: !secret conf_longitude_data
  id: sun_sun
  
switch:
  - platform: gpio
    pin: D4
    name: "Relay"
    id: relay
    inverted: yes

text_sensor:
  - platform: template
    name: "Light ON time"
    lambda: |-
      auto sunset = id(sun_sun).sunset(-0.833);//set default elevaton -0.833 must settings. id(sun_sun).elevation() not ? 
      auto time = id(sntp_time).now();
      if (!sunset.has_value())
        return {"NaN"};
      int sunset_min = sunset.value().hour * 60 + sunset.value().minute + ${shift_sunset};
      int sunset_hour = int(sunset_min / 60);
      int sunset_minute = sunset_min % 60;
      ESP_LOGD("sun_shift", "Sunset  is      %02i:%02i", sunset.value().hour , sunset.value().minute);
      ESP_LOGD("sun_shift", "Sunset shift is %02i:%02i", sunset_hour , sunset_minute);
      if((sunset_hour == time.hour) and (sunset_minute == time.minute))
        if(!id(relay).state)
         id(relay).turn_on();
      char buffer[8];
      sprintf(buffer,"%02i:%02i", sunset_hour , sunset_minute);
      return {buffer};
      
  - platform: template
    name: "Light OFF time"
    lambda: |-
      auto sunrise = id(sun_sun).sunrise(-0.833);
      auto time = id(sntp_time).now();
      if (!sunrise.has_value())
        return {"NaN"};
      int sunrise_min = sunrise.value().hour * 60 + sunrise.value().minute + ${shift_sunrise} ;
      int sunrise_hour = int(sunrise_min / 60);
      int sunrise_minute = sunrise_min % 60;
      ESP_LOGD("sun_shift", "Sunrise  is      %02i:%02i", sunrise.value().hour , sunrise.value().minute);
      ESP_LOGD("sun_shift", "Sunrise shift is %02i:%02i", sunrise_hour , sunrise_minute);
      if((sunrise_hour == time.hour) and (sunrise_minute == time.minute))
        if(id(relay).state)
         id(relay).turn_off();
      char buffer[8];
      sprintf(buffer,"%02i:%02i", sunrise_hour , sunrise_minute);
      return {buffer};
2 Likes

So as of today a delay can be made using elevation as below:

sun:
  latitude: !secret home_latitude
  longitude: !secret home_longitude

  on_sunset:
    - elevation: -3.5°
      then:
        - switch.turn_on:
            id: relay_1

Is there a delay in minutes availible like “- dealy: 00:10:00” ?