Light on at sunset and off at sunrise (in a single automation)

I’m trying to write a single automation that will turn on my lights at sunset and then turn them off at sunrise (super easy to do as two automation) but it’s not working. Can anyone help me diagnose why?

alias: Exterior - Lights Off / On
description: Turns off the lights during the day
trigger:
  - event: sunrise
    platform: sun
  - platform: sun
    event: sunset
condition: []
action:
  - choose:
      - conditions:
          - condition: sun
            after: sunrise
        sequence:
          - service: light.turn_off
            data: {}
            entity_id: light.exterior
      - conditions:
          - condition: sun
            after: sunset
        sequence:
          - scene: scene.exterior_christmas
    default: []
mode: single

No, but I can reduce it to 9 lines that will work…

alias: Exterior - Lights Off / On
trigger:
  platform: state
  entity_id: sun.sun
condition:
  - "{{ trigger.to_state.state != trigger.from_state.state }}" 
action:
  service: "homeassistant.turn_{{ 'on' if trigger.to_state.state == 'below_horizon' else 'off' }}"
  entity_id: "{{ 'scene.exterior_christmas' if trigger.to_state.state == 'below_horizon' else 'light.exterior' }}"
4 Likes

Well that’s quite a fancy way to do it! I’ll give that a shot. Thank you. I’m still learning a lot from templates and have been hesitant to use them.

Yeah, tbh it’s nearly bedtime here and I’m quite tired and I just stared at your choose statements until the letters all blurred into one and thought to myself “I’m never going to work out why that’s not working…”

So I just rewrote it :slightly_smiling_face:

I would like to encourage you to try NodeRed.

Here is example of what you need in its puriest form

