Time condition problem (SOLVED)

Hi everyone. From unknown to me reason below automation was never triggered.

- id: motion_in_bathroom_turn_on_LED_light
  alias: If there is motion in bathroom turn on the light
  trigger:
    platform: state
    entity_id:
    - binary_sensor.motion_sensor_158d0001f9d56d
    - binary_sensor.motion_sensor_158d000200cc5f
    from: 'off'
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '00:00:00'
      - condition: time
        before: '06:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.bathroom_led_light

I’m running similar automation (just different light to be switched on in different hours) and that works just great. If I change above condition to be executed somewhere in the middle of day, then it works just fine, but when I try to start it between two days (at night) then it never triggers.
My intention is simply to run this automation between some specific hours. In above example between midnight and 6am.
What could be the problem? What I’m missing here?

Something weird happens around midnight. Change the start time to 00:00:01 and see what happens.

One problem could be the time condition splitting to 2 conditions. There is actually no reason to do this. Try this:

  condition:
  - condition: time
    after: '00:00:00'
    before: '06:00:00'

Also the ‘AND’ argument is not necessary as it is the default condition argument. If this doesnt work, then try what @nickrout said. I really never had a problem with midnight, but I wouldnt be surprised if he is right. It actually makes sense.

In the past I used condition where something was triggering between 32:00:00 and 06:00:00 and all was fine. Right now any time window where 00:00:00 falls in do not work. And I also figured out that something might be happening at 00:00:00. Unfortunately I know no way to debug automations.

I will try to remove “AND” condition and will report bach to you tomorrow morning.

I wouldn’t even bother with midnight as the ‘after condition’. If you just use before 6 am, it will consider everything from midnight to 6am:

- id: motion_in_bathroom_turn_on_LED_light
  alias: If there is motion in bathroom turn on the light
  trigger:
    platform: state
    entity_id:
    - binary_sensor.motion_sensor_158d0001f9d56d
    - binary_sensor.motion_sensor_158d000200cc5f
    from: 'off'
    to: 'on'
  condition:
    - condition: time
      before: '06:00:00'
  action:
    service: switch.turn_on
    entity_id: switch.bathroom_led_light
1 Like

Thanks a lot. That made the trick. I changed two conditions to a single one without ‘AND’. It seems to work just fine right now. It’s a bit strange as I had other automations with similarly set time conditions and all was working fine. Now I have corrected them all to a single condition and all automations still works as expected. I’m not sure whether I understand why one way is working and the other not. Anyway, my problem seem to be solved. Thanks to all.

@argykaraz: This post really confused me and the reply later from @wzbart saying that it fixed his problem completely sent me into dark.

I was trying to write a simple condition (something to happen only in between 7:30AM and 9:00AM) and I wrote the condition as you suggested but in my case it does not work. Removing the condition completely works well (so the rest of the automation is good: trigger and actions).

The part which really confused me is that in the documentation (here) it says:

Time condition

The time condition can test if it is after a specified time, before a specified time or if it is a certain day of the week.

Which suggests that actually OR will apply between after, before and days of the week.
The wording in the documentation is probably not so well chosen because you might also say that AND applies between after and before and OR applies between the two groups (before,after - days of the week).

Confusing.

Here is my automation which does not work even though I press the button in that interval of time:

# Turn on TV on Kitchen switch - left_long press if between 07:30-09:00
# 1. turn on 100%, green Living Room Lamp light
# 2. turn on Samsung TV switch
# 3. delay 30s
# 4. turn off Living Room Lamp light
- alias: Kitchen switch - left_long press
  trigger:
  - platform: event
    event_type: zha_event
    event_data:
      command: left_long press
      device_ieee: 00:15:8d:00:04:57:01:59
  condition:
  - condition: time
    after: '00:07:30'
    before: '00:09:00'
  action:
# 1.
  - service: light.turn_on
    data:
      entity_id: light.lamp_light_849cc020_level_light_color_on_off
      brightness_pct: 100
      rgb_color: [0,255,0] # green
# 2.
  - device_id: 8b49c4a0a19a415ab440e65b513abbd2
    domain: switch
    entity_id: switch.innr_sp_120_b7138903_on_off
    type: turn_on
