# How to work out the position in the day relative to a time

I hope I got someone’s attention because I really didn’t know how to describe my question in one line.

So, My lights go off every night at a ‘normal off time’ (say 23:00) but I need to account for other things like motion starting and stopping, and coming home after the normal off time.

The problem I’m having is knowing if the time now is after the normal off time.

I store the normal off time (not date) in an `input_datetime` so in a situation for example where motion occurs, how do I know if it is before or after 23:00 given that 23:30 and 01:30 are clearly both after 23:00 but mathematically 01:30 is not.

I currently have a VERY kludgy method that I have been using for over a year which uses all sorts of (what I thought when I first started out with HA were clever) tricks involving Boolean flags etc. but the time has come to rationalise my old code.

The only way I can think of is to store the time and date in the normal off time and have an automation that runs at midnight to add 24 hours to it. But that still feels a bit kludgy to me and liable to timing issues if things happen, like motion, at 00:00.

For “Evening” switch points (I stress the evening) I use a Kludge
If settime > 12:00 then On if > than settime
If settime < 12:00 then On if < settime

Morning is a given as anyone thinking that 23:00 is morning is a moron

TOD sensor can be used to easily define things like morning and evening:

Thanks, yes that nearly does it.

I must confess to completely forgetting about that sensor because when it was first implemented I just found the terminology of the offsets so confusing that I shunned it. Also as an aside I hated the name why TOD? Why not `time_of_day`???

However, I need my ‘normal off time’ to be a date_time as it changes depending on circumstances. For example weekdays/weekends or when in holiday mode. And you can’t template the before, after or offset times in the TOD sensor.

how do I know if it is before or after 23:00 given that 23:30 and 01:30 are clearly both after 23:00 but mathematically 01:30 is not.

The question is where do you want to draw the line (if not midnight)?

Is 4am before or after 23:00?

That’s the point. The 23:00 is fixed on a date* the time is the time now. Hence my suggested solution of incrementing 23:00 (or whatever it is) by 24 hours. But that is a kludge.

But, I think you’ve nudged me into formulating the question much more succinctly

I need to be able to tell if

`time_now` is after `input_datetime.normal_off_time` and `binary_sensor.dark is true`

‘normal off time’ can be before or after midnight
'binary_sensor.dark` I have defined

*Today, if time now is between normal off time^ and 23:59, or yesterday if time now >= 00:00
^Which will be complicated by the fact that normal off time could be past 23:59!!

PS
I can’t believe I am the first person to have this situation. i.e.

Light comes on when it is dark (this is not always at the ‘end’ of the day e.g. in bad weather)
Light goes off at a set time (which can be changed depending on circumstances)
Light comes on whenever there is motion if it is dark
Light goes off when motion stops if it is ‘past’ the set off time

There are other situations such as occupancy but they are all variations on the same theme.

Of course it is more than possible that I have overthought all this and can’t now see the wood for the trees…

No, you’re not.
I genuinely don’t see your problem, by definintion you only want it on during ‘dark’ and you have a binary sensor for that.
The light has a given on slot : (say) 20:00 to 22:00 (are these via input datetime too ?)
So, you want to cover motion activated light from (the above) 22:00 to an arbitrary time that ‘could’ be 23:00 or ‘could’ be 02:00 and is set by an input datetime

I’m stll a bit unclear about why you wouldn’t want motion activation if you are away - if you need this we’ll need more details…

you need to be more explicit, this could be interpreted that “if motion is detected light comes on and stays on till end time, and only then if motion has stopped.”

Light comes on when it is dark (this is not always at the ‘end’ of the day e.g. in bad weather)
This bit is simple and I hope self-explanatory.

Light goes off at a pre-defined time (which can be changed depending on circumstances)
Every night the light should go off at a set time so long as there is currently no motion detected. This pre-defined time is not necessarily the same every night but is a time which is held in an `input_datetime` and accessible from the UI and (naturally) to other automations who may change it. For arguments sake let’s call this 23:00 but remember it can be a time ‘after’ 23:59.

So, the lights come on when it is dark and go off when it is 23:00. Easy.

Light comes on whenever there is motion if it is dark
Whenever there is motion, if it is dark, the lights come on (easy) or stays on if something else is trying to turn them off e.g because it is 23:00. Also not hard.

Light goes off when motion stops if it is ‘past’ the set off time
This is the bit I’m struggling with…
Whenever something decides it is time to turn off the light e.g. motion stopped, it should not happen if the time now has not yet passed 23:00.

This is starting to look like I’m defining my requirements and expecting someone to write me a system. That is not the case, I was just wondering if there was a better way than my 24 hourly automation idea.

Other things like occupancy, holiday mode and anything else are merely extra bits that are simple to include. So yes, how do I know if I am ‘above’ 23:00 (or 02:00) given the time now could be 23:30 or 01:30

``````time when motion stops   normal_off_time   action
22:30                    23:00             light stays on (22:00 < 23:00)
23:30                    23:00             light goes off (23:30 > 23:00)
01:30                    23:00             light goes off (01:30 < 23:00 !)

22:30                    02:00             light stays on (22:30 > 02:00 !)
01:30                    02:00             light stays on (01:30 < 02:00)
02:30                    02:00             light goes off (02:30 > 02:00)``````