[{"id":"63169d29.13e984","type":"switch","z":"bab2e35a.50c6f","name":"Sunset/Sunrise","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"sunsetStart","vt":"str"},{"t":"eq","v":"sunriseEnd","vt":"str"}],"checkall":"false","repair":false,"outputs":2,"x":480,"y":640,"wires":[["d57eb110.087f5"],["8a407bc6.38628"]]},{"id":"7b1d7fe4.8c958","type":"sun events","z":"bab2e35a.50c6f","testmode":false,"verbose":true,"topic":"","name":"","x":260,"y":640,"wires":[["63169d29.13e984"]]},{"id":"d57eb110.087f5","type":"api-call-service","z":"bab2e35a.50c6f","name":"lights on","server":"6cdd0bc8.b8e434","version":1,"debugenabled":false,"service_domain":"light","service":"turin_on","entityId":"light.all_lights","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":740,"y":620,"wires":[[]]},{"id":"8a407bc6.38628","type":"api-call-service","z":"bab2e35a.50c6f","name":"lights off","server":"6cdd0bc8.b8e434","version":1,"debugenabled":false,"service_domain":"light","service":"turin_off","entityId":"light.all_lights","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":740,"y":660,"wires":[[]]},{"id":"6cdd0bc8.b8e434","type":"server","name":"Home Assistant"}]
1 Like

The problem with the two conditions is that they’re not exclusive. After sunset will also be after sunrise on the same day. I suspect the Christmas Lights will never come on.
I tend to use the “above/below horizon” state because it’s less confusing.

I think I don’t understand the “sun” integration well enough (nor how dates / times work). I had assumed that midnight sort of “reset” things so sunset to midnight is “after sunset” and midnight to sunrise is “before sunrise”.

I’ve tried reading up on it but I don’t seem to be getting things clearly.

That may be true, but your conditions are both “after”: (after sunset and after sunrise) which means that any time after sunset will also be after sunrise for the same day.

Hmm, right…maybe that is my issue.

that’s why event based triggering works better for this use case. It’s not triggered after the event, but on the event instead.

Agree…with maxym

Here another example of Node-Red encouragement :wink:

[{"id":"740c0f3d.77954","type":"bigtimer","z":"cfb20f1a.f7461","outtopic":"","outpayload1":"turn_on","outpayload2":"turn_off","name":"Every day from Sunset until Sunrise","comment":"","lat":"51.831092","lon":"4.335460","starttime":"5004","endtime":"5003","starttime2":0,"endtime2":0,"startoff":"1","endoff":"-1","startoff2":0,"endoff2":0,"offs":0,"outtext1":"on","outtext2":"off","timeout":1440,"sun":true,"mon":true,"tue":true,"wed":true,"thu":true,"fri":true,"sat":true,"jan":true,"feb":true,"mar":true,"apr":true,"may":true,"jun":true,"jul":true,"aug":true,"sep":true,"oct":true,"nov":true,"dec":true,"day1":0,"month1":0,"day2":0,"month2":0,"day3":0,"month3":0,"day4":0,"month4":0,"day5":0,"month5":0,"day6":0,"month6":0,"day7":"","month7":"","day8":"","month8":"","day9":"","month9":"","day10":"","month10":"","day11":"","month11":"","day12":"","month12":"","d1":0,"w1":0,"d2":0,"w2":0,"d3":0,"w3":0,"d4":0,"w4":0,"d5":0,"w5":0,"d6":0,"w6":0,"xday1":0,"xmonth1":0,"xday2":0,"xmonth2":0,"xday3":0,"xmonth3":0,"xday4":0,"xmonth4":0,"xday5":0,"xmonth5":0,"xday6":0,"xmonth6":0,"xd1":0,"xw1":0,"xd2":0,"xw2":0,"xd3":0,"xw3":0,"xd4":0,"xw4":0,"xd5":0,"xw5":0,"xd6":0,"xw6":0,"suspend":false,"random":false,"randon1":false,"randoff1":false,"randon2":false,"randoff2":false,"repeat":false,"atstart":false,"odd":false,"even":false,"x":200,"y":160,"wires":[["506eed03.24cff4"],[],[]]},{"id":"b6ec534a.f94bc","type":"api-call-service","z":"cfb20f1a.f7461","name":"Light.outside front","server":"37d68302.2aabcc","version":1,"debugenabled":false,"service_domain":"light","service":"","entityId":"light.outside_front","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":730,"y":160,"wires":[[]]},{"id":"506eed03.24cff4","type":"function","z":"cfb20f1a.f7461","name":"light.outside_front","func":"if ( msg.payload.includes( \"turn_on\"))\n{\n    msg.payload = {\n        domain: \"light\",\n        service: \"turn_on\",\n        data: {\n            entity_id: \"light.outside_front\",\n            \"brightness_pct\": 75\n        }\n    };\n}\nelse\n{\n    msg.payload = {\n        domain: \"light\",\n        service: \"turn_off\",\n        data: {\n            entity_id: \"light.outside_front\"\n        }\n    };\n}\nreturn msg;\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":510,"y":160,"wires":[["b6ec534a.f94bc"]]},{"id":"37d68302.2aabcc","type":"server","name":"Home Assistant"}]

This trigger should work - the state of the sun changes once at sunset and once at sunrise, each should then trigger your automation.

trigger:
  - platform: state
    entity_id: sun.sun

I actually think it’s the “choose” that’s the issue because I can see that the automation is running at sunset and the lights are turning off correctly at sunrise. It’s just the lights at sunset that seem to be the trouble. I’m going to try setting the lights on as the default action.

1 Like

I sort of want to try node red but I like that (at the moment) I’m not using any external utilities. Is NR that must better?

NR is not an ‘external’ utility, it’s an 3 party solution for HA that becomes an integral part of it.

That said, if any automation works, it works.
As long as it does the job, none is better than the other.

But…the question is, which is easier, also that question i cannot answer, I believe it is easier for me, but many will disagree with that :thinking:

Why not give it a try, and uninstall it when it is not for you? :innocent:

I’m still thinking that after sunset is also after sunrise.
You might try swapping the two conditions so that after sunset would occur first and do the corresponding sequence. Then only if it were before sunset would it test if it were after sunrise.

1 Like

more node-red :slight_smile:
Capture

No. I never use NR and I don’t think I’m limited by not using it.

I tried it once but it was WAY more confusing, more complicated and less intuitive for the one relatively simple automation I tried that I just gave up on it.

But it is popular so it’s definitely a user preference type of thing.

While i think that about the scripted automation, it is confusing, more complicated and less intuitive…and it lacks overview :yum:

So as said, many ways lead to Rome, it is up to you to choose which way you take :wink:

@jjross
You could consider to use Scheduler integration, a project of mine, to accomplish your goal.