I am brand new to HA and am just trying to familiarize myself with automation.
For my very first one, I figured I would attempt to have HomeAssistant turn on a single light at my house (Philips Hue) at a certain time (11:40 am) and then turn off at 11:50am.
My automations.yaml file reads as:
- alias: 'Test Rule 1 - Fireplace Light on from 11:40am to 11:50am'
trigger:
condition: time
after: '11:40:00'
before: '11:50:00'
action:
service: light.turn_on
entity_id: light.tv_spotlight
indent preformatted text by 4 spaces
However, the time comes and goes and no light turns on.
Edit: I also can’t figure out how to neatly copy/paste code in here. The formatting is ugly.
In your automation, trigger: does not have a platform: specified. You specified condition: time which is incorrect.
The way you used after: and before: does not work the way you assume it does. It means the automation will trigger every minute between the two times. You want the automation to only trigger at 11:40:00 and at 11:50:00.
Your automation’s action: only specifies how to turn the light on. It will never turn the light off.
This automation does what you want:
- alias: 'Test Rule 1 - Fireplace Light on from 11:40am to 11:50am'
trigger:
- platform: time
at: '11:40:00'
- platform: time
at: '11:50:00'
action:
- service_template: light.turn_{{'on' if now().minute == 40 else 'off'}}
entity_id: light.tv_spotlight
It triggers at the two times you want.
When triggered, it checks if the current time’s minute is 40 (which matches the trigger’s start time of 11:40).
If it’s a match, the service is light.turn_on. If it is not a match then the automation was triggered by the finish time (11:50) so the service is light.turn_off.
Hi @traverst
So true, Initial_state is important if the automation should be automatically activated after a restart where it has been deactivated before or is deactivated by some reason prior to the restart.
From my experience if you won’t deactivate it, the automation should always be active even without that inital_state, or am i wrong? I am also kinda new to HA so for me to learn also
thanks
The light turns on at the correct time, however, the light did not turn off after 3 minutes. Is there any syntax missing from the service_template line which would be causing the ‘off’ to not be interpreted correctly at 12:12?
I assume you’re providing the abbreviated explanation for the purpose of initial_state, otherwise the statement is false. Few if any of my automations have initial_state and they are all active after a restart.
Home Assistant’s default behavior, upon restart, is to restore the automation’s last known state. If it was off before the restart it will remain off. If it was on before the restart, it will remain on.
Where this can fail is if there are frequent aborted restarts that prevent Home Assistant from storing/restoring the states properly. Then the automation’s last known state is lost and the automation will default to being off (deactivated). To prevent this possibility, initial_state was introduced to ensure the automation will always be turned on upon restart (or off if you use initial_state: false).
The potential disadvantage of assigning initial_state: true to all automations is that if you choose to disable an automation, upon restart it will be re-enabled which may not be what you want.
Now out of curiosity, how would I introduce my August smart lock into the equation? For instance, let’s say I want to turn a lamp on when my door is unlocked
- alias: 'Lamp on when front door locks'
trigger:
- platform: state
from: 'lock.lock'
to: 'lock.unlock'
entity_id: lock.front_door
action:
- service: light.turn_on
entity_id: light.lamp
Doesn’t seem to turn the light on. I think part of my problem is I don’t know where to find all the different states and arguments that HA can use. As in… what syntax do I use to check if the door is being unlocked? This was just my best stab at it.
Manually lock/unlock the August lock and observe the changes to its states in Home Assistant’s States page.
My guess the automation ought to look like this:
- alias: 'Lamp on when front door locks'
trigger:
- platform: state
from: 'lock'
to: 'unlock'
entity_id: lock.front_door
action:
- service: light.turn_on
entity_id: light.lamp