Get sun position on startup

Hello everybody,

I am trying to get the sun position on esp boot.

substitutions:
  system_name: porte_poules_v2
  friendly_name: Poulailler

      
sun:
  latitude: XXX
  longitude: XX
  
  on_sunrise:
    - then:
      - cover.open: porte_poules
  on_sunset:
    - then:
      - cover.close: porte_poules
      
esphome:
  name: ${system_name}
  platform: ESP8266
  board: d1_mini
  on_boot:
    priority: -100
    then:
      - if:
          condition:
            - sun.is_above_horizon:
          then:
            - logger.log: "****************************************************"
            - logger.log: "Ouverture porte poule"
            - logger.log: "****************************************************"
            #- cover.open: porte_poules
          else:
            - logger.log: "****************************************************"
            - logger.log: "ERREUR porte poule"
            - logger.log: "****************************************************"
        
      
wifi:
  ssid: !secret iot_wifi_ssid
  password: !secret iot_wifi_pwd
  
  manual_ip:
    static_ip: XXXXX
    gateway: XXXXX
    subnet: XXXX

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: ${friendly_name}
    password: !secret iot_wifi_callback

captive_portal:

# Enable logging
logger:


# Enable Home Assistant API
api:
  password: !secret iot_ota_pwd

ota:
  password: !secret iot_ota_pwd

mqtt:
  broker: !secret iot_mqtt_broquer
  port: !secret iot_mqtt_port
  on_message:
    - topic: Home/ota_mode
      payload: 'on'
      then:
        - logger.log: "Pas de deep sleep"
        - deep_sleep.prevent: deep_sleep_mode
    - topic: Home/ota_mode
      payload: 'off'
      then:
        #- deep_sleep.enter: deep_sleep_mode

deep_sleep:
  id: deep_sleep_mode
  run_duration: 120s
  sleep_duration: 20min
  
dallas:
  - pin: D4
    update_interval: 60s

  
text_sensor:
  - platform: version
    name: "${friendly_name} version"

time:
  - platform: homeassistant


sensor:
  - platform: wifi_signal
    name: "${friendly_name} signal wifi"
    update_interval: 30s

  - platform: uptime
    name: "${friendly_name} uptime"
    update_interval: 60s

  - platform: dallas
    address: 0x963C01A81664FA28
    name: "Temperature ${friendly_name}"
    
  - platform: adc
    pin: A0
    name: "Tension batterie ${friendly_name}"
    icon: "mdi:car-battery"
    filters:
      - multiply: 20.7
    update_interval: 60s

output:
  - platform: gpio
    id: 'shade1'
    pin: D1
  - platform: gpio
    id: 'shade2'
    pin: D2
    
switch:
  - platform: output
    #name: "Porte poules ouvre"
    output: 'shade1'
    id: shade_s1
  - platform: output
    #name: "Porte poules ferme"
    output: 'shade2'
    id: shade_s2


cover:
  - platform: template
    name: "Porte ${friendly_name}"
    id: porte_poules
    optimistic: true
    assumed_state: true
   #   TEMPLATE FOR END STOP SWITCHES
#    lambda: |-
#      if (id(porte_poule_stop_haut).state) {
#        return cover::COVER_OPEN;
#      } else if (id(porte_poule_stop_bas).state){
#        return cover::COVER_CLOSED;
#      }
    open_action:
      - switch.turn_off: shade_s2
      - switch.turn_on: shade_s1
      - delay: 30s
      - switch.turn_off: shade_s1
    close_action:
      - switch.turn_off: shade_s1
      - switch.turn_on: shade_s2
      - delay: 30s
      - switch.turn_off: shade_s2
    stop_action:
      - switch.turn_off: shade_s1
      - switch.turn_off: shade_s2
      

binary_sensor:
  - platform: gpio
    pin: D5
    name: "Boutton ${friendly_name}"
    filters:
      invert:
    on_press:
      then:
        - lambda: |
            if (id(porte_poules).position == cover::COVER_OPEN) {
              if (id(shade_s1).state){
                // shade is opening
                id(porte_poules).stop();
              } else {
                // shade is open and not moving
                id(porte_poules).close();
              }
            } else {
              if (id(shade_s2).state){
                // shade is closing
                id(porte_poules).stop();
              } else {
                // shade is closed and not moving
                id(porte_poules).open();
              }
            }
#   END STOP EXAMPLES 
  - platform: gpio
    pin: D7
    name: "Porte poule stop haut"
    filters:
      invert:
    on_press:
      then:
        - switch.turn_off: shade_s1
        - switch.turn_off: shade_s2
    id: porte_poule_stop_haut
  
  - platform: gpio
    pin: D8
    name: "Porte poule stop bas"
    filters:
      invert:
    on_press:
      then:
        - switch.turn_off: shade_s1
        - switch.turn_off: shade_s2
    id: porte_poule_stop_bas  

Unfortunatelly, It is not working

Do you have any idea ?

Thanks a lot

Is there a particular reason you don’t just use the HA sun component to get this info?

because My esp is far from my wifi and it connect not always. So i would like to run it without wifi connection

It’s possible that if the module has no internet connection, it doesn’t know when it is sunrise/sunset as those times change daily, but I’m not too sure as I don’t use ESPhome.

I’m sure you have already looked here as well.

Thanks but I am using this

I would like to use deep sleep and restart every 20minutes. If I do this, I have a lot of change to pass the sunset or sunrise. In this condition I would like to check on reboot if the sun is over or above horizon.

But my condition on reboot does not work, I go always in the “else” condition

esphome:
  name: ${system_name}
  platform: ESP8266
  board: d1_mini
  on_boot:
    priority: -1000
    then:
      - if:
          condition:
            - sun.is_above_horizon:
          then:
            - logger.log: "****************************************************"
            - logger.log: Sun is above horizon!
            - logger.log: "****************************************************"
            #- cover.open: porte_poules
          else:
            - logger.log: "****************************************************"
            - logger.log: "ERREUR porte poule"
            - logger.log: "****************************************************"

If the module has no internet connection though, how is it getting the information on Sunrise/Sunset?

I will have internet but not every reboot.
Now I am testing in my house with internet connection and it does not work (my condition on reboot)

Without deep_sleep, I get sunset and sunrose working without problem.

You won’t get sunrise without time and all the time connections require connection to the network, with one exception. That exception is the gps time component, which takes its time from a gps sensor.

I don’t know much about ESPHome. Can you include a compiled C routine into it? If so then, as long as you know what the time is and the location, you can calculate the sun’s position. There are a number of examples on GitHub that demonstrate this. For example: https://github.com/vivkin/suntime. If you know the current time then you can compare that to the sunrise and sunset times and calculate if the sun is above the horizon.