# 3.
  - delay: '00:00:30'
# 4.
  - service: light.turn_off
    data:
       entity_id: light.lamp_light_849cc020_level_light_color_on_off
  mode: single

after 07:30:00, before 09:00:00

Hmm, it was to easy to see it. Right?

Thanks! Will fix it when I get home and reply.

So it is AND afterall?

It is easy to get confused with the documentation, as it sometimes is too much for the writer to have everything perfectly explained. You could make a suggestion in the documentation, to have it fixed. Those guys are developing so much on HA, that mistakes are forgiven. Anyway, I havent tried it, but I can only guess that this is an AND.
Anyway, like pepe59 suggested, your condition time is 7 and a half minutes after 12. Huehuehue :grin:
Did you fix it after all?

Yes, it was obviously broken but when you are close to it these are exactly the things you miss :slight_smile:

Thanks a lot guys for the quick answer! It saved me some headache.

And since we are at it and seems to be AND there now I understand why the other (complementary, so it is supposed to execute outside of the interval 7:30-9:00AM) automation is not working.
Any clue how to make that OR?

- alias: Kitchen switch - left_long press outside interval
  trigger:
  - platform: event
    event_type: zha_event
    event_data:
      command: left_long press
      device_ieee: 00:15:8d:00:04:57:01:59
  condition:
  - condition: time
    before: '07:30:00'
    after: '09:00:00'
  action:
# 1.
  - service: notify.mobile_app_sm_n975f
    data:
      message: "Attempt to open TV outside of the allowed interval!"
      title: "Kitchen switch"
# 2.
  - service: light.turn_on
    data:
      entity_id: light.lamp_light_849cc020_level_light_color_on_off
      brightness_pct: 100
      rgb_color: [255,0,0] # red
# 3.
  - delay: '00:00:01'
# 4.
  - service: light.turn_off
    data:
      entity_id: light.lamp_light_849cc020_level_light_color_on_off
# 5.
  - delay: '00:00:01'
# 6.
  - service: light.turn_on
    data:
      entity_id: light.lamp_light_849cc020_level_light_color_on_off
      brightness_pct: 100
      rgb_color: [255,0,0] # red
# 7.
  - delay: '00:00:01'
# 8.
  - service: light.turn_off
    data:
      entity_id: light.lamp_light_849cc020_level_light_color_on_off
# 9.
  - delay: '00:00:01'
# 10.
  - service: light.turn_on
    data:
      entity_id: light.lamp_light_849cc020_level_light_color_on_off
      brightness_pct: 100
      rgb_color: [255,0,0] # red
# 11.
  - delay: '00:00:01'
# 12.
  - service: light.turn_off
    data:
      entity_id: light.lamp_light_849cc020_level_light_color_on_off
  mode: single

Maybe something like this:

condition: OR
    conditions:
      - condition: time
        before: '07:30:00'
      - condition: time
        after: '09:00:00'

Oh, I thought you were talking about the weekday condition. I thought you were asking about if the weekday parameter is acting like an AND with the before and after parameters. So you are just asking for the before and after… Then I am 100% sure that this is an AND over there!

If you are trying to make the automation work all day EXCEPT when the time is between 7 and 9 am, then your automation is correct. It should work.
But please excuse my misunderstanding, but I think I have lost it somewhere…
In your first post, you say:

But now you just said:

What are you exactly trying to achieve? If you want this to work between 7:30 and 9:00 then your condition should be this:

  condition:
    condition: time
    after: '07:30:00'
    before: '09:00:00'

But if you want this outside this time:

  condition:
    condition: time
    after: '09:00:00'
    before: '07:00:00'

Really sorry for confusing you with this!
My first post was about an automation which works INSIDE the interval.

This post from few minutes ago is a complementary automation which has to work OUTSIDE the interval.

Your last two configuration snippets are contradictory though.

In the first snippet: A time value can be after 7:30 AND before 9:00.
In the second snippet: A time value cannot be before 7:00 AND after 9:00. Here is where I need an OR condition.
The time value has to be before 7:00 OR after 9:00.

I have just changed it to this and I will try it in the next 10mins:

  condition:
    condition: or
    conditions:
      - condition: time
        before: '07:30:00'
      - condition: time
        after: '09:00:00'

