Question about how to make an automation "When I get home, and the sun is set till the sun rises, turn on the hallway and outside light"

Hello everyone!

I’m having trouble on making an automation work.

I want it to turn on hallway and outside light, when I get home (but only when the sun goes down till the sunrises).

The device is a Samsung A9. Here is the automation. Do I need to turn on something on at the configuration.yaml or is the “code” wrong?

- id: '1600802420933'
  alias: Turn on the lights when the sun is set and I get home
  description: ''
  trigger:
  - platform: state
    entity_id: device_tracker.sm_a920f
    from: not_home
    to: home
  condition:
  - condition: sun
    before: sunrise
    after: sunset
  action:
  - type: turn_on
    device_id: 342f60873f6e466f9e2883cd4d220524
    entity_id: light.hallway_ikea_bulb
    domain: light
    brightness_pct: 75
  - type: turn_on
    device_id: 8ee305f03a3e41de8e70020409f251c5
    entity_id: light.outside_light
    domain: light
    brightness_pct: 100
  - wait_template: 00:10:00
    timeout: ''
  - type: turn_off
    device_id: 8ee305f03a3e41de8e70020409f251c5
    entity_id: light.outside_light
    domain: light
  - type: turn_off
    device_id: 342f60873f6e466f9e2883cd4d220524
    entity_id: light.hallway_ikea_bulb
    domain: light
  mode: single

Thanks for your help!!

Classic mistake. The condition is wrong. The sun can’t be before sunrise and after sunset at the same time.

Rewrite your condition this way :

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

What type of device tracker is it? GPS?
If so then make sure the home zone is large enough to handle inaccuracies or your lights will toggle on and off all night.

Take a look at this custom sensor to determine what part of the day for your sunrise/sunset condition.

Thanks for all your responses and for the late response. I changed the code and now it works! Thank you so much! :smiley: