esphome:
  name: livingroom
  platform: ESP8266
  board: esp01_1m
  #  board: esp12e
  includes: 
    - MySunTextSensor.h

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

substitutions:
  #Shift sunset and sunrisen on minute
  shift_sunset: '+17' 
  shift_sunrise: '+6' 

# At least one time source is required
time:
  - platform: homeassistant     # sntp
    timezone: "Europe/Berlin"
    id: esptime                 # sntp_time
    # - minutes: !lambda 'return id(heating_on_time_1_minutes).state;'
    #   hours: !lambda 'return id(heating_on_time_1_hours).state;'

# At WiFi setup
wifi:
  ssid: "WLAN-LX"
  password: "SP-RAXX749x7"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Livingroom Fallback Hotspot"
    password: "R8ZKoXbtquSv"

# Example configuration entry
web_server:
  port: 80

# determine Sunrise and Sunset based on geographical location
sun:
  latitude: 48.77034 
  longitude: 8.34306

  on_sunrise:
    - then:
      - logger.log: Good morning!
    # Custom elevation, will be called shortly after the trigger above.
    #- elevation: 5°
    #  then:
    #  - logger.log: Good morning 2!

  on_sunset:
    - then:
      - logger.log: Good evening!

captive_portal:

# Example configuration entry
text_sensor:
  - platform: sun
    name: Sun Next Sunrise
    type: sunrise
    
  - platform: sun
    name: Sun Next Sunset
    type: sunset

  - platform: version
    name: Platform
    on_value:
      then:
        - lambda: |-
            ESP_LOGD("main", "The current version is %s", x.c_str()); 

  - platform: template
    name: "Template Text Sensor"
    lambda: |-
      char str[17];
      time_t currTime = id(esptime).now().timestamp ;
      strftime(str, sizeof(str), "%Y-%m-%d %H:%M", localtime(&currTime));
      return {str};
    update_interval: 60s      

  - platform: template
    name: "Sunrise"
    id: sunrise_shift
    lambda: |-
      auto sunrise = id(sun_sun).sunrise();
      auto time = id(esptime).now();
      sun_sun->set_latitude(48.77034);
      sun_sun->set_longitude(8.34306);
      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);
      char buffer[6];
      sprintf(buffer,"%02i:%02i", sunrise_hour , sunrise_minute);
      return {buffer};

#      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" );


  - platform: template
    name: "Sunset"
    id: sunset_shift
    lambda: |-
      auto sunset = id(sun_sun).sunset();
      auto time = id(esptime).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);
      char buffer[6];
      sprintf(buffer,"%02i:%02i", sunset_hour , sunset_minute);
      return {buffer};
 
#      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" );      

  - platform: template
    name: "Sunset Algo"
    lambda: |-
      int sunrise;
      int sunset;
      auto sunSensor = new MySunTextSensor();
      App.register_component(sunSensor);
      auto sun_sensor = sunSensor->sun;
      auto time = id(esptime).now();
      sun_sensor->setPosition(48.77034, 8.34306, 1);
      sun_sensor->setCurrentDate(2021, 03, 05);
      sunrise = static_cast<int>(sun_sensor->calcSunrise());
      sunset = static_cast<int>(sun_sensor->calcSunset());
      char buffer[100];
      
      sprintf(buffer, "Sunrise at %.2d:%.2dam, Sunset at %.2d:%.2dpm", (sunrise/60), (sunrise%60), (sunset/60), (sunset%60));
      return {buffer};

# sun_sensor->setCurrentDate(time.year(), time.month(), time.day());

#text_sensors:
#    name: "My Custom Text Sensor"
