Sun component, after sunset, that's always?

Hi, i’m trying to set my hallway light sensors to only activate the lights if its after sunset (with an offset), however it never works. I’m wondering if its because the “after: sunset” is effectively all of the time, or does this work on a 24hours basis?

Here is my config:

alias: Turn on hallway lights with motion
trigger:
  platform: state
  entity_id: binary_sensor.kitchen_sensor_sensor_17_0
  from: 'off'
  to: 'on'
condition:
  condition: or
  conditions:
    - condition: state
      entity_id: light.hallway_downstairs_light_level_13_0
      state: 'off'
    - condition: sun
      after: sunset
action:
  service: light.turn_on
  entity_id: light.hallway_downstairs_light_level_13_0

After sunset is from sunset to midnight. Before sunrise is from midnight to sunrise.

You’ve checked that the sensor you specify changes state?

Also, the logic in your condition - it says to turn the light on if it’s off, or if it’s after sunset. That doesn’t match your description, so maybe the following instead:

alias: Turn on hallway lights with motion
trigger:
  platform: state
  entity_id: binary_sensor.kitchen_sensor_sensor_17_0
  from: 'off'
  to: 'on'
condition:
  condition: sun
  after: sunset
action:
  service: light.turn_on
  entity_id: light.hallway_downstairs_light_level_13_0

I just noticed that i have “condition: or”, i’m sure i changed this to “and”!

Yes i only want to turn the light on if its currently off, because otherwise this automation would be running unnecessarily on a light that is already on.

Changed to this, thanks for the info, i’ll test tomorrow morning!:

alias: Turn on hallway lights with motion
trigger:
  platform: state
  entity_id: binary_sensor.kitchen_sensor_sensor_17_0
  from: 'off'
  to: 'on'
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_downstairs_light_level_13_0
      state: 'off'
    - condition: sun
      after: sunset
    - condition: sun
      before: sunrise
action:
  service: light.turn_on
  entity_id: light.hallway_downstairs_light_level_13_0

That won’t work:

If you’ve got both in an and, only one will ever be true, and so the statement will always be false. You’ll need to nest those with or:

condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_downstairs_light_level_13_0
      state: 'off'
    - condition:or
      conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
1 Like

I’m getting this now:

homeassistant.bootstrap: Invalid config for [automation]: extra keys not allowed @ data[‘condition’][0][‘conditions’][1][‘conditions’][1][‘before’]. Got None
not a valid value for dictionary value @ data[‘condition’][0][‘conditions’][1][‘conditions’][1][‘condition’]. Got None
required key not provided @ data[‘condition’][0][‘conditions’][1][‘conditions’][1][‘entity_id’]. Got None. (See ?:?). Please check the docs at Automation - Home Assistant

alias: Turn on kitchen lights with motion
trigger:
  platform: state
  entity_id: binary_sensor.hallway_downstairs_sensor_sensor_8_0
  from: 'off'
  to: 'on'
condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.kitchen_light_level_6_0
      state: 'off'
    - condition: or
      conditions:
      - condition: sun
        after: sunset
      - condition: sun
        before: sunrise
action:
  service: light.turn_on
  entity_id: light.kitchen_light_level_6_0

What version number of HA are you on?

I just dropped that exactly as you posted into my own config and it didn’t generate any errors.

I’m in learning mode so just curious if this version is a little simpler and would work using the sun’s elevation instead of the sunset/sunrise conditions.

condition:
  condition: and
  conditions:
    - condition: state
      entity_id: light.hallway_downstairs_light_level_13_0
      state: 'off'
    - condition: template
      value_template: '{{ states.sun.sun.attributes.elevation < 0 }}'

0.35.3, no idea why i’m getting this then…

You’re quite far behind then, that’s most likely the reason why.

I wouldn’t consider a month or so “far behind” in any software, but sure i can upgrade that isn’t a problem. I’m not sure what exact change in the last month you think could have caused it anyway.

Turns out it was just a typo.

Thanks for the help, the docs are actually quite tricky to understand, nothing seems to be “complete”, and the required information for a single component always seems to be spread across 2-3 pages, places where you would never expect it!

Well, since there’s been 3 point releases, and many sub-releases, and many of those changes are minimally documented, I work on the theory that more than one point release is “far” :wink:

Yeah, the documentation is spotty. The stock answer you’ll get is to use the Edit this page in GitHub link and submit improvements. Part of me feels like pushing fixing that to the us, the user base, is wrong - on the other hand I’d rather those with the skills to dev focussed their time on cool things and providing “just enough” documentation for the rest of us to address.

Don’t get me wrong i’m a sucker for updating all the time, i forgot it was a pip package with little other pre-update steps so i’ve done it already and will continue to do so.

Yea I understand that the devs are best spent working on dev’ing, but this forum would be virtually empty if the docs were spruced up a little, for software where config is so very important, my view is the docs for the configs are just as important as the code in some respects.

1 Like