Switching off lights at or before sundown

I have configured HA to switch on an outdoor light at sundown. Quite often the ambient light in the evening is low before sundown and i want HA to switch on the light either at sundown or at any moment during a period from one hour before sundown when the ambient light has a value of less than 4 w/m2 (my weather station provides this information). I have two questions, 1 - Is the logic in the following code correct? 2 - Can someone describe a good way to test code like this. Obviously its a bit hard to have to wait until the conditions required actually occur!

- id: '1617954555299'
  alias: Switch on lighting at sundown and also in low daylight one hour before sundown
  description: Switch on lights at sundown or up to 1 hour earlier if ambient light
    is less than 4 w/m2
  trigger:
    - platform: sun
      event: sunset
    - platform: sun
      event: sunset
      offset: -01:00:00
    - platform: state
      entity_id: sensor.lallerod_562_orust_solar_rad
      attribute: friendly_name
      from: 0 w/m2
      to: 4 w/m2
  condition:
  - condition: or
    conditions:
    - condition: sun
      after: sunset
    - condition: and
      conditions:
      - condition: sun
        after: sunset
        after_offset: '- 01:00:00'
      - condition: state
        entity_id: sensor.lallerod_562_orust_solar_rad
        state: < 4 w/m2
        attribute: friendly_name
  action:
    - type: turn_on
      device_id: 2c9ce9c1f0927fc846a2f4f508c88d4f
      entity_id: switch.ikea_vagguttag
      domain: switch
  mode: single

Please:

  1. Format your code
  2. Explain which steps you took to try to learn how to write automations (Doc, Forum, …) before asking for overly generic help and how it didn’t help you.

Hint: There are already bazillions automation samples in this forum to get you started

To format your code, paste it into your post as preformatted text (</> in the toolbar).

As you probably know, you can test automations by clicking on “Run actions”, but this doesn’t test triggers or conditions, unfortunately.

I have a similar automation (lights on/off at sunset/sunrise according to ambient light). I found the sunrise and sunset platforms to be more trouble than they were worth in this situation, and instead created an input_boolean flag to indicate night time. In the morning the lights off automation, which only runs if the night flag is “on”, is triggered by the ambient light and immediately switches the night flag off to stop it running again; vice versa in the evening. This works pretty well in most conditions.

A bit harsh, surely? Most of us would have to admit that we learned by trial and error. :woozy_face: And by asking questions.

Am I?
I had the feeling that points 1, 2, 3, 4, 11 of How to help us help you - or How to ask a good question haven’t been considered, here

Point is that I’m happy to help other if they tried to help themselves, first.

entity_id: sensor.lallerod_562_orust_solar_rad
attribute: friendly_name  <-----   I dont think the attribute "friendly_name" will have the light readings.  
from: 0 w/m2  <----  I dont think this should have units.  should just be a number.
to: 4 w/m2 <----- no units
condition:

I am not worried by Chris comments, good to get feed back and know how to pose a question in future. However I do feel that the message should have been sent privately to me. Chris comment could well inhibit other readers from answering my queries, however incorrectly they might have been formatted. Even more constructive would have been to give me an answer as well, I assume that in his position as moderator he would have no difficulty pointing me in the right direction. Asking questions I think is the best way forward, why should we try to re-invent the wheel?

1 Like
  1. I’m not a moderator
  2. I’m forced to notice you still haven’t edited your post to format your code.
  3. “Asking questions I think is the best way forward”. Sure, but only after RTFM. That’s my opinion, however.

If an actual moderator thinks I’m out-of-line, here, I’ll be happy to delete my posts.

Let’s be nice to each other, yes a piece of code reads better if it is in the right format (@Peter_G I did it for you). End of discussion and let’s continue on-topic and help him with his question :wink:

2 Likes

Agree, get rid of the units, and get rid of the “attribute: friendly_name” it’s not doing anything.

When you have multiple trigger conditions they are all “or” conditions and the first one that is “true” will trigger the automation. In this case having both “event: sunset” and “event: sunset, offset” will fire the automation at BOTH times, so you need to incorporate that into your thinking.

When I’m building automations, it’s sometimes hard to know if something should be a trigger or something should be a condition, or some combination. My recommendation is to first write out what you want to happen using your native language. In this case, it seems you want the following:

“Starting at an hour before sundown, turn on the switch either AT sundown or whenever the ambient light is below 4 w/m2”.

So, the TRIGGER is “an hour before sundown”.
The conditions are “sundown” OR “ambient light below 4 w/m2”.

The trick is that the trigger will only happen once, but the conditions could take up to an hour for one of them to happen. In this case, you could leverage the relatively new “wait_for_trigger” action. I’ve also added an “or” trigger to make sure it’ll always turn off at sunset, regardless of the ambient light. If you remove the sunset trigger then it’ll just wait until the light is below 4, even if that happens AFTER sunset.

Note: I haven’t tested this at all, it’s my best guess, but hopefully it’ll get you down the path:

- id: '1617954555299'
  alias: Switch on lighting at sundown and also in low daylight one hour before sundown
  description: Switch on lights at sundown or up to 1 hour earlier if ambient light
    is less than 4 w/m2
  trigger:
    - platform: sun
      event: sunset
      offset: -01:00:00
    - platform: sun
      event: sunset
  condition: []
  action:
  - wait_for_trigger:
    - platform: state
      entity_id: sensor.lallerod_562_orust_solar_rad
      below: 4
    continue_on_timeout: false
  - service: homeassistant.turn_on
    target:
      entity_id: switch.ikea_vagguttag
  mode: single

Thank you! :+1: :+1: