Turn off all lights or just some based on presence

Hello
I have a button that will turn off all the lights in my house. But when my son is there I would like to leave some of the lights on. I use presence detection. So how can I make a button that will either turn off all lights or just some based on if his phone is there or not?
I have not used templates or scripts yet so if this is involved please explain it in an easy way if possible, :slight_smile:

No templates or or scripts required. Simplified automation (you may wish to add an alias, intital state, change the entity id’s to groups of lights or lists of lights, etc…):

trigger:
  platform: state
  entity_id: switch.button
  to: 'on'
action:
  - service: light.turn_off
    entity_id: light.some_lights
  - condition: state
    entity_id: tracker.sons_phone
    state: 'not_home'
  - service: light.turn_off
    entity_id: light.some_other_lights

The other lights will only turn off if your son’s phone is not home.

It is that easy? I am thinking too advanced I guess. But a couple of follow up questions:
How do I make the button?
When the button is pressed its state will be “on”. So what is the best way to automatically turn the button off again without triggering anything?

What sort of button is it?

I just assumed you already had it in home assistant as an entity when you said “I have a button…”

Stop pressing it?

It’s early in the morning here and my head does not work properly, so I see I did not explain this very well :slight_smile: Yes I have an entity button card on my frontend that turn off the entity called all lights (The lights are in a group). I could change this to run an automation instead, but I can not make an automation without a trigger? I can not find the entity button on my frontend as a switch to use as a trigger.

I appreciate your patience…

Ah I see (I was thinking hardware button).

We’ll use a script instead. Set up the tap action of your button to call this sort of script (script.lights_off):

lights_off:
  sequence:
  - service: light.turn_off
    entity_id: light.some_lights
  - condition: state
    entity_id: tracker.sons_phone
    state: 'not_home'
  - service: light.turn_off
    entity_id: light.some_other_lights

Scripts are services so all you need in your button is:

tap_action:
  action: call-service
  service: script.lights_off

I guess that is the automatic group. What you need to do is create an extra group with only the lights to turn off when your son is at home. The script will turn off that group first and then all lights when no one is at home.

Yes the all lights group is a group from Telldus. I have also made a group in Telldus with just the lights.
What i am trying to achieve is to have one button in my frontend that will normally turn off all the lights when I click it. I have an entity button today that turn off a group from telldus called light.all_lights. But when my son is home I would like a few lights to stay on, because he is a little afraid of the dark if he wakes up at night to go to the bathroom.
So normally the button should turn off all the lights no matter if we are at home or not, but if he is home 2 lights should not turn off.
I could of course make another entity button that turn off another group when he is home, but would like to have only one button that turn off either all the lights, or most lights if he is at home.

That’s what the script and tap action above will do.

Use the light group to create your two groups of lights (“off if son home” and “rest of the lights”) rather than a bog standard group. That way the groups actually act as lights. e.g.

  • The groups look like a light entity if added to the lovelace fronend.
  • You can also control other attributes of the group like brightness and colour (if available in the light).
  • You can use the light.turn_on/off services rather than the homeassistant.turn_on/off services usually used for groups.

I am sorry but I don’t seem able to wrap my head around the logic in this :-). But the first group with the condition is the lights that should stay on when he is home? So that group will only turn off if he is not home. Then the rest of the lights will turn off if he is home or not?

My lights are both lights and switches. Can I add switches to a light group?

No. Not without using light switch which you may want to do if you want those switches to look like lights in the front end. But let’s forget that for now.

Normal groups it is then:

group_1
Only the lights and switches you want to turn off when your son is home. In the script below they will however also turn off if your son is away. But that’s ok. If he’s away we want to turn all the lights off.

group_2
The rest of the lights and switches you want to turn off if your son is away.

New script using the correct services for groups:

lights_off:
  sequence:
  - service: homeassistant.turn_off # always turn these lights off
    entity_id: group.group_1
  - condition: state
    entity_id: tracker.sons_phone # only turn these lights off if son is away
    state: 'not_home'
  - service: homeassistant.turn_off 
    entity_id: group.group_2

The tap action for the button remains the same as in my post above.

Breaking this down and removing the condition and everything after it that wont run if your son is home it looks like this to home assistant (turn group_1 off):

lights_off:
  sequence:
  - service: homeassistant.turn_off # always turn these lights off
    entity_id: group.group_1

However if your son is away it looks like this (turn all lights off):

lights_off:
  sequence:
  - service: homeassistant.turn_off # always turn these lights off
    entity_id: group.group_1
  - service: homeassistant.turn_off # turn these off as well.
    entity_id: group.group_2

Aha, now I understand. Being new to scripts I thought the condition belonged to the first group, and that it would only turn off when the state of his phone was not_home, but I understand now that it won’t go any further in the script if the condition is not met.

1 Like

Thanks for all the help with this. It works very well, apart from that one of my lights will not turn off, not in HA and not physically. I have double checked that it is correct in the group. I can control it from the front end manually, but it will not react when tapping the button I made. I have even added it separately to the script but still nothing happens. I don’t understand why.

Are you sure you have the correct entity id for the light?

Yes. I have even copied it instead of typing it.

image

Check the entity id of this entity you are controlling manually.

The entity is really a switch, which I turned into a light using light switch in my configuration. I did it with several others that work as they should. I tried adding the original switch to the group as well, but same thing happens: nothing.
I have split my configuration and have a groups.yaml. I guess it should not matter what I put in the group, since it just a plain group. It should turn off whatever I have in the group.?

there’s an alternative possible route I tend to use with conditional actions, a service_template. This then can very easily use tailor-made scripts. relieving you from writing complex conditional scripts in the first step of the system logic. Like:

  - service_template: >
      script.turn_off_{{'selection' if states('device_tracker.son') == 'home' else 'all'}}

script:
  turn_off_all:
    sequence:
      service: light.turn_off
      entity_id: all

  turn_off_selection:
    sequence:
      service: light.turn_off #or homeassistant.turn_off when using a group as entity_id
      entity_if:
        - light.1
        - light.2
        - light.3

have look if you can use that. The top service template can be used in an automation, or another script, depending on whether you want to trigger manually or by pushing a physical button, or, by some other system logic calling this automatically.

That does not matter. Check the entity id of the entity you can successfully control in the front end (lok at the lovelace card that contains it). Add that to the group.