Lock unlock automation

I’m trying to write and automation to lock my front door at sunset but I can’t get the code right. I’m having trouble understanding how to call the service to lock.

The entity_id of the lock is “front_door_lock_locked” according to the entity list. My automation looks like this:

- alias: UnlockFrontDoorWhenHomeZone
  trigger:
    platform: zone
    entity_id: front_door_lock_locked
    zone: zone.home
    # Event is either enter or leave
    event: enter
  action:
    service: lock.unlock

Something isn’t right as the log is complaining about invalid entity IDs.

Does anyone have an example of what I’m trying to do?

Thanks!

Your action should be:

  action:
    service: lock.lock
    entity_id: <the entity id of your lock>

Ok, so I had the entiity_id in the wrong place. Reloaded automations with the change and so far no errors. We’ll see if it works.

Thanks for the quick response!

I find it hard to keep up with the inconsistencies in the syntax for different parts of HASS. For instance in a zone automation the enitity_id you’re acting on is up under the trigger section. But for a sun automation like above it goes down in the service: section. Confusing!

Show me that example…

Example of platform zone:

- alias: NotifyWhenHomeZone
  trigger:
    platform: zone
    entity_id: device_tracker.iphone
    zone: zone.home
    # Event is either enter or leave
   event: enter
  action:
    service: notify.ios_iPhone
    data:
      title: "Lucy I'm Home!!"
      message: "I'll be surprised if this works!"

vs platform sun

- alias: LockFrontDoorAtSunset
  trigger:
    platform: sun
    event: sunset
  action:
    service: lock.lock
    entity_id: group.all_locks

I assume that the entity_id in the first example is the entity which triggers the automation, while the service ios_iPhone contains the entity that should be notified. In the second example, the entity_id refers to which entity to take action upon. In that sense, HA is consistent, it just uses entity IDs everywhere :slight_smile:

1 Like