Chicken eggs!

I’m trying to get my chickens to keep laying as winter comes. Optimally they need 14-15 hours of light so I have an egg light in the coop controlled by a WiFi switch. I have this automation working great:

- id: '1569453255440'
  alias: Egg light on for 4 hours before sunrise
  trigger:
  - event: sunrise
    offset: -04:00:00
    platform: sun
  condition: []
  action:
  - data:
      entity_id: switch.4857044184f3eb916030_2
    service: switch.turn_on
  - delay: 05:00:00
  - data:
      entity_id: switch.4857044184f3eb916030_2
    service: switch.turn_off

And I’ve added the enhanced sun component and now I know the length of my day in hours:
states(“sensor.daylight_hr”)

The number of hours it should be on:
Egglight.Time.on=15 - states(“sensor.daylight_hr”)

I get the right daylight value in the template editor, I just don’t know how to apply the math in the ‘delay’ section where my switch turn on… help!

Jeff

1 Like

Sadly, an automation setup for this can be a bit complicated in Home Assistant.

First of all, the easy way. If you know you want the light on for 15 hours, you could just change the delay to 15 hours and be done. However, using a delay this long in Home Assistant isn’t great. When Home Assistant is restarted all automations loose track of their “place”, meaning your timer will not work if Home Assistant is restarted while it’s running. Also, this means you’d be running the light even when the sun is up which is not needed.

What is sounds like you really want is a light that turns on at ( sunrise - 15 hours + sensor.daylight_hr ), and then turns off at sunrise. Maybe even add an hour or two of overlap so you can be sure the sun is nice and bright before your light goes off.

To do this, you need two automations. One that turns the light off an hour after sunrise. That one is easy. The second automation needs a time_pattern trigger set to run every minute or every 30 minutes if you don’t care about exact timing. Then you need a condition on that automation that compares the current time to the formula for your on time. And, in order to do that, you’ll need a time sensor.

A search for “time template trigger” in these forums will lead you to several examples that might get you on your way. However, for me, this is MUCH easier to do in something like AppDaemon, a service that connects to Home Assistant and allows for much easier automations and the full power of Python.

You can call a delay with a template in a script:

sequence:
    - delay: '00:{{ states.YOURSTATE.state | int }}:00'

Use your template editor to find the right calculation for it.

Then just call the script from your automation.

no you miss his point, he needs 15 hours of light
Which is what he gets an hour after sun up
So, 15 hours - the length of the day + 2 hours - the 4 hours he already had before sunup = the length of time he needs in a second automation starting 1 hour before sunset.
Daylight is currently free, though amazon may start charging for it soon.
Check my addition (it isn’t math as no algebra is involved)

Don’t waste his bulb (life) and electricty for no reason.
If he needs it for (say) heat, then that’s a different automation

so you have daylight hours {{ (states("sensor.daylight_hr") | int ) }} and a constant of how much light you need (15). you should also get the time the egg light is on. you could get that with history_stats:

sensor:
  - platform: history_stats
    name: Lamp ON today
    entity_id: switch.4857044184f3eb916030_2
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

you can get the hours via {{ (states("sensor.lamp_on_today.state") | default(0) | int ) }}.
with this you could:

  1. automation 1: turn on the lamp four hours before sunrise
  2. automation 2: turn off the lamp at sunrise
  3. automation 3: check at sunset if morning LED light and SUN light (sum light) add up to 15 hours
    {% if (states("sensor.daylight_hr") | default(0) | int ) + (states("sensor.lamp_on_today.state") | default(0) | int ) . < 15 %} less than 15 hours light {% end if %}
  4. if not: turn on the LED
  5. automation 4: run an automation every 10 minutes with the conditions: is after sunset && sum light > 15 h && is LED on to turn off the LED.

