Sun Condition in Automation

Hi,
Under Automation I want to add a condition SUN

Condition type

Sun

Before:

Sunrise

Sunset

Before offset (optional)

After:

Sunrise

Sunset

Do I have to specify both before and after conditions?

I only want my lights to activate after sunset.

Regards,

You should format your code for readability first.

Do you need a condition or a trigger?

Please explain your automation in plain English?

‘I want the lights to turn on at sunset’

‘I want to turn on the lights when the doorbell rings, but only after sunset’

I want it to be a condition in my automation.

I want my lights active after dusk / before dawn, do I need to specify both conditions i.e before sunrise and after sunset
`

  • id: ‘1576669964147’
    alias: Back Garden Security Light
    description: ‘’
    trigger:
    • entity_id: binary_sensor.pir_back_garden
      from: ‘off’
      platform: state
      to: ‘on’
      condition:
    • before: sunrise
      condition: sun
      action:
    • entity_id: switch.back_garden_security_light
      service: switch.turn_on
    • service: script.1578119325234
      `

It depends…

If you want one single automation to toggle the lights, you would need two triggers, one for sunset and one for sunrise (or just one for each sun.sun state change).

automation:
  - alias: Toggle with sun
    trigger:
      - platform: state
        entity_id: sun.sun
    action:
      - service: light.toggle
        entity_id: light.mylight

Or you create 2 automations for separate events. One for on at sunset and one for off at sunrise.

1 Like

The Trigger is the PIR.
SUN is a Condition in the Automation
The Service Script gives the light an OFF Delay

Please format your code correctly by marking your code and pressing the </> button at the top.

You need 2 conditions. Check out the sun condition part of the docs, there is a graph in there showing how it works.

HA changes sunset sunrise times on state change so imagining you would need an OR condition: before sunrise (counting from midnight) or after sunset.

automation:
  - id: ‘1576669964147’
    alias: Back Garden Security Light
    description: ‘’
    trigger:
      - platform: state
        entity_id: binary_sensor.pir_back_garden
        from: ‘off’
        to: ‘on’
    condition:
      conditions: or
        - condition: sun
          before: sunrise
        - condition: sun
          after: sunset
    action:
      - service: switch.turn_on
        entity_id: switch.back_garden_security_light

You can also use the Sun elevation angle, I use that in all of my Sun based automations instead of sunrise/sunset as when a reboot occurs the sunrise/sunset usage fails as it looks to the next event.

I.e. if the sun is already down, that event has past, so if a reboot occurs of your system, that automation will fail to work.

Try something like this. In this example, the light will trigger on while the sun is below 8° elevation in your location.

- alias: All Lights On
  hide_entity: true
  trigger:     
    platform: numeric_state
    entity_id: sun.sun
    value_template: "{{ state.attributes.elevation }}"
    below: 8
  action:
    service: light.turn_on
    data:
      entity_id: light.bar_light
      effect: colorloop
3 Likes

This is the snapshot from my automation configuration page

If that is the case, try this variation of my post above.

  trigger:
    platform: state
    entity_id: binary_sensor.pir_motion
    to: 'on'
  condition:
    condition: template
    value_template: "{{ states.sun.sun.attributes.elevation | int > 8.1 }}"
  action:
    service: light.turn_on
    entity_id: light.outdoor_light

If the PIR is triggered, and the Sun is below 8°, the light will turn on. If it’s above 8°, it will do nothing.

This code doesn’t match the document. The document has sunset and then sunrise ( which is how most people Invision this to work, i.e it turns on when the sun sets and off at sunrise) Not sure if it matters, but my automation did not run correctly like this.
The new built-in automation GUI builds it similar to how you typed it.

The order of sunrise and sunset doesn’t matter, it’s an OR condition it evaluates both conditions no matter which comes first. Please show the automation that did not run correctly.

So if I understand correctly, the code goes to sundown and goes to the next sunrise? It does not go to the 00 time and evaluate from there?

I am testing the new one and will know tonight. If it works I will post both. The one that failed was one that the automation GUI created. I’ll try and recreate the or. The code was definitely different than the docs. It has one condition: sun statement. I am not fluent in the code yet to be able to evaluate if it is equivalent. I do know that this is a common need and it is not working for me. I’ll post it when I get home.
From the automation

condition: or
conditions:
  - condition: sun
    before: sunrise
    after: sunset
    after_offset: '03:00:00'

From the docs:

condition: or
conditions:
  - after: sunset
    after_offset: '03:00:00'
    condition: sun
  - before: sunrise
    condition: sun

Are they the same?

The Automation GUI does not work.

If I input the following

image

I cannot get the Automation to trigger.

If I change it to
image

my Automation triggers current time is 16.37 and sun is below horizon.

I am guessing that after 00.00 this will not work again, will have to stay up late.

It was mentioned before that it should be an OR condition?

The automation editor does not (yet) support advanced conditions. You have to edit the automations file manually.

Thanks. So the advanced condition it is setting using the editor is incorrect?

The conditions set by the editor are ‘simple’. They are simply a non templated list of conditions that by design are AND conditions.

I think I have got it to work.

`
condition: or
conditions:

  • before: sunrise
    condition: sun
  • condition: or
    conditions:
    • after: sunset
      condition: sun
      `
1 Like

Are they changing it as there is an “or” option?

we can read with you if you would use triple back quotes

```

before and after your code. :wink: