ESPHome Automation using sun as condition

Hi guys,
I am trying to add a “if condition” to my yaml file in esphome, but keep getting a error, "Unable to find condition with the name ‘sun.is_above_horizon’.
I’m following esphome docs as in the following link: https://esphome.io/components/sun.html#sun-is-above-below-horizon-condition

See code attached.

esphome:
  name: master_bedroom_cupboard
  platform: ESP32
  board: esp32doit-devkit-v1

wifi:
  ssid: "ssid"
  password: "password"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Master Bedroom Cupboard"
    password: "Iw0MTmL1zWsW"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


binary_sensor:
  - platform: gpio
    pin: 36
    name: "Nathan Door"
    device_class: door
    on_press:
      - if:
          condition:
            - sun.is_above_horizon:
          then:
            - light.turn_on:
                id: nathanlight
                brightness: 30%
            
    on_release:
      then:
        - light.turn_off:
            id: nathanlight
    
  - platform: gpio
    pin: 39
    name: "Mariska Door"
    device_class: door
    on_press:
      then:
        - light.turn_on:
            id: mariskalight
            brightness: 100%
    on_release:
      then:
        - light.turn_off:
            id: mariskalight

  - platform: gpio
    pin: 34
    name: "Mirror"
    device_class: door
    on_press:
      then:
        - light.turn_on:
            id: mirrorlight
            brightness: 100%
    on_release:
      then:
        - light.turn_off:
            id: mirrorlight

  - platform: gpio
    pin: 35
    name: "Drawer"
    device_class: door
    on_press:
      then:
        - light.turn_on:
            id: drawerlight
            brightness: 100%
            red: 100%
            green: 0%
            blue: 0%
    on_release:
      then:
        - light.turn_off:
            id: drawerlight

output:
  - platform: ledc
    id: nathan
    pin: 15
  - platform: ledc
    id: mariska
    pin: 2
  - platform: ledc
    id: mirror
    pin: 4
  - platform: ledc
    id: red1
    pin: 1
  - platform: ledc
    id: green1
    pin: 22
  - platform: ledc
    id: blue1
    pin: 3
  - platform: ledc
    id: red2
    pin: 18
  - platform: ledc
    id: green2
    pin: 19
  - platform: ledc
    id: blue2
    pin: 17 

light:
  - platform: monochromatic
    output: nathan
    id: nathanlight
    name: "Nathan Cupboard"
  - platform: monochromatic
    output: mariska
    id: mariskalight
    name: "Mariska Cupboard"
  - platform: monochromatic
    output: mirror
    id: mirrorlight
    name: "Mirror"  
  - platform: rgb
    name: "Kick Plate"
    id: kickplatelight
    red: red1
    green: green1
    blue: blue1
  - platform: rgb
    name: "Drawer"
    id: drawerlight
    red: red2
    green: green2
    blue: blue2

See error attached.

I just can’t seem to get this to work.

hi there, look at the very first requirements under the docs you linked to! You need to set up the location for sun and you need a time platform, like this:

sun:
  latitude: -34.000000
  longitude: 138.000000

time:
  - platform: homeassistant
    id: homeassistant_time

interval:
  interval: 10sec
  then:
    - if:
        condition:
          - sun.is_above_horizon:
        then:
          - logger.log: Sun is above horizon!

hope that helps!

PS you need to insert a then: after on_press: and before the - if:

Thank alot Patrick, I got it to work doing the following:

sun:
  latitude: xx.xxxxx
  longitude: xx.xxxxx

time:
  - platform: homeassistant
    id: homeassistant_time

binary_sensor:
  - platform: gpio
    pin: 36
    name: "Nathan Door"
    device_class: door
    on_press:
      - if:
          condition:
            - sun.is_above_horizon:
          then:
            - light.turn_on:
                id: nathanlight
                brightness: 100%
      - if:
          condition:
            - sun.is_below_horizon:
          then:
            - light.turn_on:
                id: nathanlight
                brightness: 30%
            
    on_release:
      then:
        - light.turn_off:
            id: nathanlight
2 Likes