Sun below horizon = lamp on?

Hi,

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”?

Thanks,
-Ambi

Home assistant is event driven.

1 Like
  • id: bathroom motion led on
    alias: bathroom motion led on
    trigger:
    • entity_id: binary_sensor.motion_sensor_xxxx14
      from: ‘off’
      platform: state
      to: ‘on’
      condition:
      condition: or
      conditions:
      - condition: sun
      after: sunset
      after_offset: “-01:00:00”
      - condition: sun
      before: sunrise
      after_offset: “01:00:00”
      action:
    • 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'
  trigger: 
    # 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 }}"
  action:
    - service_template: >-
        {% if is_state('sun.sun','above_horizon') %}
          light.turn_off
        {% else %}
          light.turn_on
        {% endif %}
      data:
        entity_id: light.your_light

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

2 Likes

Great answer, thanks for your help!