Yes it is. What I do is turn the automation to do it on and off based on my needs using service automation.turn_off and automation.turn_on. But I know others frown upon that way of working because you lose the ability to disable the automation for other reasons.
The advised way is to create an input boolean helper. You can create one in the helper section. You turn the input_boolean on when you turn the light on with the automation you mentioned. The automatic off automation checks if the input_boolean is on to see if action is required, and turns the input boolean off off after it is done.
You can opt to turn the input boolean off when you want some kind of manual override too.
I can write an example, but I’d have to understand this. Wouldn’t you want to turn it off when it was previously off? I’ll assume the latter:
create an input helper in the Helper section of settings, named Auto fan (You can use another name, but then the entity name changes too in the automation. Automation for off:
alias: Fan off
description: ""
trigger:
- platform: state
entity_id: light.dakkapel
from: "off"
to: "on"
condition:
- condition: time
after: "00:30:00"
before: "10:00:00"
action:
- if:
- condition: state
entity_id: fan.dakkapel
state: "on"
then:
- service: input_boolean.turn_on
target:
entity_id: input_boolean.auto_fan
data: {}
- service: fan.turn_off
target:
entity_id: fan.dakkapel
data: {}
mode: single
Automation for on:
alias: Fan on
description: ""
trigger:
- platform: state
entity_id: light.dakkapel
from: "on"
to: "off"
condition:
- condition: state
entity_id: input_boolean.auto_fan
state: "on"
action:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.auto_fan
data: {}
- service: fan.turn_on
target:
entity_id: fan.dakkapel
data: {}
mode: single
You should of course replace entity id’s with the ones for your use case.
Go to Developer Tools, States and find your light and switch entity ID’s.
(The UI is supposed to make this step unnecessary, but it makes debugging easier if you know the actual entity IDs).
The state of the light will tell your automation if it’s currently on or off.
The best way to answer your question is that you should test the automation’s logic using various combinations of the light and switch being on/off.
If you don’t like thought experiments and prefer to observe it in action, change the time ranges from their current values to something more convenient (a 3 hour window around whatever time you test it during the day) then subject it to your test scenarios (I suggest you write them down beforehand because it’s easy to lose track of the various combinations).
I think it will work the way you want but testing it provides the best confirmation.
Start testing the simple scenarios first (fan is on, turn light on, fan should stop, then turn light off and fan should start) and once you’re satisfied it does the basics correctly, subject it to the ‘edge cases’ where you manually turn the fan on/off and see how it impacts the automation’s behavior.
There may be some combination that fools the “last_changed” technique I used in the automation and causes the fan to be set incorrectly. If you discover such a failure scenario then it may be necessary, after all, to employ an Input Boolean as has been suggested.
thank you for your .yaml, once I made the necessary changes it works perfectly, I changed the entity_id and changed the entity from fan to switch and it works just like I wanted