When to turn on porch lights

You can try creating this sensor and use it for your automation. The states change depending on your region.

I use it to close and open my curtains. You will need to adjust the elevation for your region.
The sensor has four states:

daylight
dawnlight
dusklight
nightlight

sensor.yaml

  - platform: template
    sensors:
      period_of_the_day:
        friendly_name: 'Period of the day'
        value_template: >-
          {% set elevation = state_attr('sun.sun', 'elevation') %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {%- if elevation <= -7 -%}
            night
          {%- elif -7 < elevation <= -1 -%}
            {{ 'dawn' if rising else 'dusk' }}
          {%- else -%}
            day
          {%- endif -%}light
        icon_template: >-
          {% set elevation = state_attr('sun.sun', 'elevation') %}
          {% set rising = state_attr('sun.sun', 'rising') %}
          {%- if elevation <= -7 -%}
            mdi:weather-night
          {%- elif -7 < elevation <= -1 -%}
            mdi:weather-sunset-{{ 'up' if rising else 'down' }}
          {% else %}
            mdi:weather-sunny
          {% endif %}

automation.yaml

- alias: 'Open Living Room Curtains Sunrise'
  trigger:
  - entity_id: sensor.period_of_the_day
    platform: state
    to:
     - 'dawnlight'
     - 'daylight'
  condition:
  - condition: template # Someone is home
    value_template: "{{ not is_state('input_select.home_mode', 'Away') }}"
  action:
  - service: cover.set_cover_position
    data:
      position: '{% if is_state("sensor.period_of_the_day", "dawnlight") %} 50 {% else %} 0 {% endif %}'
      entity_id: cover.fr_curtains
      
- alias: 'Close Living Room Curtains Sunset'
  trigger:
  - entity_id: sensor.period_of_the_day
    platform: state
    to:
      - 'dusklight'
      - 'nightlight'
  condition:
  - condition: template 
    value_template: '{{ states("cover.living_room_curtains") != "closed" or states("cover.computer_room_curtains") != "closed" }}'
  action:
  - service: cover.set_cover_position
    data:
      position: '{% if is_state("sensor.period_of_the_day", "dusklight") %} 50 {% else %} 100 {% endif %}'
      entity_id: cover.fr_curtains
2 Likes

How? Like I see you’re specifically looking for when the sun’s elevation is -7 to decide between night and dusk/dawn. How did you come up with that particular number for your region?

I also just do sunset -30 min. I just figured it was good enough. But this topic has piqued my interest, I have noticed it was turning on when it was still bright out before. And I’m always on the lookout for ways to optimize my automations :slight_smile:

EDIT: Duh, click the link Mike :person_facepalming: . Sounds like 6 degrees is the official differentiator but some trial and error will figure out what works best for your region, might be a bit higher or lower. Thanks!

It was more of a trial and error. Spend a few days of actually seeing how close the automation triggered to the actual light outside at the time.

2 Likes

Here is another I found awhile back when I want to turn on my living room lights at sunset but also depends on the cloud coverage outside. The sample uses Darksky which is now a paid subscription. Weatherbit is free.

is there a HA integration for that? I didn’t see it in the official list.

is it available for the US?

yes, it’s in HACS. You can following this project on how to install and setup.

That’s wonderful. From the sensor code, I don’t see anywhere to specify the region?

The region is where you are running HA. No need to set anything for that.

1 Like

I just have an event on sunset or sunrise and I add an offset for what I feel is comfortable for my area. In my case I like the lights on 30min BEFORE sunset.

alias: 'Lights: turn on outdoor lights when the sun sets'
trigger:
  - event: sunset
    offset: '-00:30:00'
    platform: sun
condition: []
action:
  - device_id: xxxxx
    domain: switch
    entity_id: switch.switch_3
    type: turn_on
  - device_id: xxxxx
    domain: switch
    entity_id: switch.switch_2
    type: turn_on
initial_state: 'on'
mode: single

More on the sun and offsets

1 Like

Configuration --> General

The time zone and location on the map set for Home will be used to figure out when sunset and sunrise are.

1 Like

Deploy a light sensor outside. Outdoor light during twilight time varies much more than I realized until comparing Lux readings against astronomical calculations (e.g. civil twilight). This is especially true if you’re in northern latitudes (I’m close to 48 degs N).

I use an XBee3 ZigBee module (which can do I2C) and a TSL2591 light sensor (it’s part of my weather station). Use a moving average of Lux readings to smooth variations and outliers as last light approaches.

Works perfectly. Sometimes DIY is the best choice :slight_smile:

1 Like

Exactly this was gonna be my suggestion. I originally did this with sunset ending up in the same problem where it would need changing. Then I tried using info from weather but was also not as accurate. The best solution I ended up with was a lux sensor. It is way more close to what is actually happening outside.

The offset of the sun is based on geographic location and the time of year. It should be accurate regardless of time of year and latitude that is the entire point as long as your “Home” is set correctly.

I am at 49 degrees N, and have been fine year round with 30min before sunset for a few years now even with our dark short overcast days in winter and long summer days.

I am curious how “spot on” you are trying to be I find that 30 min window somewhat covers any variance however I am trying to have the lights on BEFORE it gets dark.

I even use it with my Christmas lights and have them come on in the morning but ONLY if it is dark during the normal hours people get up for work.

I checked the sun elevation when I think it is best time to turn on the light, it is showing -36.6 for me. Will continue experiment this.

Sure yes I used that too and works fine. It is just curiosity and more fine optimization if possible.

Thanks for the suggestion. I have not tried any light sensor yet. May consider adding in the future.

an elevation of -36 is pretty much pitch black out, I think you are reading the wrong value

1 Like

I posted work I did developing a simple model in this thread:

I am still working on it, I need to get data now that the snow is gone, and in 2 days when it is raining to adapt the final formulas, then in the summer sort out my surface reflectivity value

I got it working exceptionally well during winter as long as it was not actively snowing, but I figure the adjusted compensation due to rain will be sufficient.

1 Like

I was wondering too. Seems not correct. I was reading elevation value from sun.sun. In config->general, I’ve set the correct time zone (-5 timezone)

From the history of this value, it seems it is based on UTC time? I’m in -5 timezone, the sun.sun elevation value was peak 38.66 6:55am(-5 timezone).

Is there any other settings I need to make in HASS?

quickly checked and this is amazing, using weather info instead of light sensor! will try it out. thanks!