Time period binary sensors - based on sun.sun (sunrise and sunset) in 45 min intervals [working]

I put this yaml together (with much help from the Facebook community!) because currently there’s no sunrise/set option available in the ‘helpers’ UI menu, only time of day/week/etc - hopefully it’ll soon be included and this post will become obsolete, I’ll raise a WTH after this.

This yaml creates binary sensors to set periods of the day based around sunset/rise, rather than time of day. For example, this sensor ‘sunset 01 minus 45’ is ON from 45 mins before sunset untill 1 second before sunset, otherwise it is OFF. I am using these time slots to trigger scenes in automations.

image

Note the negative offset in this one because it is before sunset. Later entries don’t have ‘-’ on the hrs:min:sec before/after offset

To keep things tidy, I’ve created a new file called ‘binary_sensor.yaml’ and put an !include line in my config.yaml You can call anythingyoulike.yaml

Here’s the yaml. There’s 10 binary sensors based around sunset and 3 based around sunrise, and one that takes up the gap in between. They’re in 45 min increments - obviously you’ll want to adjust the offsets to your latitude/season and use case


## these binary sensors are ON during
## their 45 min time slots triggered by sunSET with offset
## else OFF

- platform: tod
  name: Sunset 01 minus 45
  after: sunset
  after_offset: "-00:45:00"
  before: sunset
  before_offset: "-00:00:01"

- platform: tod
  name: Sunset 02 sunset actual
  after: sunset
  after_offset: "00:00:00"
  before: sunset
  before_offset: "00:44:59"

- platform: tod
  name: Sunset 03 plus 0.45
  after: sunset
  after_offset: "00:45:00"
  before: sunset
  before_offset: "01:29:59"

- platform: tod
  name: Sunset 04 plus 1.30
  after: sunset
  after_offset: "01:30:00"
  before: sunset
  before_offset: "02:14:59"

- platform: tod
  name: Sunset 05 plus 2.15
  after: sunset
  after_offset: "02:15:00"
  before: sunset
  before_offset: "02:59:59"

- platform: tod
  name: Sunset 06 plus 3.0
  after: sunset
  after_offset: "03:00:00"
  before: sunset
  before_offset: "03:44:59"

- platform: tod
  name: Sunset 07 plus 3.45
  after: sunset
  after_offset: "03:45:00"
  before: sunset
  before_offset: "04:29:59"

- platform: tod
  name: Sunset 08 plus 4.30
  after: sunset
  after_offset: "04:30:00"
  before: sunset
  before_offset: "05:14:59"

- platform: tod
  name: Sunset 09 plus 5.15
  after: sunset
  after_offset: "05:15:00"
  before: sunset
  before_offset: "05:44:59"

- platform: tod
  name: Sunset 10 plus 5.45 to sunrise minus 15
  after: sunset
  after_offset: "05:45:00"
  before: sunrise
  before_offset: "-00:14:59"

  ## these binary sensors are ON during
## their 45 min time slots triggered by sunRISE with offset
## else OFF

- platform: tod
  name: Sunrise 01 minus 0.15
  after: sunrise
  after_offset: "-00:15:00"
  before: sunrise
  before_offset: "00:29:59"

- platform: tod
  name: Sunrise 02 plus 0.30
  after: sunrise
  after_offset: "00:30:00"
  before: sunrise
  before_offset: "01:14:59"

- platform: tod
  name: Sunrise 03 plus 1.15
  after: sunrise
  after_offset: "01:15:00"
  before: sunrise
  before_offset: "02:29:59"

After a quick reboot, they will all then show up in ‘entities’

Here’s an entities card I did just as proof of concept:

And here’s a basic automation requiring ‘sunrise 01’ sensor to be ON as a condition

I’m only just implementing them, I plan to tweak them, possibly adding seasonal variations - in my northern winter, the time interva I want to change my scenes by can be long because - it gets dark really early (I wrote it in winter, hence 45 min increments) but in summer, I want it to cycle the scenes quickly, because sunset is after kids’ bedtimes.

However, I’m pretty sure sun will be properly integrated in the helpers menu very soon and this will become unnecessary, in the meantime I hope it helps someone out. My ideal would be to see this in the GUI with sliders to set the offsets, but that is further down the rabbit hole…

This is the first thing I’ve added to the community and I’m really grateful to all who helped me learn enough to share :slight_smile: Any comments and improvement are most welcome.

5 Likes

This has already been raised in feature requests, but so far this only has 5 votes, so…CLICK HERE and give it a bump!

Thx for this awsome code.
Should be implemented in the GUI.

1 Like