ESPHome on_sunset multiple if condition

Hello,

I know I’m asking a lot of ESPhome with this conditional automation.

  • If it is December, turn on the RGB “XMAS” light effect.
  • If it is January, turn on the RGB “Random_60_15” light effect.
  • If it is February, turn on the RGB “Flicker” light effect with pink.

I can set the colors using on_time and leave the effect as flicker, but I’m trying to create a conditional that evaluates the month on sunset and sets the light effect based on the month.

The problem is that on_sunset doesn’t accept time conditionals as far as I can tell, so I’ve fallen back to lambdas.

As I read the documentation, yaml only allows for nested if statements. So I have to use this cascade of if statements. Am I understanding the limitations of ESPhome correctly. I’ve tried most of the other possible configurations short of just coding it all in C++ as a single lambda (possibly in a switch), which is probably about as hard as what I’m doing here once I call setting all the RGB light variables.

sun:
  latitude: 00.00
  longitude: 00.00
  on_sunset:
  ##########################
  # Original
  ##########################
    # - then:
    #   - light.turn_on: 
    #       id: h801_2_rgb
    #just for december effect: "XMAS"
    ######################################
    # New Code to apply the light effect at sunset when it turns on. 
    ######################################
    then:
      if: 
        condition:
          lambda: |-
            auto time = id(sntp_time).now();
            return time.month == 12; 
        then: 
          - light.turn_on: 
              id: h801_2_rgb
              effect: "XMAS"
        else:
          if: 
            condition:
              lambda: |-
                auto time = id(sntp_time).now();
                return time.month == 1; 
            then: 
              - light.turn_on: 
                  id: h801_2_rgb
                  effect: "Random_60_15"
            else:
              if: 
                condition:
                  lambda: |-
                    auto time = id(sntp_time).now();
                    return time.month == 2; 
                then:
                - light.turn_on: 
                    id: h801_2_rgb
                    effect: 'Flicker'
                    red: 100%
                    green: 0%
                    blue: 75%
                    brightness: 0
                    # - logger.log: "Valentines Flicker Activated"

1 Like

Probably answering my own questions but I think I’ve got it with un-nested if statements

then:
    - if: 
        condition:
          lambda: |-
            auto time = id(sntp_time).now();
            return time.month == 12; 
        then: 
          - light.turn_on: 
              id: h801_2_rgb
              effect: "XMAS"
        else:
    - if:
        condition:
          lambda: |-
            auto time = id(sntp_time).now();
            return time.month == 1; 
        then: 
          - light.turn_on: 
              id: h801_2_rgb
              effect: "Random_60_15"
    - if: 
        condition:
          lambda: |-
            auto time = id(sntp_time).now();
            return time.month == 2; 
        then:
        - light.turn_on: 
            id: h801_2_rgb
            effect: 'Flicker'
            red: 100%
            green: 0%
            blue: 75%
            brightness: 0
    
1 Like

From what I’ve found in ESPHome, your use of the datetime library looks to be the best way to differentiate those ‘seasons’.
https://esphome.io/components/time/index.html?highlight=date#use-in-lambdas

1 Like