Trigger Lights when Home

Hello all,

I might be duplicating another post somewhere, but I’m trying to create an automation I can’t quite wrap my head around…Here is what I have so far, so you can see what I’m currently working with.

- alias: "Lights Homecoming Dark"
  condition: 
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.living_room_light_sensor
        below: 300
      - condition: or
        conditions:
          - condition: template
            value_template: "{% if is_state('input_boolean.person1_home', 'on') %}true{% endif %}"
          - condition: template
            value_template: "{% if is_state('input_boolean.person2_home', 'on') %}true{% endif %}"
  trigger:
    - platform: state
      entity_id: binary_sensor.front_door
      to: 'on'
  action:
    service: switch.turn_on
    entity_id: group.Homecoming

I would like to be able to sense if someone recently arrived home, then only trigger when the door opens…and only if it’s a certain brightness. Then, after the lights in the group “homecoming” have been on for 2 minutes, turn off the lights. I’ve been tinkering with this a lot today and I can’t quite figure out how to do this…I’ve been changing the code because the lights keep turning on at night when one of our phones registers as “away” in the middle of the night for some stupid reason. So, this way, it only triggers if the door also opens…But I don’t want them to turn on if I take out the garbage or something.

I also can’t figure out the final part…turn off the lights after a certain amount of time. Essentially, I need a timer for after the action is triggered. Any ideas?

Let’s start by removing the templates, they’re not needed. Then replace switch.turn_on with home_assistant.turn_on, since groups aren’t lights :wink:

I’m pretty sure you’ll find the group isn’t capitalised too

Tinkerer, thanks for your reply.

So this simplifies the input booleans, but it doesn’t really do much about my problem, unfortunately. The system I have does what it’s supposed to, just not what I want it to do, lol. So what I need to do yet is:

  • detect if person 1 or 2 has recently returned home (if possible), then trigger when the door opens
  • turn off lights 2 minutes after automation triggers

Any way to do this?

IF you make a group of the devices say group.people then whenever one of them returns home the group will turn on.

people:
  entities:
    - input_boolean.person1_home
    - input_boolean.person2_home

To turn the light off again use a script with a delay

turn_off_light:
 alias: Turn off Light
 sequence:
 - delay: 00:02:00
 - alias: turn off light
   data:
     entity_id: group.homecoming
   service: home_assistant.turn_off

Thanks phillprice! I think this is pretty much what I’m looking for. PS, since both of the switches in the group “homecoming” are mqtt switches, home_assistant.turn_off doesn’t work as expected. I’ll post my code a bit later when I’ve had some time to tinker with it a bit more.

Thank you both so much!

Got it! I decided not to turn off the whole group, just the light that won’t be used at all. For now, this is what I have

Automation:

- alias: "Lights Homecoming Dark"
  condition: 
    condition: and
    conditions:
      - condition: numeric_state
        entity_id: sensor.living_room_light_sensor
        below: 300
      - condition: or
        conditions:
          - condition: state
            entity_id: group.people
            state: 'on'
  trigger:
    - platform: state
      entity_id: binary_sensor.front_door
      to: 'on'
  action:
    - service: switch.turn_on
      entity_id: 
        - group.Homecoming
    - service: script.turn_off_lr_light

Script:

turn_off_lr_light:
  alias: Turn off Laundry Room Light
  sequence:
    - delay: 00:00:30
    - service: switch.turn_off
      data:
        entity_id: switch.laundry_room_light
1 Like

On a side note … I had some issues with presence detection and the iOS app so to help each person is in their own group with the iOS tracker and my asuswrt tracker so it only lists the group away if both say not home… this eliminated the false aways I was getting in the middle of the night from iOS devices alone

I have something similar. The group people is actually two binary sensors that sense whether either of us is home or not. I can post the code if you would like, but it sounds like you already have something set up.

I wouldn’t mind seeing it myself… mine is simply a group I like to see other people’s methods as there are often multiple ways to accomplish the same tasks… some are often much better than others.

1 Like

Hopefully I get everything here…There are a lot of parts spread across a lot of files. It’s a bit complicated, but it works like a charm.

Sample from known_devices.yaml

# bluetooth
person1:
  hide_if_away: false
  icon:
  mac: BT_*************
  name: person1
  picture: /local/person1.jpg
  track: true
  vendor: Samsung Electronics Co.,Ltd

# Wifi
person1_2:
  hide_if_away: false
  icon:
  mac: **************
  name: person1_2
  picture:
  track: true
  vendor: SAMSUNG ELECTRO-MECHANICS(THAILAND)

# Also working on Owntracks

Input_boolean.yaml

person1_home:
  name: person1 Home
  initial: on
  icon: mdi:android
person2_home:
  name: person2 Home
  initial: on
  icon: mdi:cellphone-iphone

Customize.yaml

input_boolean.person1_home:
  entity_picture: /local/person1.jpg
  friendly_name: person1
input_boolean.person2_home:
  entity_picture: /local/person2.jpg
  friendly_name: person2

Example Home and Away for person 1

- alias: 'person1 Away'
  trigger:
    - platform: state
      entity_id: device_tracker.person1
      from: 'home'
      to: 'not_home'
      for:
        minutes: 10
    - platform: state
      entity_id: device_tracker.person1_1
      from: 'home'
      to: 'not_home'
      for:
        minutes: 10
    - platform: state
      entity_id: device_tracker.ap
      from: 'home'
      to: 'not_home'
      for:
        minutes: 10
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.person2_home
        state: 'on'
      - condition: or
        conditions:
          - condition: time
            after: '07:00:00'
            before: '00:00:00'
            weekday:
              - mon
              - tue
              - wed
              - thu
              - fri
          - condition: time
            after: '09:00:00'
            before: '01:00:00'
            weekday:
              - sat
              - sun
  action:
    - service: homeassistant.turn_off
      entity_id: input_boolean.person1_home

- alias: 'person1 Home'
  trigger:
    - platform: state
      entity_id: device_tracker.person1
      from: 'not_home'
      to: 'home'
    - platform: state
      entity_id: device_tracker.person1_2
      from: 'not_home'
      to: 'home'
    - platform: state
      entity_id: device_tracker.ap
      from: 'not_home'
      to: 'home'
  condition:
    condition: state
    entity_id: input_boolean.person1_home
    state: 'off'
  action:
     service: homeassistant.turn_on
     entity_id: input_boolean.person1_home

- alias: "Away"
  trigger:
    - platform: state
      entity_id: group.people
      from: "home"
      to: "not_home"

Groups.yaml

People:
  entities:
    - input_boolean.person1_home
    - input_boolean.person2_home
2 Likes