What you just did, will work. But I dont get it, why use the OR parameter? It still will get you the same result. Both conditions (your previous post and your last) will make the automation work outside 7:30-9:00, if thats your purpose.

It works indeed.

I’m not sure how it can work without OR. When the conditions are evaluated the time value has to be less than 7:00 or greater than 9:00.

It cannot be both lower than 7:00 and greater than 9:00 so you cannot use AND.

The thing with the time conditions is that the time resets at midnight, meaning 00:00:00.
That being said, when you want to have your action work after 9am and before 7am, if you just set after: 09:00:00 then if the time is 02:00:00, the automation will --not-- trigger. So you must define 2 variables, like in your case, if you just want to exclude a specific timespan.

So, when you set before 07:30:00 and after 09:00:00, you are basically saying: I DONT want you to trigger before 07:30:00 AND after 09:00:00.

But you actually mean: I DONT want you to trigger before 00:00:00 to 07:30:00 AND after 09:30:00 to 00:00:00

There are many ways to achieve the same thing. You could for example use a NOT condition.

  condition:
    condition: not
    conditions:
      - condition: time
        after: '07:30:00'
      - condition: time
        before: '09:00:00'

The above would trigger the automation anytime but NOT between 7:30 and 9:00
I am not really sure if I made it clear for you now, but this is how it works. I see your point, where you specifically say the time cannot be both before 7 AND after 9, but I am guessing that this is a time condition where the 2 parameters work together to surpass the midnight “thing”. Anyway, I am not an expert and I might have this all wrong, but since no one else is trying to explain this to you, I am doing my best. This is just my understanding. I hope it helped a bit. :smiley:

1 Like

@argykaraz Thanks alot for all the explanations!

I was aware that time condition works to and from midnight (so it does not cross in different days unless you have both “before” and “after” in the same condition). I think that is very well explained in the documentation (here), exactly the source of my initial confusion.

I have read again the documentation, starting with the beginning (here), and now it is very clear to me that automation conditions are applying AND if there are more of them (so all conditions have to be met for the automation to execute):

Conditions can be used within a script or automation to prevent further execution. When a condition does not return true, the script or automation stops executing. A condition will look at the system at that moment. For example, a condition can test if a switch is currently turned on or off.

Unlike a trigger, which is always or , conditions are and by default - all conditions have to be true.

So, in the example that you give above using NOT, you could also write it this way

  condition:
    - condition: time
      before: '07:30:00'
    - condition: time
      after: '09:00:00'

(I’m not too sure how strict is “before” and “after”, ie if they mean strictly “<” and respectively strictly “>” - I tend to believe it is a strict comparison).

In other words the above means:

if (
( (‘00:00:00’ <= now) AND (now < ‘07:30:00’) )
AND
( (‘09:00:00’ < now) AND (now <= ‘23:59:59’)
)

This cannot work! It is not possible to have all 4 comparisons be true in that “if” statement.

Though it will work if you put both “before” and “after” in the same condition, that is when the time condition applies and there is an OR (either of the two have to be true so the condition evaluates to true).

So this should look like this:

  condition:
    - condition: time
      before: '07:30:00'
      after: '09:00:00'

And translate to:
if (
( (‘00:00:00’ <= now) AND (now < ‘07:30:00’) )
OR
( (‘09:00:00’ < now) AND (now <= ‘23:59:59’)
)

Again, thanks alot for all the help! It really helped me out thinking things through and understand very well how this works!

4 Likes

@argykaraz many thanks for your smart thinking!

I was struggling to have an automation only trigger between 07:00:00 and 00:30:00 (we usually don’t go to bed before midnight :slight_smile:).

The NOT approach works like a charm.

1 Like

I stuggled almost one hour with this same problem and finally I noticed that my timezone was incorrect. My timezone was UTC but I live in Helsinki (GMT+3) and I was testing this just 1-2 hour before time to go to sleep and 1-2 after wake up and home assistant was still living in night time. Changed timezone to my current time zone Helsinki and tadaaaaa now my automation works and Homeassistant is living same time where I live :slight_smile: :slight_smile: