How to turn off all lights and media players when everyone is out of the house

Hi, I’m very new to HA and its been going well so far but I’ve hit a snag. I’m trying to automate, if me and my wife leave the house, to turn off all lights and media players. I have grouped the people, lights and media players. and this is the code I have come up with but un surprisingly, something is wrong and my very limited coding knowledge cant help.

alias: Turn off lights and music when nobody is home
mode: single
trigger:
  - platform: zone
    entity_id: pdevice_tracker.WIFE_s_phone
    zone: zone.home
    event: not_home
condition:
  - condition: zone
    entity_id: device_tracker.MY_phone
    zone: zone.home
    event: not_home
action:
   - data:
  entity_id: group.all_lights
service: light.turn_off
  entity_id: group.all_music
service: media_player.turn_off

Any help would be much appreciated.
Thanks

alias: Turn off lights and music when nobody is home
mode: single
trigger:
  - platform: state
    entity_id: device_tracker.WIFE_s_phone
    to: 'not_home'
  - platform: state
    entity_id: device_tracker.MY_phone
    to: 'not_home'
condition:
  - condition: state
    entity_id: device_tracker.WIFE_s_phone
    state: 'not_home'
  - condition: state
    entity_id: device_tracker.MY_phone
    state: 'not_home'
action:
  - service: homeassistant.turn_off
    entity_id: group.all_music
  - service: homeassistant.turn_off
    entity_id: group.all_lights

This will trigger if either of you leave home, then check to see that both of you are away. If so, do the actions.

When turning off groups you have to use the homeassistant.turn_off service.

There is a special light group that you can use the light.turn_off service with as it actually creates a new light (that controlls all the group members), not a group.

This has other advantages in that it looks like a light in the Lovelace frontend.

You create this group under light: not group:

2 Likes

entity_id: pdevice_tracker.WIFE_s_phone

Tom has your answer. but I just figured i would point out the extra ‘p’ in the device_tracker text.

copy paste error, or what have you. but she wont work like that!

1 Like

Thanks! This has been accepted and looks like it will work. Will test it out later.

Good catch! thanks

i use the solution from you for this example, for my “sleep-mode”, but it dont work:

if i switch the input_boolean.sleep_mode_toggle from “off” to “on”, turn the entities off.

automations/sleep_mode.yaml

- id: sleep_mode_turn_off
  alias: "Sleep Mode - Turn off"
  mode: single
  trigger:
    - platform: state
      entity_id: input_boolean.sleep_mode_toggle
      to: "on"
  condition:
    - condition: state
      entity_id: input_boolean.sleep_mode_toggle
      state: "on"
  action:
    - service: homeassistant.turn_off
      entity_id: group.all_turn_off_entities

packages/turn_off/group_turn_off.yaml

group:
  # All Entities to switch turn off
  media_player_turn_off_entities:
    name: All Turn Off Entities
    entities:
      - media_player.ax_hd60_office
      - media_player.samsung_au8079_55_tv

mh, what is the right way?

i found the problem… “all_turn_off_entities” => “media_player_turn_off_entities” :slight_smile:

Good stuff here. I didn’t want to start my own thread but I’m in a similar boat. New to HA and trying to start doing some automations.

I’m approaching this a little more granularly as I work from home so I need some things turned off while others can be left on.

So, for instance my kids. Each has their own bedroom, each has a smart light and a Google Nest mini device. All of them are fond of leaving for school etc, and leaving their lights on with music playing on their speakers.

I’m trying do automate that when they leave our home zone (using device tracking with the HA companion app) that it checks to see if their light is on and/or if their music is playing and if either are, to turn them off.

Here’s what I have and this sort of half worked. It turned the light off but not the media.

  - platform: device
    device_id: 7390b7cbe92787085af7ed830b23ec47
    domain: device_tracker
    entity_id: 3f3cc9b689a8c461552b9449b7591093
    type: leaves
    zone: zone.home
  condition:
  - condition: device
    type: is_on
    device_id: e586a6c0a335396a4c7a01b7db2373f7
    entity_id: 742340b934f04e52b04a30ececd15c1b
    domain: light
  - condition: device
    device_id: d38cd53e12bce6fc6516ba06ab348cac
    domain: media_player
    entity_id: 1c2795ce2ebe5b2255d2a86fca02070a
    type: is_playing
  action:
  - type: turn_off
    device_id: e586a6c0a335396a4c7a01b7db2373f7
    entity_id: 742340b934f04e52b04a30ececd15c1b
    domain: light
  - service: media_player.turn_off
    data: {}
    target:
      device_id: d38cd53e12bce6fc6516ba06ab348cac
  mode: single```

Edit your post and do this: https://community.home-assistant.io/t/how-to-help-us-help-you-or-how-to-ask-a-good-question/114371#oneone-format-it-properly-16

Thanks, tom, how’s that?

1 Like

BTW, I was able to get mine working. I failed to use an “Or” condition. Once I did that, both devices in each room were turned off.

Would I be best to wrap the conditions in an “and”?

condition:
  - condition: and
    conditions:
      - condition: device
        device_id: 
        domain: device_tracker
        entity_id: 
        type: is_not_home
      - condition: device
        device_id: 
        domain: device_tracker
        entity_id: 
        type: is_not_home