Trigger lights ON based on sunset and cloud coverage

So my main floor inside lights currently turn on 15 minutes before sunset, which is fine when the sun is out and shining but when there is cloud coverage, it’s dim inside and the lights aren’t on yet. So I thought about using OpenWeatheMap Cloud coverage (which is a percentage) to adjust that offset. Here’s my current Automation, how would you adjust the offset so it varies from -45 minutes to -15 minutes (100% cloud coverage to 0% cloud coverage)?

  alias: At sun set
  description: Turn on the inside light on the main floor
  trigger:
  - event: sunset
    offset: -0:15:00
    platform: sun
  condition: []
  action:
  - scene: scene.main_floor_at_sunset

Thanks.

Might have something working… Needs to figure out how to implement it into the offset field of the trigger

{% set cloudOffset = "-0:" + ((state_attr("weather.openweathermap", "cloud_coverage") / 100 * 30 + 15) | int | string) + ":00" %}
{{ cloudOffset }}

Whelp, this isn’t working :frowning:

    offset: "00:{{ ((state_attr('weather.openweathermap', 'cloud_coverage') / 100 * 30 + 15) | int | string) }}:00"

I get

Error:offset 00:{{ ((state_attr('weather.openweathermap', 'cloud_coverage') / 100 * 30 + 15) | int | string) }}:00 should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F' for dictionary value @ data['offset']. Got None.

I can’t figure out the syntax… help?

The problem with this premise is that cloud cover can increase the light level.

e.g. sun low to horizon and reflecting from the under side of the clouds.

Buy or build yourself a light level sensor. They are cheap.

2 Likes

If you have a weather station, you could try using the solar radiation as a trigger. That acts as a reasonable proxy for cloud cover in my use case to trigger a roller blind.

A long, long time ago (over 30 years), I added a photocell on the celing of my covered porch (so no direct sunlight) connected to an opamp that would trigger a relay when a voltage threshold was reached. That opamp is long gone, replaced by an automation but that photocell is still there. I could repurpose it, but for now, using OWM would do a ‘good enough’ job.

Yeah, I do. It’s a Acurite 5-in-1 weather station so it lacks that sensor. It does have a solar panel (two, actually) but it’s just use to power a fan that ‘cools down’ the temperature sensor when hit by the sun so it gives an accurate outside temperature.

For now, all I need is fixing that automation so it would work relatively well, better than it currently does, at least.

BTW, I also tried the following sensor in configuration.yaml

sensor:
  - platform: template
    sensors:
      sunset_offset:
        value_template: "{{ '-0:' + ((state_attr('weather.openweathermap', 'cloud_coverage') / 100 * 30 + 15) | int | string) + ':00' }}"

Which according the the developer’s state tab is working well


but can’t figure out how to use it (if even possible) in the offset field in my automation (I get the same error as above) :frowning: I don’t meddle with Home Assistant often enough to remember from time to time how I did things lol.

Well, the following didn’t produce any error… Not sure if it’s working though. We’ll see this afternoon (sun sets at arounf 4:30pm).

  trigger:
  - event: sunset
    offset: '"{{ state_attr(''sensor.sunset_offset'',''offset'') }}"'
    platform: sun

TBC

Edit: Nope, didn’t work

Based on my test, the offset option doesn’t support templates. That’s why the error message reports the value is not in HH:MM or HH:MM:SS.F format.


I used the Automation Editor to create a new automation with a Sun Trigger and with a very simple template in its offset option. Upon attempting to save the new automation, it reported the following error message.

Message malformed: offset {{00:15:00}} should be format 'HH:MM', 'HH:MM:SS' or 'HH:MM:SS.F' for dictionary value @ data['offset']

Thanks. I guess I’ll try to get the sunset time, subtract the offset from it and use that, with a real offset set to 0. Weird that it can’t be templated. It’s such a obvious one that could use a variable…

Another approach is to use a Sun Elevation Trigger … which is just a Numeric State Trigger that monitors the sun’s elevation above/below the horizon.

  triggers:
    - trigger: numeric_state
      entity_id: sun.sun
      attribute: elevation
      below: sensor.sunset_offset

A Numeric State Trigger’s below (and above) option supports a hard-coded elevation value or the entity_id of a sensor that reports an elevation value. You can make a Template Sensor that calculates an appropriate elevation based on cloud coverage.

Hi, thanks for the hint. Using this site

I figured the sun’s elevation is 5.69 degrees 45 minutes before sunset and 1.33 degrees 15 minutes before sunset, so a diff of 4.36 degrees.

So I created this sensor

  - platform: template
    sensors:
      sunset_elevation:
        value_template: "{{ state_attr('weather.openweathermap', 'cloud_coverage') / 100 * 4.36 + 1.33 }}"

That I’m using in this automation:

  triggers:
    - trigger: numeric_state
      entity_id: sun.sun
      attribute: elevation
      below: sensor.sunset_elevation
  condition:
  - condition: state
    entity_id: light.lumiere_evier
    state: 'off'

The condition might be unnecessary but it’s to make sure the automation doesn’t fire again if the cloud condition has changed and the lights have been dimmed to a value other than the default for the scene.

We’ll see at next sunset if it works as expected.

Thanks.

EDIT: It works!

1 Like