hi, I have an automation to turn off my fan plug if on when light turned on between 00:30-10:00, but was wondering if it’s possible to do the following
turn fan on if it was previously on before light turned on
hi, I have an automation to turn off my fan plug if on when light turned on between 00:30-10:00, but was wondering if it’s possible to do the following
turn fan on if it was previously on before light turned on
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.
sorry I’m new to HA, how would I do that
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.
We could write it for you, but what fun is that?
I’ll try to explain more
When
If
Then
I’ve already created this automation, I want the following to happen
When
if
then
When you paste code, you heed to place it between three backticks to show properly. Otherwise it is of no use to is. See number 11 in this post:
I see I read your example the wrong way. I edited my post above to fit your example.
last question, when I click create helper, what do I select
Strangely enough, I see it is now labeled Switch (Schakelaar in Dutch for me) while it does create an input_boolean.
“Toggle” creates an Input Boolean…
The following single automation will:
Turn off the fan switch when:
on
.Turn on the fan switch when:
off
.Change the following two entity_ids to match what you have in your system.
light.your_light
switch.your_fan_plug
alias: example
trigger:
- platform: state
entity_id: light.your_light
from: 'off'
to: 'on'
variables:
mode: 'off'
is_true: >
{{ today_at('00:30') <= now() < today_at('10:00')
and is_state('switch.your_fan_plug', 'on') }}
- platform: state
entity_id: light.your_light
from: 'on'
to: 'off'
variables:
mode: 'on'
is_true: >
{{ today_at('00:30') <= now() < today_at('09:45')
and is_state('switch.your_fan_plug', 'off')
and today_at('00:30') <= trigger.from_state.last_changed < today_at('09:45') }}
condition:
- condition: template
value_template: '{{ is_true }}'
action:
- service: 'switch.turn_{{ mode }}'
target:
entity_id: switch.your_fan_plug
It doesn’t rely on an Input Boolean.
Correction. Removed extraneous }}
characters from second trigger’s is_true
template.
can I still turn fan on/off manually between those times without affecting light
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.
I can test it when I go to bed tonight
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.
will do, automation won’t be used much
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