Help with sunrise and sunset

I have this autonation. I want it to run every morning at 06:30 and if the sun has not risen yet at this time, lights should be turned on. Today the sun rose at. 06:20 and the automation still ran.

alias: Turn on Lights in the morning
description: ""
trigger:
  - platform: time
    at: "06:30:00"
condition:
  - condition: time
    weekday:
      - tue
      - thu
      - mon
      - wed
      - fri
  - condition: numeric_state
    entity_id: sun.sun
    attribute: elevation
    below: -2
action:
  - type: turn_on
    device_id: c86bba1
    entity_id: light.ikea_e27_1
    domain: light

What sun elevation value should I use here?

Likewise, I would like the lights to be turned off right before sunrise again.


alias: Turn off lights In the morning
description: ""
trigger: 
  - platform: numeric_state
    entity_id: sun.sun
    above: -1
condition: []
action:
  - type: turn_off
    device_id: 0aaaedc5
    entity_id: light.ikea_e14_3
    domain: light

I get really confused. How is above/below actually working? Positive and negative numbers?

Please format your code properly so it can be read and understood.

Troubleshooting Automations

In your second example you are missing the attribute. The state of sun.sun is not numeric… you need to specify that the trigger or condition should be listening to the elevation attribute.

Elevation at sunrise and sunset is 0, but you can use whatever value you want.

When you use above, the value of the state (or attribute in this case) changes must be greater than the specified value in order for the condition to pass.

Both positive and negative numbers are supported.

Couldn’t find the formatting options in phone version.

Ah, thanks. :slight_smile:

As I understand it postive integers are above, negative are below.

I think this is where most confusion comes from me.

The first example, I want to check if sun is about 2 degrees before it rises. Does that mean that both of these are equivalent?

  • Above: -2
  • Below: 2

Likewise for the second example, these two are the same?

  • Above: -1
  • Below: 1

No. Both positive and negative numbers can be used in both above and below. The value you use defines the threshold, while above and below define the direction of comparison. For example…

condition: numeric_state
entity_id: sun.sun
attribute: elevation
below: -2

… can be summarized as follows: “Is the sun’s elevation less than -2°”

No, those are two very different conditions.

  • above: -2 will be true starting a short time before sunrise and ending a short time after sunset.
  • below: 2 will be true starting a short time before sunset and ending a short time after sunrise.

Alright, not sure what

Still confufsed I’m afraid. Today, sunrise occured at 06:31

The turn on lights automation ran at 06:30 and the sun elevation condition was true. The condition checks if the sun is above -4.

The turn off lights automation ran at 06:22. I currently have it set to run at Above -2. Which is fine I guess, it did run before the sun rose

With this logic, I think there will be issues as long as the off job runs before the on job.

Maybe I should also add a condition to make sure these two only runs during like October-april or similar. During summer it is bright outside anyway so then it is not needed. Something like this condition:

  - condition: template
    value_template: "{{ now().month >= 10 or now().month <= 4 }}"
    alias: Winter months
    enabled: true

So am I… you keep only providing snippets and making statements that are confusing, or of indiscernible purpose. For example, what is the purposes of the following?

To summarize the quote above,

  1. You have stated that the solar elevation is 0 at 6:31.
  2. The automation ran at 6:30 (when the solar elevation would be barely less than 0, but let’s call it -0.1).
  3. The automation has a condition to run only when the solar elevation is above -4
  4. Mathematically -0.1 is greater than -4, so the condition passes. That is the expected behavior.

If this is not what you wanted to happen, what behavior did you expect?