Chicken eggs!

Where I live, the longest day of the year provides over 15 hours of daylight. So simply offsetting 15 hours from sunrise sunset would mean the light is turned on after sunrise.

Perhaps the ‘turn on coop light’ automation needs a condition that ensures daylight hours are less than 15. If greater than 15, then the light doesn’t need to turn on.

1 Like

Good catch. I’ve never looked to see how long daylight is in the summer here.

so the modified automation would be:

alias: turn on coop light
  platform: template
  value_template: "{{ states.sensor.time.state == (as_timestamp(states.sensor.sunset.state) | int - 54000) | timestamp_custom('%H:%M') }}"
  condition: template
  value_template: "{{states.sensor.daylight.state | int < 15 }}"
  - service: switch.turn_on
    entity_id: switch.4857044184f3eb916030_2

I was think of something similar and the way I thought of the automations is :

Automation #1 : Turn ON the light at SUNSET, with an offset of -15 hours, and a condition that it is before SUNRISE
Automation #2 : Turn OFF the light at SUNRISE

Would that work ?


Maintaining that will be critical to your constipation :rofl:

Hang on, what balance is needed, if any e.g. should we just start 7.5 hours before noon? And off 7.5 hrs later? Then work out what to switch around the horizon transitions ? What are the predator criteria ? When are the predators most active ? Early evening or early morning? What overlap sunrise/sunset do you need with the daylight. Sunrise/sunset is still pretty bright. I only allow external lights on when sun lower than -4 degrees.
Do you have a dimmer, it it set with transitions? Would that be better or hard / on hard off

(noon is calculated as the sun’s highest point, not 12:00)

I think that this should be written up as a package and anyone should be able to use it we could call it care of chicken kind or cock for short. :rofl:

Too funny. You guys are a hoot. Care of Chicken Kind… my rooster would approve, but would that package name fly on Github?

I’ll try finity’s automation (thank you!) tomorrow and report back! Too hard to modify code on this iPad.


I couldn’t wait… yaml config checks ok. Let’s see if it works tomorrow.

- id: '1569922898305'
  alias: turn on coop light with template
  - platform: template
    value_template: '{{ states.sensor.time.state == (as_timestamp(states.sensor.sunset.state)
      | int - 54000) | timestamp_custom(''%H:%M'') }}'
  - condition: template
    value_template: '{{states.sensor.daylight.state | int < 15 }}'
  - data:
      entity_id: switch.4857044184f3eb916030_2
    service: switch.turn_on
  - after: sunrise
    after_offset: 01:00:00
    condition: sun
  - data:
      entity_id: switch.4857044184f3eb916030_2
    service: switch.turn_off

That just seems waaay too simple. Why wouldn’t this work? It’s a conditional based on sunset, not a timer so even if a restart happened, the condition would still be tested.

I’ll try it as well.


1 Like

I was also thinking about it some more :wink:

If you don’t care if the light is on either before sunrise or after sunset you could easily just do something like this:

Trigger: CurrentTime = 15 - (Todays daylight in hours). Use this to turn on the light that many hours before sunrise.

Then turn off the light as soon as the sun is up.

In this case, you can easily make sure that your chickens always get 15h of light.
i.e.: Daylight Hours are 8h.
Sunrise is at 07:00

That would mean your automation would turn on the light @ 0:00 midnight (07:00 - ( 15:00 - 8:00 ))
And then turn off as soon as there is daylight at 7:00.

Another possibility would be a light sensor in the chicken coop :slight_smile: Then you just check for the light level and turn on the light if the light level is below a certain threshold and has not been above a certain threshold for 15h already.

that’s kind of what my template is doing, except it uses sunset instead of daylight hours.

But it’s not as simple as a strict numerical calculation because you have to convert everything to a datetime object the do the calculation, then compare it to the current time. Or convert the current time hour into a number to compare. but then how do you handle the minutes?

if you just do the “15 - hours” you get a number similar to what jazzmonger got in this post

And that can’t be used for comparison to the time now to cause the trigger.

That would probably work.

I didn’t think about using an offset of that many hours. I use offsets for stuff but mine are only minutes, not hours.

I’m happy to report this does indeed work and turns off an hour after sunrise. And it’s SIMPLE!!

- id: '1569929226364'
  alias: turn on egglight at sunset -15 hrs
  - event: sunset
    offset: '-15:00:00'
    platform: sun
  condition: []
  - data:
      entity_id: switch.4857044184f3eb916030_2
    service: switch.turn_on
  - after: sunrise
    after_offset: '1:00:00'
    condition: sun
  - data:
      entity_id: switch.4857044184f3eb916030_2
    service: switch.turn_off

Thanks to everyone who cared deeply about my eggs! I learned a few things along the way as well.


So the automation turns on the light 15 hours before sunset then turns it off 1 hour after sunrise. That’s it, that’s all.

The joke’s on us. This lengthy thread was all about accurately interpreting your requirements and precisely controlling the light’s duration (to the minute). In contrast, your chosen solution allows the light to remain on for a full hour after sunrise … or so it seems?

Yep, it just goes to show that the most important step of any design process is the URS :rofl:

I think the specs morphed over the course of 40 posts.

agreed. Or at least it gave the impression that it did.

nowhere did I ever state that. in fact this was stated: ““Yes, I want to overlap and turn on an hour before sunrise and give the little guys 15 +/- hours of light. Each day.””

and as you can tell, there a numerous ways to do this, depending on how you approach it. some quite complex, others simple. And judging from the responses, it appears it was a worthwhile exercise where most learned something, esp about templating and an enhanced sun.sun. which makes the platform even more useful to folks like me.

And that is no joke.


I never claimed you did. Nevertheless, the efforts to create a solution aimed for that level of accuracy. It’s clear that your 1-hour overlap comment was overlooked by many contributors (including me).

Based on my experience (about a year of participation in many hundreds of posts), I’d say this was an exercise in a cluster-fudge. Something is amiss when it takes a small army and 40+ posts to devise an egg-timer.

Dollars to donuts this would’ve been a concise thread had the requirements been stated as ‘turn on the light 15 hours before sunset then turn it off 1 hour after sunrise’. And that’s no yolk.

1 Like

Way to go. You’re doing a really great job of discouraging people from asking questions and even figuring out what question to ask and stumbling through a complicated system where one of the stated objectives is to simplify it.

On the contrary. It’s in everyone’s best interest to ask questions. In fact, there’s even a detailed explanation available of how to ask good questions.

I encourage everyone to read it and apply it when composing their questions. They’re more likely to get a faster, better answer.