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

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.

Very cool. I’ll check this out.

@neliss, how do I choose sunset and sunrise tiggers?

I wrote it in a manual here: GitHub - nielsfaber/scheduler-card: HA Lovelace card for control of scheduler entities

In programming like this we always do things in reverse.
So the first condition should be after SUNSET - because that’s only true once. Then you do after SUNRISE - that way after sunset will match first (after sunset) and be executed, whereas as other people have pointed out - the way you have it once sunrise has happened, it will always match that first - even at sunset.

EDIT: But using the sun elevation is a better way of doing it - because sunset can be already be quite dark in the winter but quite light in the summer.

I have the sun integration but I don’t see the sun moon button.

You can achieve this easily in Standard HA Automations. Change the device and entity id as also domain if that’s light instead of switch in my case. After writing the code you can also see this in visual editor

description: Turn On/Off Garden Light
trigger:

  • platform: sun
    event: sunrise
    offset: 0
  • platform: sun
    event: sunset
    offset: 0
    condition:
    action:
  • choose:
    • conditions:
      • condition: sun
        after: sunrise
        sequence:
      • type: turn_off
        device_id: xxxx
        entity_id: xxxx
        domain: switch
    • conditions:
      • condition: sun
        after: sunset
        sequence:
      • type: turn_on
        device_id: xxxx
        entity_id: xxxx
        domain: switch
        mode: single

At least if you’re going to resurrect a 3 year old dead thread the least you could do is properly format your suggested code correctly.

:wink: