ESPHome trigger on_sunrise or on_sunset with conditions?

Is there a way to include conditions checks using the on_sunrise or on_sunset automations?

If there is, I can’t figure out the syntax. Currently, I have:

sun:
  latitude: !secret latitude
  longitude: !secret longitude
  on_sunrise:
    - elevation: -10°
      then:
        - fan.turn_on:
            id: ${node_name}

but I want to include a check for a global variable I set elsewhere called use_morning_precharge. Something like this:

    - if:
        condition:
          and:
            - lambda: return id(use_morning_precharge) == true;

Is there any way to gate the action by both the sun’s elevation value and the global variable?

I think you were almost there, as you can insert the if Action Automation like this:

sun:
  latitude: !secret latitude
  longitude: !secret longitude
  on_sunrise:
    - elevation: -10°
      then:
        - if:
            condition:
              lambda: 'return id(use_morning_precharge) == true;'
            then:
              - fan.turn_on:
                  id: ${node_name}