Switch on lights on door open, but after sunset

Hi all,

I am a real newbie on the HA platform, and have been trying my hand at some basic automation, but seem to be coming unstuck. Really hoping someone on here can help me figure out where I am going wrong.

I would like to switch on my patio lights when I open my patio door, but only after sunset. I have tried various permutations in my configuration.yaml, but keep getting errors.

Here is the code as it stands now:

automation: 
  condition:
    - condition: state
      entity_id: binary_sensor.patio_door
      state: 'on'
    - condition: state
      entity_id: sun_sun
      state: 'below_horizon'
  action:
    - service: light.turn_on
      entity_id: light.patio_led

Any help would be appreciated.

automation: 
- alias: example 1
  trigger:
    - platform: state
      entity_id: binary_sensor.patio_door
      to: 'on'
  condition:
    - condition: state
      entity_id: sun_sun
      state: 'below_horizon'
  action:
    - service: light.turn_on
      entity_id: light.patio_led

Thanks, but I get the following error on the config validation:

Invalid config for [automation]: Entity ID sun_sun is an invalid entity id for dictionary value @ data[‘condition’][0][‘entity_id’]. Got None. (See /config/configuration.yaml, line 17).

Try changing the underscore to a period in the id.

Yes, a typo I copied and overlooked to correct.

automation: 
- alias: example 1
  trigger:
    - platform: state
      entity_id: binary_sensor.patio_door
      to: 'on'
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    - service: light.turn_on
      entity_id: light.patio_led

1 Like