No it’s a given amount for each day (daylight hours) between sunrise and sunset
He wants and hour overlap with the sunrise so presumably another hour with the sunset
So sunset offset -1 hours = your trigger
the delay will instantly expire if a negative so that’s okay
15 - the daylight (lets say 9 cos winter is approaching (sorry any southern hemisphere dwellers) - the four he had before sunrise (even though his delay was 5 hours)
so this is 15-9-4+1 for the hour offset before sunset = 1 hour (= at sunset)
but if the day is 8 then = 2 hours (= 1 hour after sunset)
if 7 then 2 hours after sunset
if 10 then the delay is 0
if 11 then it’s -1 so same as zero
(so my adding up was wrong but I was at a dog training session :smile: )
Why run something every 10 minutes when you can be precise about it ?
The whole hour bits are just to convey the idea, you will need to set these as minutes and 300 minutes is a valid delay ie ‘00:300:00’
You don’t need to ask how long the lamp has been on, you know, 4 hours.
I assume he turns it on for an hour to overlap (so don’t turn off at sunrise, he specifies 1 more hour) to allow the chickens to recognise it’s day outside and move out leisurely and the sam on the ‘call back’ into the roost at nightfall.
There’s something funny though I don’t see daylight_hr as an attribute of sun.sun - so where did you get that ?
The only way round that I can see is state_attr(‘sun.sun’, ‘next_setting’) - state_attr(‘sun.sun’, ‘next_rising’)
This will be wrong, as past sunrise you will be calculating on tomorrow’s sunrise but I don’t think the chickens will complain

This is great. Yes, I want to overlap and turn on an hour before sunrise and give the little guys 15 +/- hours of light. Each day. Today the light is on for 4 hours bc I have 11 hrs of light. In December it will be more like 6 hours due to short days. And yes, The egglight warmer is a different automation as are the sprinklers (Cooler) and automatic door closers. This project has been massive fun!

I’m using this sun enhancement:

Here is my suggestion…

  1. Turn on the light and an input boolean one hour before sunrise. At the same time (all done with the same automation) start a 15 hour timer.

  2. Turn the light off at sunrise (or there about) but leave the input boolean on. The timer will continue to run.

  3. Turn the light back on at sunset with the condition that the input boolean is still on.

  4. After the timer expires turn off both the light and the input boolean.

You don’t need to care how long the day is because the light will only be on for one hour before sunrise no matter what time of year it is. The light will always be off when it is daylight outside. And the light will turn on again at sunset and will always go off when there has been either sunlight or lamp light for at least 15 hours based on the timer/input boolean combination.

1 Like

Thanks finity
Jazzmonger, see, sometimes a new pair of eyes can help reduce a problem to one really simple timer.
Okay the starts a bit off but the meat is all there.
Can you write this from here?

Yes the solution by @finity does rather cut through the complications :wink:

However as @swiftlyfalling points out, using timers isn’t safe if there is a possibility HA will restart while it is running.

I got around this problem with my House Occupancy Guest Timers by populating an input_datetime with the start time and and input_number with the duration.

1 Like

On the off chance that there is a restart on a day that it could effect the operation of the timer you could always just put in a “failsafe” time that you want the light to turn off no matter how long it’s been on.

It doesn’t sound like having a longer time is super critical. So you could just write another automation that turns off the light at say 10 pm.

Or as @klogg suggests set an input datetime when the input boolean turns on (or the timer starts) and set the off time with a calculation of 15 hours later and trigger the off when now() equals the off input datetime.

How complicated you make it is determined by how exact the time is that you want the light to be on or if your chickens can tolerate the occasional HA restart while the timer is running without disrupting their schedule.

1 Like

I can’t help but feel that we have come full circle back to @swiftlyfalling 's suggestion. My apologies to you sir. I also agree that relying on a timer of 15 hours is quite dodgy. Unless Jazzmonger has an HA instance just for his chickens.
I also think that keeping chickens up past their bedtime is just bad form.
So my new thoughts on this is four automations.

  1. The 2nd - Turn off the light at sunrise plus 1 hour
  2. The 3rd - Turn the light back on at sunset minus 1 hour
  3. The 4th - Turn the light off at the contents of an input_datetime
  4. The 1st - Is to turn the light on and calculate a time 15hrs from now and store that in the input_datetime

I’m not sure how to do that, but I will figure it out.
I will also figure out how to cope with a reboot at any time of the day
klogg, I don’t think an input_number will help you survive a reboot.
I’d be happy if you prove me wrong :smile:

This appears to be by far the easiest way to do this. These are just chickens. They need a few hours of artificial light calculated from how much daylight there is going to be today +/- an hour or so. And (very important) It needs to add light in the AM before the sun rises. Messing with bedtime light is a bad idea and has resulted in mountain lions eating chicken for dinner because they missed the automated door closing because there was “still” light in their coop.

Here’s my sensor

'{{ 15- (states.sensor.daylight_hr.state | float) +1  }}:00:00'

The plus one hour is just to be safe and add a little buffer the output is

'4.07:00:00'

So this is my time for the switch to be on. now I need it to go on at:

sunrise - 4.07:00:00

and then turn off in 4.07:00 hours

Now I need to use it somewhere, possibly somehow in a script (?) if that’s the best place. I’m still not sure as this is all new to me. I feel like all the pieces are here… they’re just all over my floor.

Jeff

So…

I thought you always wanted it to go on 1 hour before sunrise and on for 15 hours?

Tho, now it looks like before sunrise is OK no matter how long before but sunset is the hard stop?

That changes things a bit (…a lot…).

And the way you calculated things you can’t use the result in that format (4.07:00) for any time calculation.

you will have to use the time of the next sunset and convert that to a timestamp. Then subtract 54000 seconds from it then convert that new result back into a correctly formatted time.

I was working on what I thought was the original desired result and had something worked out but now that won’t work in light of the corrected information.

If nobody else pops in with anything I will try to get something worked out for you in a while.

Eh the very first post says 4 hours before sunrise, what changed ?

Problem statement:
Chickens need 15 hours of light in total For maximum egg production. That is a combination of both artificial light and natural sunlight. If artificial light is added, it needs to be added in the morning.

My solution needs to take both into account and add artificial light in the morning darkness before they wake up. Then natural sunlight takes over after sunrise and the artificial light is then turned off. This changes with the seasons. Winter will add the most light, Summer will add the least amount of light

Does that help?cmon guys! My wife loves fresh eggs and needs to eat a lot of them! Happy wife, happy life.

Getting this working will justify all the expenditures I’ve made (to her) and time I invest in this whole solution.

Jeff

Hey… Maybe I’ll get some cheap smart plugs and do the same!

What sort of daily egg consumption are we talking about here?

Yeah but you’ll still need the automations to make sure they work properly!

pnbruckner’s enhanced Sun component might be useful for this application. In addition to reporting today’s sunrise it can report the amount of daylight.

Assuming I’ve understood the requirement correctly, the arithmetic involved would simply be to subtract the available daylight hours from 15. The result would represent the offset from sunrise. For example, if there are 11.5 hours of daylight today then 15 - 11.5 = 3.5 hours. The lights should be turned on 3.5 hours prior to sunrise.