Controlling lighting based on Sun

Hi all,

I’m hitting some really big stumbling blocks on simple things.

I’d like to control lighting based on a motion sensor and the time of day. Not clock based but sunrise and sunset.

I’ve tried all configurations in the GUI and they stop working as soon as introduce sun as a condition.

Can anyone think why? Below is the YAML

alias: Hallway Lighting On After Sunset
description: After sunset, turn on the hallway light
trigger:
  - type: motion
    platform: device
    device_id: c9244e26b3cff0472c6eb4cefd1d3bea
    entity_id: binary_sensor.hallway
    domain: binary_sensor
condition:
  - condition: sun
    after: sunset
action:
  - type: turn_on
    device_id: 4d133c378a42f4889393deff2af147d7
    entity_id: light.ikea_of_sweden_tradfri_bulb_e27_w_opal_1000lm_b06fc6fe_level_on_off
    domain: light
    brightness_pct: 100
mode: single
`````````````````````

You cant use

condition:
  - condition: sun
    after: sunset

Because the condition resets at midnight
you should try

condition:
  - condition: sun
    below_horizon
1 Like

Check out my other thread, very similar to what your after.

1 Like

Another approach, use a condition of the sun being nearly below horizon, or just risen (at less tahn 4 degrees in this case)

condition:
  - condition: template
    value_template: '{{ state_attr(''sun.sun'', ''elevation'') < 4 }}'
2 Likes

Thanks I this has cracked it. And it was the easiest to implement.

How do you know what options are available for each device such as sun. I know it’s written in python but not sure about how to code it myself?

This explains why I thought I’d cracked it on a night.

Thanks