Sun below horizon = lamp on?


Rather than an automation to turn on a light at sunset (+/- some offset), and then a second to turn it off at sunrise (+/- offset), can’t we use “below horizon” as a stand in for “dark”? I’d want a single automation that says “while dark turn on if off, else turn off if on”.

So I suppose the basic question is are “do while” loops available for automations, and how often does it loop to check the status of “dark”?


Home assistant is event driven.

1 Like
  • id: bathroom motion led on
    alias: bathroom motion led on
    • entity_id: binary_sensor.motion_sensor_xxxx14
      from: ‘off’
      platform: state
      to: ‘on’
      condition: or
      - condition: sun
      after: sunset
      after_offset: “-01:00:00”
      - condition: sun
      before: sunrise
      after_offset: “01:00:00”
    • data:
      color_name: orangered
      brightness_pct: 80
      entity_id: light.led_bathroom
      service: light.turn_on

Please format your post correctly.

You can use a value template and check for the elevation, that’s what I do.

1 Like

See the sticky post for how to correctly format code blocks, and other things. If you don’t do that, then nobody can use what you posted.

You can do that with templates. That does mean that your single automation is harder to debug and maybe harder to read though:

- alias: 'Make the lights chase the sun'
    # Use only of these
    # Triggers when the sun goes above/below the horizon
    - platform: state
      entity_id: sun.sun
    # Triggers when the sun goes above/below the defined elevation
    - platform: value_template
      value_template: "{{ state_attr('sun.sun','elevation')|int < -3 }}"
    - service_template: >-
        {% if is_state('sun.sun','above_horizon') %}
        {% else %}
        {% endif %}
        entity_id: light.your_light

And as has been said, no. Home assistant is event based.


Great answer, thanks for your help!