Sun condition written out by automation editor does not work

The docs has this snippet, which works

condition:
    condition: or  # 'when dark' condition: either after sunset or before sunrise - equivalent to a state condition on `sun.sun` of `below_horizon`
    conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise

but the automation editor writes this out to the yaml file which does not allow my automation to work.

  condition:
  - after: sunset
    before: sunrise
    condition: sun

should the automation editor code work?

I’ve been fighting with this for around a month. And not once did I think of checking the yaml that the editor spits out…

Mine is also upside down and as you say, it doesn’t actually work.
I’ve not tried hard coding it in Yaml yet though.

The editor doesn’t do ‘complex’ or conditions.

But the second code block is the result of clicking the radio buttons for before sunrise and after sunset and then saving. I would expect that to mean night time.
If it can’t produce the expected in code it should warn you or prevent you from saving or something.
I assume if I reversed my selection it would work during the day because a boolean ‘and’ would be valid and non complex?

1 Like

I understand that.
However, if you choose the ‘Sun’ feature as a ‘State’ in the conditions, you get radio buttons for ‘before’ & ‘after’ sunset/sunrise as well as a input area to enter an offset amount if you would like.
All I am doing is clicking two radio buttons that are available via the front-end and clicking save.

I never posted about this issue previously as I thought I was being stupid and doing something wrong with the editor (I don’t know what it would be because as I said, you only need to click two radio buttons and click save).

As someone starting out with using automations and the ease of using a frontend editor, this is really not helpful to a newbie.

1 Like

I understand your frustration. If you have an automation with or conditions like you made manually, if you open it in the editor, You will see an error that it’s not supported.

I’m not saying it’s right I’m just saying it is what it is. These types of conditions you can’t edit that way.

1 Like

No problem! It wasn’t aimed at you (sorry if you thought that).

Looks like I’ll have to learn Yaml automations! :smiley:

2 Likes

The crux of the confusion for the user here is not really that you can’t edit or create ‘or’ conditions it’s that a built in UI element creates a condition that can never evaluate as true and lets you save it. The resulting code effectively silently disables the automation.
If it’s impossible for it to be before sunrise and after sunset at the same time I should not be able to select that as a choice in the UI.

There are a couple of other ways to achieve the same/similar desired result using the automation editor but delving in with a text editor is definitely advised (make sure you use something like Atom, Visual Studio Code or the configurator add-on for hass.io).

  1. Use the STATE condition and the entity sun.sun with the state set to below_horizon
  2. Use the TIME condition and set your before and after times to when sunset/sunrise happens where you are (this may need manual adjustment throughout the year)
1 Like

That’s why I use the sun angle instead of sunset as that’s a bit more predictable lightwise.

1 Like

I agree but that’s just one of the many problems with the GUI editor

Ahh yep, thanks, I’ll give that a go too :smiley:

This is actually the automation I use for my lounge lights:

- id: '1505977024304'
  alias: Turn Lounge Light On Elevation < 25° & after 4PM
  initial_state: 'true'  
  trigger:
  - platform: numeric_state
    below: 25
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
  - platform: time
    at: '16:00:00'
  condition:
  - condition: time
    after: '16:00:00'
    before: '18:00:00'
  - condition: state
    entity_id: 'input_boolean.homeandawayauto'
    state: 'on'
  - condition: template
    value_template: '{{states.sun.sun.attributes.elevation < 25}}'
  action:
  - service: light.turn_on
    data:
      brightness_pct: 75
      entity_id: light.lounge
      kelvin: 3100

So I’m looking for it to come on when the sun is at 25° degrees and after 4PM as long as I haven’t switched off the input_boolean (when I am away). The tim condition of after 4 and before 6 is so that if I turn the automation on again after 6 it won’t trigger… actually, now I think of it, I used to have an automation that disabled automations when I was away but I switched to using an input_boolean instead so the automation is always on and I don’t need this condition anymore… just after 4 so I will take out the before 6 part.

Note that either after 4 or sun angle below 25° will trigger it but then the conditions also get checked before the action will be performed.

1 Like

Know this is an old topic but in case it helps some one else heres another take on it: This can be done through the front end but you need an input_boolean and couple of automations to toggle the value,

Eg Put this in configuration 

input_boolean:
  isdark:
    name: Is it dark outside?
    initial: off
    icon: mdi:moon-waning-crescent

Then some thing like the following, in automations (can be configured via the gui)

- id: '1569694302440'
  alias: Time - Gets Dark
  trigger:
  - event: sunset
    offset: -01:30
    platform: sun
  condition: []
  action:
  - alias: ''
    data:
      entity_id: input_boolean.isdark
    service: input_boolean.turn_on
- id: '1569694447606'
  alias: Time - Gets Light
  trigger:
  - event: sunrise
    offset: 01:00
    platform: sun
  condition: []
  action:
  - data:
      entity_id: input_boolean.isdark
    service: input_boolean.turn_off

The your condition for the automation you actually wanted to write is just based oiff the bololean IsDark…

What about using Not Daytime? This satisfies the automation editor:

condition: not
conditions:
  - condition: sun
    before: sunset
    after: sunrise