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.
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.
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.
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.
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.
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
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.)