Spanning midnight time

According to one of the examples here https://www.home-assistant.io/docs/scripts/conditions/ the time condition allows you to specify after and before values that span midnight (e.g. after 7pm, before 7am) and the condition will work as expected. My experience has been it doesn’t work and the automation never gets triggered. A quick search for this on various forums suggests that I should using multiple conditions and the ‘OR’ statement, that is two condition statements for after and before.

Is the documentation example incorrect (or have I misread?), or am i experiencing a bug? If I change the condition so the ‘after’ value is one minute past midnight, it works as expected.

Yeah, the docs are incorrect, unless something has changed recently.

Time conditions are to or from midnight on the day.

So if you want a condition that it is between 7pm and 7am, you would need

condition:
  condition: or
  conditions:
    - condition: time 
      after: "19:00:00" #7pm - midnight 
    - condition: time 
      before: "07:00:00" #midnight - 7am
4 Likes

Thanks! So this is either a bug OR an anticipated future behavior. I’ll see if this needs reporting somewhere.

I think somebody has reworded the docs wrongly tbh, I’ll fix it when I get chance :+1:

FYI the truth table that is in the conditions does not apply to the triggers time before/after attributes that @anon43302295 said. That’s probably why you see a difference.

Could you show an example of your code, I have the following and it works as expected:

  condition:
    condition: and
    conditions:
      - condition: time
        after: '22:00:00' 
        before: '02:00:00' 
      - condition: numeric_state
        entity_id: sensor.outside_temperature
        below: 12

if you have split it over two time conditions it will not work, eg: (it will need to be an ‘or’ as per the above example from mf_social)

  condition:
    condition: and
    conditions:
      - condition: time
        after: '22:00:00' 
      - condition: time
        before: '02:00:00' 
      - condition: numeric_state
        entity_id: sensor.outside_temperature
        below: 12
1 Like

Actually I configured this via the automation GUI, and that spits out:
condition:

  • after: 11:00:00
    before: 07:00:00
    condition: time

… but it seems I probably needs a more complex condition than this allows anyway, so I will resort to manually try to code something. It looks like I should try out templates so I can combine a time and temperature condition & trigger into one statement - I’d need two or three if I use the GUI.

Thanks

You need quotes around the times and the 11:00:00 should be 23:00:00, then it should work as expected. (I’m assuming that you meant 11PM, but even so it should also work fine between the times given.)

I have just tested the following and it works as described in the documentation, I received an alert every minute until ‘06:50:00’ and then the alerts stopped:

- trigger:
    platform: time
    minutes: '/1'
    seconds: 00
  condition:
    condition: and
    conditions:
      - condition: time
        after: '22:00:00' 
        before: '06:50:00' 
      - condition: numeric_state
        entity_id: sensor.outside_temperature
        below: 20
  action:
    - service: notify.all
      data_template:
        message: Test

So, spanning midnight does work, what @anon43302295 said is incorrect and the documentation does not need to be changed.

1 Like

I noted the inconsistencies with double and single quotes - also the fact the GUI sometime adds quotes and then sometimes leaves them off. Is the use of quotes documented? I would be useful.

I’m basically trying to get an automation that says:

Between 11pm and 7pm
If the temperature (sensor) is below X then switch on heater (switch)
If the temperature (sensor) is above Y then switch off heater (switch)

I have thought the trigger to be the sensor, and the condition to be the time.

Do I have this wrong?

I’m not sure of the exact rules, I usually look at the examples or automations that I already have (that work).

In theory that would work, but if the temperature is already below or above the given temperatures before 11PM the trigger will not fire until it has crossed the threshhold. I’ve never had use for such an automation before, but if I had to write one it would look a bit like my last example above:

  • Set timer to go off every 10 minutes or so
  • Check the time condition
  • Check the temperature condition
  • All OK? - Fire Action

Something like this (I’m assuming 11pm to 7am):

- alias: Heater on
  trigger:
    platform: time
    minutes: '/10'
    seconds: 00
  condition:
    condition: and
    conditions:
      - condition: time
        after: '23:00:00' 
        before: '07:00:00' 
      - condition: numeric_state
        entity_id: sensor.outside_temperature
        below: 16
  action:
    service: switch.turn_on
    entity_id: switch.heater

- alias: Heater off
  trigger:
    platform: time
    minutes: '/10'
    seconds: 00
  condition:
    condition: and
    conditions:
      - condition: time
        after: '23:00:00' 
        before: '07:00:00' 
      - condition: numeric_state
        entity_id: sensor.outside_temperature
        above: 18
  action:
    service: switch.turn_off
    entity_id: switch.heater

(examples off the top of my head, not tested)

*Edit: added ‘seconds: 00’

quick note: the above examples could be improved by combining them with some Jinja code.

Another option is to use the Generic thermostat template and switch it on and off at the given times. (I have no experience with this component though.)

1 Like

I just happened to try something like this last night and was surprised to find that the midnight wrap around did not work for me.

E.g.:

condition:
  - condition: time
    before: "05:00:00"
    after: "22:00:00"

did not work, but

condition:
  - condition: or
    conditions:
      - condition: time
        before: "05:00:00"
        after: "00:00:00"
      - condition: time
        after: "22:00:00"
        before: "00:00:00"

did work. Though I don’t know what would happen if it was triggered at exactly “00:00:00”

Here is the trace for the version that did not work:

Executed: December 6, 2022 at 12:43:52 AM
Result:
after:
  __type: <class 'datetime.time'>
  isoformat: '22:00:00'
now_time:
  __type: <class 'datetime.time'>
  isoformat: '00:43:52.103830'
before:
  __type: <class 'datetime.time'>
  isoformat: '05:00:00'
weekday: []
now_weekday: tue
result: false

Should wrap around work?
Any ideas as to why it isn’t working in this example?

P.S. Sorry for the thread necromancy… I couldn’t find a fresher one about this topic.