Automation to open close blinds based on illuminance and around sunrise/sunset

Hi, I am a noobie when it comes to HA and still learning the ropes. That said, I have added all of my sensors and done a fair bit of automation already but am really struggling with this one. Searches for similar automation have come up short so looking for help here.

Here is what I wish to do
Around sunrise (maybe 30 mins before to an hour after) and if my sensor shows the light is above a certain lux level, I want to open my blinds. Since the illuminance value will not happen at a fixed time, I wanted a 2 hour window around sunrise.
Similarly, I want to do the same around sunset. When the lux drops below a number, I want it to close.

Here is the automation code

alias: open blinds around sunrise
description: ""
trigger:
  - type: illuminance
    platform: device
    device_id: 166c72c5466b7ead2fa823675848ab00
    entity_id: 493b529fe48dda774014ef5c86af45f8
    domain: sensor
    above: 450
condition:
  - condition: sun
    before: sunrise
    before_offset: "-00:30"
    after: sunrise
    after_offset: "+01:30"
action:
  - device_id: a107c63329961c0649e1d13ad2003df7
    domain: cover
    entity_id: 43874310c9592bb07ad623b2ee7b009c
    type: open
  - device_id: e06dffb21b30dc2659d169eefba503ca
    domain: cover
    entity_id: 50d4766d3402c82b82b01f7f92caee59
    type: open
mode: single

Here is the trace

