New to HA - first automation help

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.

Hi,
Welcome! :slight_smile:
Now i haven’t been here for very long either, but i have recieved alot of help so i will try to give back :slight_smile:

Condition as you have configured is used to tell the system if the trigger should do an action. So since you miss the trigger nothing will happen.

Simplest start automation will be:

- id: 'fireplace_light_on'
  alias: Fireplace light ON
  trigger: 
  - platform: time
    at: '11:40:00'
  action:
  - service: light.turn_on
    data:
      entity_id: light.tv_spotlight

And then a separate for the turn off.

Hope it helps :slight_smile:

1 Like

Dont forget initial_state: true otherwise the automation wont be active, example

- alias: 'Porch On'
  initial_state: true
  trigger:
    - platform: sun
      event: sunset
  action:
    - service: light.turn_on
      data: 
        brightness: 50
        entity_id: light.porch
1 Like

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.
1 Like

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 :slight_smile:
thanks :slight_smile:

Excellent! Your post helped illustrate a lot about how this yaml language works. Cleared a lot of things up for me.

The code that I put in looks like:

- alias: 'TV Spotlight On'
  trigger:
    - platform: time
      at: '12:09:00'
    - platform: time
      at: '12:12:00'
  action:
    - service_template: light.turn_{{'on' if now().minute == 09 else 'off'}}
      data: 
        brightness: 100
        entity_id: light.tv_spotlight

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.

2 Likes

Because you changed the action: section. You put in brightness: 100. Effectively, turn the light off and set it to brightness=100.


If you want maximum flexibility (without having to create templates), just use two separate automations for turning the light on and off.

- alias: 'Test Rule 1 - Fireplace Light ON'
  trigger:
    - platform: time
      at: '11:40:00'
  action:
    - service: light.turn_on
      data:
        brightness: 100
        entity_id: light.tv_spotlight

- alias: 'Test Rule 2 - Fireplace Light OFF'
  trigger:
    - platform: time
      at: '11:50:00'
  action:
    - service: light.turn_off
      entity_id: light.tv_spotlight

Fantastic!

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.

You need to check in the dev-state what the states for your entity lock.front_door actually are.
I doubt they are lock.lock and lock.unlock.

+1 to what chairstacker said.

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