Turning lights on and off at sunset and sunrise

Based on a post by @123

description: "Turn on lights at sunset and off at sunrise"
trigger:
  - id: 'on'
    platform: sun
    event: sunset
  - id: 'off'
    platform: sun
    event: sunrise
action: 
  - service: 'light.turn_{{ trigger.id }}'    # If switch entity, 'switch.turn_{{ trigger.id }}'  
    target:
      entity_id: light.your_light             # Replace with the entity id of your light
mode: single

With offsets:

description: "Turn on lights before sunset and off after sunrise"
trigger:
  - id: 'on'
    platform: sun
    event: sunset
    offset: "-00:45:00"            # 45 min before sunset
  - id: 'off'
    platform: sun
    event: sunrise
    offset: "+00:45:00"            # 45 min after sunrise
action: 
  - service: 'light.turn_{{ trigger.id }}'    # If switch entity, 'switch.turn_{{ trigger.id }}'  
    target:
      entity_id: light.your_light             # Replace with the entity id of your light
mode: single

The Home Assistant Cookbook - Index.

3 Likes

And for Node-Red using the Big Timer node

image

[{"id":"8b79443e.ce44a8","type":"bigtimer","z":"69ff7662.5ff0d8","outtopic":"","outpayload1":"","outpayload2":"","name":"Porch Light","comment":"","lat":"","lon":"","starttime":"5004","endtime":"5000","starttime2":0,"endtime2":0,"startoff":"15","endoff":"0","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,"xday7":"","xmonth7":"","xday8":"","xmonth8":"","xday9":"","xmonth9":"","xday10":"","xmonth10":"","xday11":"","xmonth11":"","xday12":"","xmonth12":"","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":true,"odd":false,"even":false,"x":110,"y":380,"wires":[[],[],["d3cc00fd.c2d92"]]},{"id":"d3cc00fd.c2d92","type":"switch","z":"69ff7662.5ff0d8","name":"On-Off","property":"payload","propertyType":"msg","rules":[{"t":"eq","v":"ON","vt":"str"},{"t":"eq","v":"OFF","vt":"str"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":380,"wires":[["86758fbd.fb589"],["180e0e9c.574831"]]},{"id":"86758fbd.fb589","type":"api-call-service","z":"69ff7662.5ff0d8","name":"Porch Light ON","server":"","version":5,"debugenabled":false,"domain":"switch","service":"turn_on","areaId":[],"deviceId":[],"entityId":["switch.porchlightswitch"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"first","x":460,"y":340,"wires":[[]]},{"id":"180e0e9c.574831","type":"api-call-service","z":"69ff7662.5ff0d8","name":"Porch Light OFF","server":"","version":5,"debugenabled":false,"domain":"switch","service":"turn_off","areaId":[],"deviceId":[],"entityId":["switch.porchlightswitch"],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[],"queue":"first","x":460,"y":400,"wires":[[]]}]

And another Node-Red example: