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

Then what’s the problem? If the time is after the “off start” time, it’s after the “off start” time, right?

I have no idea what one of those is but let’s say…

Normal light off time is defined as 23:00

It’s midday and light.
Light is off.
Nothing will trigger the light on while it stays light
At 14:00 Something makes it dark while it is ‘supposed to be’ light (weather)
The light comes on, it is before 23:00 so it goes off again when it becomes light (weather passes)
It gets dark because it is the end of the day
The light comes on
Nothing else affects the light until 23:00 when it goes off (if there is no motion)
At 23:30 while the light is off there is motion so the light comes on for (say) 5 minutes before it goes off as long as there is no more motion (obviously)
At 01:30 there is more motion so it comes on again for 5 mins before going off
and so on until…
When it is morning and light again the light goes off if it is on and stays off until something (weather or sunset) makes it dark again.

When it gets (i.e., becomes) light, or when it is light? Are you trying to define a period via logic, or are you trying to detect when it starts by monitoring for an event?

I’ve re-read your first and last posts. I’m at a loss to understand what you’re trying to achieve.

Maybe let’s try to attack this a different way. What are you trying to prevent?

You say when it gets dark the light should go on.

You say when it gets light the light should go off.

You say when there’s motion the light should go on and back off after some amount of time.

You say at the “normal off time” (set by an `input_datetime`) the light should go off, if there is no motion.

At least I think that’s what you’ve said. Which of the above are false or incomplete?

I fail to see either an “automatic” period or “not automatic” period. And, besides, automatic what?

Could it simply be that what you’re trying to say is you want to know when it is after the normal off time and before the first time it becomes light during the day (or possibly some worst case latest time in the morning if it stays dark due to weather, etc.)?

Exactly the question I asked many, many confusing posts ago.

The question is where do you want to draw the line (if not midnight)?
Is 4am before or after 23:00?

1 Like

Yes

Yes

Yes (but only if it is dark but that’s not particularly relevant here I just add it for completeness)

Yes (unless there is motion but again not relevant here)

I am not sure who ‘invented’ the term ‘automatic period’ but I don’t think it was me but it refers to that period of time which is after the ‘normal off time’. However, there is no real ‘automatic period’, just a boundary. Once morning comes the lights stay off by default and the next time they come on it must be before the ‘normal off time’ (because it is dark all night until it is light again) so the only thing I need to know is whether now is before or after that time. (The more I write it the simpler it sounds to me but I am very conscious that simply saying that same thing over and over again does not help get the message over, so I apologise for that.)

Notice how you didn’t say, “and before xyz”? A period is not defined by one time, it is defined by two. You still haven’t said what constitutes the end of the period.

If it’s midnight, then it’s simply time > off_time. But I don’t think that’s what you’re after, especially when off_time is early in the morning because, again, that implies most of the day.

Yes!
I think we might be there!
It is a simple question. I just can’t find a way to deal with the clock ‘roll-over’ at midnight. without flags or including the date which then means it needs to be incremented every day with the potiential for problems I describe (briefly) in my first post.

So we’re making progress? Yippie!

If that’s exactly what you want, then the problem stems from the fact that “the first time it becomes light during the day” is probably not directly available (yet) in an entity. You probably have to add automations and an `input_boolean` to record when that happens (and resets it at the end of the day.)

I was going to add that the ‘period’ effectively continues until the next ‘normal off time’. But I still don’t see it as a period, it isn’t. All the other logic and triggers deal with everything else. I don’t care about anything except before and after the boundary of (usually) 23:00 so that I know whether the lights should go off after an event (e.g. motion)

Well it has taken a while but thanks, I think you’ve just solved it. And it couldn’t have been more simple…

My first post says:

Why did I think I had to update the normal off time (date and time) at midnight? I can do it when it gets light in the morning thus avoiding any time issues with things happening at the same time!!

Simple?
(Unless you have a better idea? )

And slightly embarrassing that I didn’t think of it. Oh well hopefully I kept you and @mutt off the streets for another night

Why do I think that’s a bit premature?

Honestly, I think I know what you ultimately want, but I’m still stumped on how to implement it. But at least I think I know what you want now.

Good luck! If I come up with something practical I’ll let you know.

Have confidence!

That makes me feel better

Thanks…

1 Like

So what about my end time template?
That rolls over midnight quite well

Edit: Found an error in the template, corrected now

But quite often you can get away with a truth table, also explained in the above

Can’t find anything on time lines but it basically draws an axis
With any input variables shown as blocks above it either on off or some other statement about their condition
From it you can identify actions / transitions the notes are important as they explain why this happens here and why
You can draw the same entities in multiple lines (different line blocks) to show an alternate logic set (if required) required as not all logic can continue if a particular event occurs in one block

So the problem is, when normal time off is late in the day, and time has crossed midnight, without any other constraint, it’s impossible to know that you’re effectively after the off time when you’re really before the off time. This is because, as you said, they’re just times without dates.

I think I had something like this in my SmartThings days, and @Mutt touched on it when he had noon in his suggested solution. Basically, one way to crack this nut is to cut the day in half and treat the first part differently than the last part.

If the off time (X) is in the first half of the day [i.e., it’s less than noon (N)], then the current time (t) is after the off time if 1) it’s greater than it AND 2) it’s less than when it first becomes light (L), which is probably around sunrise. I.e., the time is after the off time but before when it becomes light. Or stating it logically/mathematically:

``````X < N and X < t < L
``````

But when the off time is in the second half of the day, then the current time is after the off time if 1) it’s greater than it OR 2) it’s less than when it first becomes light:

``````X >= N and (X < t or t < L)
``````

So putting them together:

``````X < N and X < t < L or X >= N and (X < t or t < L)
``````

QED

P.S. Of course, it can’t know a priori when L will be. I leave that to the interested student.

EDIT: Ok, one last comment and then I’m done.

An automation like this can replace the `t < L` part.

``````trigger:
- platform: numeric_state
entity_id: sensor.LIGHT
above: LIGHT_THRESHOLD
- platform: time
at: '00:00:00'
action:
service_template: >
input_boolean.turn_{{ 'off' if trigger.platform == 'numeric_state' else 'on' }}
entity_id: input_boolean.still_dark
``````

You’ll also need to make sure `input_boolean.still_dark` is set correctly when HA starts.

If we make `SD = is_state('input_boolean.still_dark', 'on')`, then `SD = t < L`. Substituting:

``````X < N and X < t and SD or X >= N and (X < t or SD)
``````

If you haven’t figured this out yet…(i’m confused if there is a solution in here somewhere):

It might help to post what you have that is working right now.

As far as my ideas:

in simplest terms it seems you need four automations to do everything you want -

1. turn on the light at dark

2. turn off the light when it gets light out or at the variably programmed time as long as there is no motion…

3. turn on the light with motion and when dark

4. turn off the light after no more motion unless the light is supposed to still be on because it’s before the off time.

I’m assuming that #1, #2 & #3 are easy to do so moving on to #4

See what you think of the following pseudo-code:

``````trigger:
motion detector to off
condition:
- the light is off
- it's dark
and
(
- if off_time <= 23:59 and off_time < time_now <= 23:59 ### if off_time is before midnight and motion goes off between off_time & midnight then return true
or
- if off_time  is >= 00:00 and off_time < time_now < sunrise_time ### if off_time is after midnight and motion goes off between off_time & sunrise then return true
)
action:
turn off the light
``````

Okay, I don’t mention the light but the rest of this is what the template does ??? What am I missing ?

1. It has to be dark
2. If the end time is before 00:00 it then has to be time < endtime
3. If end time is after 00:00 but time is between 12:00 and 24:00 then it’s ON (okay ficticious, non-existent time but it works in the template, also remember it has to be dark)
4. If it’s after 00:00 then time just has to be less than endtime

So, given the above, the permissive will come on when dark (although will go off again if it becomes light a minute later (does lamp light impact on sensor to cause flip flopping, I assume he’s taken care of that) then stays on until the end time (whatever time that is, as long as it’s still dark)

I have top be honest and tell you I didn’t read the template to closely. I meant to go back and work thru what you had but I forgot about it before I replied.

So I think what you have is:

``````{{ (drknss and tme < extlgtofftme) if extlgtofftme > '12:00' else ('12:00' < tme < '24:00' or tme < extlgtofftme) and drknss }}
=
if off_time > noon then turn off the light if there is motion, it's dark and now is < off_time
else turn off the light if there's motion and
(
it's dark
and
(
now is between noon & midnight
or
now < off_time
)
)
``````

However, I think that it won’t do what he wants.

He wants the light to be turned off with motion only if the time is after the off time (tme > extlgtofftme) not before.

Unless I’m missing something myself.