Okay,
Just to clarify, when you say ‘light stays on’ you mean “after” normal off time, the light just goes into "motion --> switch on, no motion, run timer (say 1m), then light turned off ?

I need some entities (or I’ll assume the ones I make up here) like : -
binary_sensor.dark_outside
light.outside_light
input_number.light_mins_on
input_datetime.ext_light_normal_off_time
binary_sensor.motion_detected

Your basic problem is you’ve defined when the “turn off automatically” time period has started (i.e., via `input_datetime`), but you haven’t defined when it should end. You can’t determine if you’re inside a period of time if you’ve only defined one end of the range.

nah forget all that (my request) … I’ll just give you the template for ‘upto’ your ‘time value’

``````#### klogg's night time
{% set tme = sensor.time %}
{% set sundwn = is_state('sun.sun', 'below_horizon') %}
{% set drkout = is_state('binary_sensor.dark_outside', 'on') %}
{% set drknss = drkout or sundwn %}
{% set extlgtofftme = states('input_datetime.ext_light_normal_off_time') [0:5] %}
{{ (drknss and tme < extlgtofftme) if extlgtofftme > '12:00' else ('12:00' < tme < '24:00' or tme < extlgtofftme) and drknss }}
``````

There’s belt an braces here as I figure sun below the horizon is (by definition) pretty dark but supplemented by your dark sensor.
Rather than below_horizon you could state sun below (say) -6° elevation (if you want it a little darker) or just run with your dark sensor

Edit : And I have to agree with Phil, I don’t think I would be asking you to write me a URS for anything

I’m not sure that is true. Well maybe I haven’t defined it explicitly as it is an undefined absolute time, but it is clearly implied within the ‘logic’.

The light always turns on with motion, if it is dark.

When in the “turn off automatically” period it turns on with motion as usual, and then turns off after x minutes (no motion).

This continues ad-nauseum until such a time it isn’t dark anymore (the end of the “turn off automatically” period) in which case motion will not cause the light to go on in the first place.

But it also mustn’t turn off when motion stops if is before the “turn off automatically” period.

I think I’ve just said exactly the same thing as I’ve always said but in the reverse case!

The question (to me) is still, how do I test if it is in (or out) of that automatic period?

If it can be dark at different periods throughout any given day (clouds, storms, etc.), and your start time can be at the end of the day, or at the beginning of the day, and you haven’t defined any other criteria, then you can’t determine if you’re in the “automatic turn off” period, because you haven’t defined it.

Think about it. Let’s say the turn off period starts at 12:01 AM (00:01.) Then any dark period is the “automatic turn off” period because it’s always after the start time.

You’re having so much trouble figuring out the answer because your stated requirements are insufficient.

Also consider, what if it is dark all day long? When does the period ever end?

Maybe I’m missing something but no, I don’t agree. If the turn off period starts at 00:01 then it has already been dark for (some) hours before 00:01. During that time I don’t want the ending of motion to turn off the lights; but after that time I do.

And yes that is true, I can’t remember if I said it earlier, I meant to, there are a lot of other nuances* to the controlling of lights but they become fairly trivial(ish) once I can define the “automatic off” period.

I have a working system now but based on ‘flags’ (`input_booleans`) and other messy code structures/automations which I don’t like. I just hoped there might be relatively simple way to do it that I had overlooked using time based calculations.

*e.g. bad weather as you said, not wanting the lights to come on automatically around ‘getting up time’ (a personal preference in this house, it just feels better to us not to be greeted by lights in the morning), lights that go off when we go out and lights that come on when we come home and obey the “automatic” rules. Lights that randomise when we’re on holiday… And I don’t even have dimmable lights, I can only imagine the options that could bring.

It’s actually ironic that the simple ‘hello world’ automation to turn on a light that most people start with can become so nuanced.

Yeah, I found that when I was building the logic.
I thought “well he clearly knows what he wants, and just needs help with the time closing template”
Hence…

Like it or not, all your other flags and automations, etc. are defining something that you so far have not been able to put into words.

Consider this. You set the time to 00:01. That time happens and it’s dark, so the period starts. Some time later it becomes light and the period ends. So far sounds simple.

But then it gets dark again. Well, it’s dark and it’s after 00:01, so the period starts again, right? You’ll say no. But how can an algorithm that only says, “it has to be after the specified time and dark for the period to begin” know that?

There’s another requirement you haven’t stated yet. That’s why you can’t figure it out.

Can you work with the template I gave you ?
Does it suffice to determine your period of action?

Can you draw us a karnaugh map of the light response given : say
Light level
Time on
Time off both before and after 00:10
Motion activation triggered
Motion activation ignored

Maybe a time line would be better
There is a problem because I at least, am not clear on how this light responds
Sorry

Yes.

No, I say yes!
The period starts when it goes from light to dark (except in cases where I choose to ignore that transition, e.g. getting up time, but ignore that for now)

The not automatic time is any time from when it gets light up to the ‘off time’. Doesn’t that clearly define the start and end? That will also cover non night-time darkness events because it will eventually either get light again or the automatic period will start…