Automation help with sunset condition

Can anyone tell me where I’m going wrong?

I have 2 floodlights that don’t have PIR(yet) attached to my garage, so I set my back door to turn on these lights when opened. Now I want to make sure this is only done when dark, around sunset.

  • alias: Turn on Garage Light
    trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0000fd1d75
    from: ‘off’
    to: ‘on’
    condition:
    condition: sun
    event: sunset
    offset: “-00:15:00”
    action:
    service: homeassistant.turn_on
    entity_id: script.garage_light

I’m getting this error, but unsure what to do next.

extra keys not allowed @ data[‘condition’][0][‘conditions’][0][‘event’]. Got None

Try this. I believe event is a one time action. If you want the light to come on any time after the sun sets you want to use below_horizon.

- alias: "Turn on Garage Light"
  trigger:
    - platform: state
      entity_id: binary_sensor.door_window_sensor_158d0000fd1d75
      from: 'off'
      to: 'on'
  condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
      service: homeassistant.turn_on
      entity_id: script.garage_light
1 Like
## Motion sensor frontdoor
- alias: Light on
  trigger:
    - platform: state
      entity_id: sensor.visitor
      to: '1'
      
  condition:
    - condition: sun
      after: sunset
      # Optional offset value
      after_offset: "0:30:00"
     
  action:
    - service: homeassistant.turn_on
      entity_id: switch.light.frontdoor

- alias: Light off after 5 minuts
  trigger:
    - platform: state
      entity_id: sensor.visitor
      to: '0'
      for:
        minutes: 5
        seconds: 30
  action:
    - service_template: homeassistant.turn_off
      entity_id: switch.light.frontdoor

This is what I use.

1 Like

Ahh, with both of your help, I came up with this, which works great. Thank you!

- alias: Turn on Garage Light
  trigger:
    platform: state
    entity_id: binary_sensor.door_window_sensor_158d0000fd1d75
    from: 'off'
    to: 'on'
  condition:
    condition: state
    entity_id: sun.sun
    state: 'below_horizon'
  action:
    service: homeassistant.turn_on
    entity_id: script.garage_door

Hi All,

I am using the following automation however am getting an error. Herewith the config which is in automation.yaml

  • alias: “Main Bedroom is Cold”
    trigger:

    • platform: numeric_state
      entity_id: sensor.master_bedroom_motion_sensor_temperature
      below: 20
      condition: state
      entity_id: sun.sun
      state: ‘below_horizon’
      action:
      service: homeassistant.turn_on
      entity_id:
    • switch.master_anna_heater
  • alias: “Main Bedroom is Hot”
    trigger:

    • platform: numeric_state
      entity_id: sensor.master_bedroom_motion_sensor_temperature
      above: 23
      condition: state
      entity_id: sun.sun
      state: ‘below_horizon’
      action:
      service: homeassistant.turn_off
      entity_id:
    • switch.master_anna_heater

However this is the error I am getting in the logs with HA not able to start

File “uvloop/loop.pyx”, line 1879, in uvloop.loop.Loop.call_exception_handler (uvloop/loop.c:35028)
stdout
14:44:07
File “/usr/src/app/homeassistant/core.py”, line 95, in async_loop_exception_handler
stdout
14:44:07
_LOGGER.error(“Error doing job: %s”, context[‘message’], **kwargs)
stdout
14:44:07
KeyError: 'message’e[0m
stdout
14:44:08
e[31m17-03-09 14:44:08 ERROR (Dummy-15) [homeassistant.core] Error doing job: Task was destroyed but it is pending!e[0m
stdout
14:44:08
e[31m17-03-09 14:44:08 ERROR (Dummy-15) [homeassistant.core] Error doing job: Task was destroyed but it is pending!e[0m
stdout
14:44:12
Config directory: /config
stdout
14:44:14
e[31m17-03-09 14:44:14 ERROR (Thread-3) [homeassistant.util.yaml] mapping values are not allowed here
stdout
14:44:14
in “/config/automation.yaml”, line 651, column 16e[0m
stdout
14:48:11
Config directory: /config
stdout
14:48:12
e[31m17-03-09 14:48:12 ERROR (Thread-3) [homeassistant.util.yaml] mapping values are not allowed here
stdout
14:48:12
in “/config/automation.yaml”, line 651, column 16e[0m

Any ideas on where I am going wrong?

Thanks

btw Line 651 is entity_id: sun.sun…

the condition you are using with “below horizon” just triggers as true if the sun is already set i guess, is there a way to set an offset ? because its quite dark in my home an hour before sunset, so this condition would not really work for me.
or is the offset just for an event?

1 Like

Maybe use elevation as a condition instead of sunset??

          - condition: numeric_state
            entity_id: sun.sun
            value_template: '{{ state.attributes.elevation }}'
            below: 10

Could someone confirm this is the right way to use offsets for a sun condition. I want it to be true from 30 min before sunset to 30min after sunrise:

    - condition: or
      conditions:
        - condition: sun
          after: sunset
          after_offset: "-00:30:00"
        - condition: sun
          before: sunrise      
          before_offset: "+00:30:00"

Based on your statement:

This would be correct.

    - condition: or
      conditions:
        - condition: sun
          after: sunrise
          after_offset: "+00:30:00"
        - condition: sun
          before: sunset
          before_offset: "-00:30:00"
1 Like

I need the condition to be true at night and add 30min before and after it.

What I meant is:

from sunset -30min
to sunrise +30min

I think your condition is true for daylight?
So my condition above is correct right?

Yep, you’re right. You just phrased it oddly in your original post.

Yes, I did sorry :smiley: Saw that after you posted your condition.