{
  "trace": {
    "last_step": "condition/0",
    "run_id": "afb1b804695e88ff42f51b9801fa01a6",
    "state": "stopped",
    "script_execution": "failed_conditions",
    "timestamp": {
      "start": "2024-01-10T17:42:00.677285+00:00",
      "finish": "2024-01-10T17:42:00.677839+00:00"
    },
    "domain": "automation",
    "item_id": "1704868212751",
    "trigger": "numeric state of sensor.eve_motion_illuminance_illuminance",
    "trace": {
      "trigger/0": [
        {
          "path": "trigger/0",
          "timestamp": "2024-01-10T17:42:00.677348+00:00",
          "changed_variables": {
            "this": {
              "entity_id": "automation.open_blinds_around_sunrise",
              "state": "on",
              "attributes": {
                "id": "1704868212751",
                "last_triggered": null,
                "mode": "single",
                "current": 0,
                "friendly_name": "open blinds around sunrise"
              },
              "last_changed": "2024-01-10T11:50:57.108957+00:00",
              "last_updated": "2024-01-10T11:50:57.108957+00:00",
              "context": {
                "id": "01HKSKC5AMHR4XWBGSMY7HWRDS",
                "parent_id": null,
                "user_id": null
              }
            },
            "trigger": {
              "id": "0",
              "idx": "0",
              "alias": null,
              "platform": "device",
              "entity_id": "sensor.eve_motion_illuminance_illuminance",
              "below": null,
              "above": 450,
              "from_state": {
                "entity_id": "sensor.eve_motion_illuminance_illuminance",
                "state": "432.7",
                "attributes": {
                  "state_class": "measurement",
                  "unit_of_measurement": "lx",
                  "device_class": "illuminance",
                  "friendly_name": "Eve motion/illuminance Illuminance"
                },
                "last_changed": "2024-01-10T17:35:30.698995+00:00",
                "last_updated": "2024-01-10T17:35:30.698995+00:00",
                "context": {
                  "id": "01HKT732CAR1YEYP1KC9E9ZTFX",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "to_state": {
                "entity_id": "sensor.eve_motion_illuminance_illuminance",
                "state": "503.7",
                "attributes": {
                  "state_class": "measurement",
                  "unit_of_measurement": "lx",
                  "device_class": "illuminance",
                  "friendly_name": "Eve motion/illuminance Illuminance"
                },
                "last_changed": "2024-01-10T17:42:00.676847+00:00",
                "last_updated": "2024-01-10T17:42:00.676847+00:00",
                "context": {
                  "id": "01HKT7EZ74FRD0ZTEM7FPVZMPG",
                  "parent_id": null,
                  "user_id": null
                }
              },
              "for": null,
              "description": "numeric state of sensor.eve_motion_illuminance_illuminance"
            }
          }
        }
      ],
      "condition/0": [
        {
          "path": "condition/0",
          "timestamp": "2024-01-10T17:42:00.677402+00:00",
          "result": {
            "wanted_time_before": "2024-01-10T14:53:18.735798+00:00",
            "result": false
          }
        }
      ]
    },
    "config": {
      "id": "1704868212751",
      "alias": "open blinds around sunrise",
      "description": "",
      "trigger": [
        {
          "type": "illuminance",
          "platform": "device",
          "device_id": "166c72c5466b7ead2fa823675848ab00",
          "entity_id": "493b529fe48dda774014ef5c86af45f8",
          "domain": "sensor",
          "above": 450
        }
      ],
      "condition": [
        {
          "condition": "sun",
          "before": "sunrise",
          "before_offset": "-00:30",
          "after": "sunrise",
          "after_offset": "+01:30"
        }
      ],
      "action": [
        {
          "device_id": "a107c63329961c0649e1d13ad2003df7",
          "domain": "cover",
          "entity_id": "43874310c9592bb07ad623b2ee7b009c",
          "type": "open"
        },
        {
          "device_id": "e06dffb21b30dc2659d169eefba503ca",
          "domain": "cover",
          "entity_id": "50d4766d3402c82b82b01f7f92caee59",
          "type": "open"
        }
      ],
      "mode": "single"
    },
    "blueprint_inputs": null,
    "context": {
      "id": "01HKT7EZ75MFACNV8RP7PDPB4Q",
      "parent_id": "01HKT7EZ74FRD0ZTEM7FPVZMPG",
      "user_id": null
    }
  },
  "logbookEntries": []
}

Can someone help me figure out what I am doing wrong. Thanks much for your help

Quick question: why the “around sunrise/set” window.

I ask because I have something similar and the window won’t make any difference.

More useful was to ensure the lux value had dipped/risen for a minimum time period to ensure “flashes” didn’t effect the automation (torchlight, headlights etc).

You are right @jchh. My main concern is once the window is open and then say around noon the light levels drop below xx, then I dont want the window to close… I want it to close if it does so say starting after 30 mins before sunset

Cool. Its late here so I’ll post Sao e yaml tomorrow for you.

As far as I know, defining sunrise twice will not work.
To state the docs:

" The sun condition can also test if the sun has already set or risen when a trigger occurs. The before and after keys can only be set to sunset or sunrise. They have a corresponding optional offset value (before_offset, after_offset) that can be added, similar to the sun trigger.

Note that if only before key is used, the condition will be true from midnight until sunrise/sunset. If only after key is used, the condition will be true from sunset/sunrise until midnight. If both before: sunrise and after: sunset keys are used, the condition will be true from midnight until sunrise and from sunset until midnight. If both after: sunrise and before: sunset keys are used, the condition will be true from sunrise until sunset."

You would need to set up an or condition with two sun conditions, one with the negative and the other with the positive offset. And use before sunrise for the second condition as well, as otherwise it will be true until midnight (after sunrise). So you want to offset the before sunrise time positively.

Also, the format is wrong if you want a 30-minute delay

This is the revised code for the two conditions nested inside an or condition:

condition: or
conditions:
  - condition: sun
    before: sunrise
    before_offset: "-00:30:00"
  - condition: sun
    before: sunrise
    before_offset: "+01:30:00"

I can’t test this now, but it should be true from 30 minutes before sunrise until 1.5 hours after.

You could also use the Sun Elevation, that way you wouldn’t have to rely on lux readings as the elevation is a known variable. Except of course if it is relevant to indoor lights etc.

This in an example from the docs for elevation:

condition:
  condition: and  # 'twilight' condition: dusk and dawn, in typical locations
  conditions:
    - condition: template
      value_template: "{{ state_attr('sun.sun', 'elevation') < 0 }}"
    - condition: template
      value_template: "{{ state_attr('sun.sun', 'elevation') > -6 }}"

The reason I want to use lux levels is because I don’t want to open the blinds if its still dark outside. If i rely on Sun elevation alone, but the sun is completely behind dark clouds, the blinds shouldn’t open till it reaches some semblance of it being day.

When I was with Smartthings, I had a similar routine and it worked flawlessly year round. That said, I am open to ideas if there are better solutions on how to achieve something similar.

I may be reading this wrong, but (I think) The English that is equivalent to is:

EITHER before (sunrise-30m) OR after (sunrise+90m)

which is anytime outside of the window (I think) the OP wants. (I think (the OP) wants::

BOTH after (sunrise-30m) AND before (sunrise+90m)

This may work…

conditions:
  - condition: sun
    after: sunrise             # AFTER sunrise
    after_offset: "-30:00"     # -30m (double negative)
  - condition: sun             # AND 
    before: sunrise            # BEFORE sunrise
    before_offset: "-01:30:00" # +90m (double negative)

…but, tbh, it needs testing as not 100% I have it the right way round either!

You’re right, the sun conditions I provided are wrong, in fact I looked at the Sun Condition Documentation again, and now I’m pretty certain that this would result in being true from midnight until 1.5 hours after the sun has risen. (So not close to what OP wanted at all)

As far as I can tell now, your conditions would never result in being true, as they are checked with AND Logic, and the first is after Sunrise (-30 minutes) until midnight, and the second from midnight until sunrise (1.5Hours +). However, I can’t say that for sure, so don’t take this for the truth.

I am referring to this image from the docs.
Sun Condition Illustration

@periferral If I have understood now correctly that you want the condition to render true FROM -30 minutes before Sunrise UNTIL 1.5 hours after Sunrise, then I have this new condition that I even tested just now:

condition: sun
before: sunrise
after: sunrise
before_offset: "01:30:00"
after_offset: "-00:30:00"

This is the result:

I have tested it with sunset because that’s the only thing I had just now, but it should work the same way for sunrise, obviously.

Furthermore, I have made this mockup graphic to illustrate what I think all these conditions do (green is if the condition is true):

This is in no way a guarantee, as I find this condition quite confusing myself.

You said @periferral that the reason you use the lux sensor is primarily because of clouds overcasting the sun, so in total giving off less sunlight.
I don’t think you can substitute this so easily, except maybe if you combine the known position and multiply it with a cloud coverage factor from a reliable (and fast) weather service. But I think in this case, if the Lux Sensor gives reliable enough readings, it should be better than this solution.

I must admit the above thread is TLDR, and I did not quite get your graph. But seeing your question and your first attempt, I see some misconceptions. A trigger is something happening (something becoming a certain way), a condition is something that needs to be a certain way.

The automation only evaluates when the trigger happens. What you stated is a lux level trigger, so only at the exact moment when the lux level changes from below 450 to above, the conditions are checked. If it happens at the wrong time, nothing is checked again until light levels drop below 450 and rise above it again.

So this won’t work on its own. You also need a trigger to fire at the start of the period, just in case the light level already is above 450. If the trigger at some time before sunrise is also firing, then you also need a condition to check for the light level being above 450.

So if multiple things need to be a certain way for the automation to run, the pattern for the automation would be something like:

trigger:
  a happens
  b happens
  c happens
condition:
  a is the case
  b is the case
  c is the case
action:
  do something

In your case, you need two triggers: the light level rises above 450 or the time is half an hour before sunrise. You also need the conditions: the light level is above 450, the time is between half an hour before sunrise and an hour after sunrise.

There is no trigger for the end time (as you would expect from the general case), in this case it isn’t needed because you do not want to open covers anymore after that time.

You could also do it like this if you wanted to:

platform: time_pattern
minutes: /1

Or maybe better, trigger at the time the condition begins to be true

platform: sun
event: sunrise
offset: "-00:30:00"

Then have no Condition in the Condition Section, and use Wait for Trigger (Lux level) in the Action Section (with Timeout continue if you want to). After that, use the Sun Condition to check that it triggered within your desired timeframe:

action:
  - wait_for_trigger:
      - type: illuminance
        platform: device
        device_id: 166c72c5466b7ead2fa823675848ab00
        entity_id: 493b529fe48dda774014ef5c86af45f8
        domain: sensor
        above: 450
  - condition: sun
    before: sunrise
    after: sunrise
    before_offset: "01:30:00"
    after_offset: "-00:30:00"
  - device_id: a107c63329961c0649e1d13ad2003df7
    domain: cover
    entity_id: 43874310c9592bb07ad623b2ee7b009c
    type: open
  - device_id: e06dffb21b30dc2659d169eefba503ca
    domain: cover
    entity_id: 50d4766d3402c82b82b01f7f92caee59
    type: open

This assumes the Lux Level never is above 450 already at the start of the condition and doesn’t go below in this timeframe.
If it is basically totally random, you should use the Time Pattern Trigger every X Minutes.

Thanks for all the suggestions. I will try this tomorrow morning.

Thank you to all who replied on this thread. Learned a lot from you all. After a frustrating couple of days with SkyConnect losing the ability to work with my zigbee devices and moving my setup to Sonoff, I finally got a successful close blinds automation today. I will wait for tomorrow am to see if the open also works but I am optimistic. I also added a trigger timeout which I think is great because it will open and close regardless at the end of the timeout period (I assume).

I am including my YAML below

alias: Close blinds around sunset
description: ""
trigger:
  - platform: sun
    event: sunset
    offset: "-01:00"
condition: []
action:
  - wait_for_trigger:
      - type: illuminance
        platform: device
        device_id: 166c72c5466b7ead2fa823675848ab00
        entity_id: 493b529fe48dda774014ef5c86af45f8
        domain: sensor
        below: 300
    timeout:
      hours: 1
      minutes: 30
      seconds: 0
      milliseconds: 0
  - device_id: a107c63329961c0649e1d13ad2003df7
    domain: cover
    entity_id: 43874310c9592bb07ad623b2ee7b009c
    type: set_position
    position: 40
  - device_id: e06dffb21b30dc2659d169eefba503ca
    domain: cover
    entity_id: 50d4766d3402c82b82b01f7f92caee59
    type: set_position
    position: 40
mode: single

Hi @periferral ,

does your automation work as wished for multiple days?

Thanks
Dominik

I am looking to do this as well, what illumination sensor did you purchase/use

Sorry for not responding sooner. Yes, my automation is now working. I have changed it a few times now and while not perfect, it is doing what it is supposed to.

I am using the eve motion sensor for illumination

alias: Open blinds around sunrise
description: ""
mode: single
triggers:
  - event: sunrise
    offset: "-00:30"
    trigger: sun
conditions: []
actions:
  - wait_for_trigger:
      - type: illuminance
        device_id: 166c72c5466b7ead2fa823675848ab00
        entity_id: ff0af024e3801989f859608bad094d2d
        domain: sensor
        above: 300
        trigger: device
    timeout:
      hours: 1
      minutes: 15
      seconds: 0
      milliseconds: 0
  - device_id: a107c63329961c0649e1d13ad2003df7
    domain: cover
    entity_id: 43874310c9592bb07ad623b2ee7b009c
    type: open
  - device_id: e06dffb21b30dc2659d169eefba503ca
    domain: cover
    entity_id: 50d4766d3402c82b82b01f7f92caee59
    type: open
  - device_id: ab058e490bb0a4ceced8d4389c5577d4
    domain: cover
    entity_id: de38d27b4368ce258f4e9790b82d0683
    type: open

Thanks for the update and yaml code. How long does the Eve sensor stay powered before you need to change the batteries? I"m using one from Tuya and it eats batteries every 3 months or so

I have 3 eve motion sensors. The one I use only for illuminance lasts for quite some time. I can’t remember the last time I replaced the batteries but it has been around 3 months and it is still at 30% battery.

However, I use another on my front door for motion sensing and that depletes the battery much faster.

I just purchased the Eve motion sensor but cannot figure out how to get it integrated into HA. Do you have a link with any instructions? Thanks in advance