pnbruckner / ha-sun2

Home Assistant Sun2 Sensor
The Unlicense
224 stars 20 forks source link

Add instructions for using sun2 sensors in automations #128

Open tensiondriven opened 2 months ago

tensiondriven commented 2 months ago

Hola! I love this package. So good.

I am trying to set up an automation to trigger at sunset. Currently, it isnt clear to me if I should:

Assuming there's no sensor that has the current sun's risingness/settingness (which is what the builtin Sun integration provides, e.g. trigger on sunset / trigger on sunrise), it would be great if the documentation (README.md) was updated with a little info on the best way to trigger automation based on sun state.

pnbruckner commented 1 month ago

Run an automation with trigger every second and ...

That is almost never the right approach to an automation.

I am trying to set up an automation to trigger at sunset. ... trigger on "nautical sunset changes"

By "nautical sunset" do you mean nautical dusk (i.e., sun is 12 degrees below the horizon)? So, which do you want the automation to trigger on - sunset or nautical dusk?

Assuming there's no sensor that has the current sun's risingness/settingness

The sun phase sensor has a rising attribute.

Probably the easiest way to trigger an automation at sunset is to define the elevation binary sensor with an elevation threshold of horizon, then trigger when it changes from on to off. If you want to trigger at nautical dusk instead, then define the sensor's elevation threshold to be -12 degrees.

Or if you use the sun phase sensor, then trigger when it changes from Day.

tensiondriven commented 1 month ago

Sounds good - thanks for the thorough and timely reply!

I know it's not typically best practice to trigger every second and then bail out based on a condition.. I'm curious if there's any real significant penalty to doing this, as it can make the logic easier to set up in some cases (this is a more general question, rather than something specific to sun2)

Re: sunset vs nautical dusk, I still need to play around with it to get a feel for what it's like outside at my location at those times.

Using a custom binary sensor makes a lot of sense. I think what I ended up doing was using "next nautical dusk" "changed" as my conditions, but that won't have the flexibility of offsetting to fine tune.

Going back to the original intent of opening the ticket - it would be great if the guidance you gave me was integrated into the readme at some point for the benefit of others.

Thanks again!

pnbruckner commented 1 month ago

I know it's not typically best practice to trigger every second and then bail out based on a condition.. I'm curious if there's any real significant penalty to doing this, as it can make the logic easier to set up in some cases

There is much more work for the system to do if you're firing an automation every second and then having it check conditions as opposed to just triggering on the event(s) you're interested in. Will you ever really notice the difference, though? Depends on how powerful your system is, how much other stuff you're asking it to do, etc., etc.

In my mind, it's more "logical" to define the trigger(s) based on the event(s) that should cause the automation's actions to run rather than waking up every second and asking, "are we there yet"? But then, everyone has their own way of looking at things, so do whatever makes the most sense to you. In programming, generally it's better to worry about functionality first, and then optimize if/when it's necessary.

Going back to the original intent of opening the ticket - it would be great if the guidance you gave me was integrated into the readme at some point for the benefit of others.

That's just one of many possible use cases. The docs are really meant to explain what the integration does, not necessary how it might be used. Obviously, some examples could be added, but I'm really not interested in doing that at this time.

voidzero commented 1 month ago

I'm also playing with this and I'm currently trying to figure out how to turn on a switch at golden hour in the evening. Could you give a quick explanation for that, please? Still finding my way with HA.

By the way - I enabled deCONZ, and noticed it mentions "nautial dusk" instead of "nautical dusk".

pnbruckner commented 1 month ago

I'm also playing with this and I'm currently trying to figure out how to turn on a switch at golden hour in the evening. Could you give a quick explanation for that, please? Still finding my way with HA.

Enable the phase sensor, then trigger on its golden_hour attribute changing to true while its rising attribute is false.

By the way - I enabled deCONZ, and noticed it mentions "nautial dusk" instead of "nautical dusk".

Good catch. That's a typo in the English translation file. I'll fix it. Thanks!