PSA: Turn on/off all lights in Home Assistant 0.104+ (group.all_* changes)

OK. If you want to update the Template Sensor more frequently than every minute, create an automation, triggered by Time Pattern (using whatever short interval you require), that runs the homeassistant.update_entity service (to refresh the Template Sensor).

Wow that’s clever!
Thank you.

Would that put a significant load to the machine or is it acceptable / minimal?

create_every_device_group:
    sequence:
      - service: group.set
        data_template:
          object_id: "all_devices"
          entities: >
            {{ states.device_tracker |  map(attribute='entity_id') | join(',') }}
create_every_light_group:
    sequence:
      - service: group.set
        data_template:
          object_id: "all_lights"
          entities: >
            {{ states.light |  map(attribute='entity_id') | join(',') }}

adding this worked great alongside running these two scripts at startup

thanks to brianhanifin

1 Like

In a badge, for an Entity Button Card,

entity: all
hold_action:
  action: none
name: Close all lights
show_icon: false
show_name: true
tap_action:
  action: call-service
  service: light.turn_off
  service_data:
    entity_id: group.all_lights
type: entity-button

Doesn’t work. I get

Invalid Entity

entity: all
1 Like

Do you have an automation running them on every startup?
Also could there be a way to select only the entity_id’s of the items that have a specific attribute? (like device_clase:motion)

Nevermind, here is how i did it: Create groups with the device_class i want, automation to set them up on startup and a binary sensor to show them to me as safety

script:
  create_group_all_motion_sensors:
      sequence:
        - service: group.set
          data_template:
            object_id: "all_sensors_motion"
            entities: >
              {{ states.binary_sensor | selectattr('attributes.device_class','eq','motion') | map(attribute='entity_id') |  join(', ') }}
  
  create_group_all_opening_sensors:
      sequence:
        - service: group.set
          data_template:
            object_id: "all_sensors_opening"
            entities: >
              {{ states.binary_sensor | selectattr('attributes.device_class','eq','opening') | map(attribute='entity_id') | join(', ') }}

automation:
- alias: Startup Stuff - Notifications
    id: cea3ed9689054b0dasdadadsasdw
    trigger:
    - event: start
      platform: homeassistant
    action:
    - data:
        message: HomeAssistant is up
      service: telegram_bot.send_message
#......
    - alias: Create All Groups
      entity_id: script.create_group_all_motion_sensors, script.create_group_all_opening_sensors
      service: script.turn_on

binary_sensor:
#...
  - platform: template
    sensors:
#....
      all_motion_sensors_off:
        friendly_name: "No Motion"
        value_template: "{{ is_state('group.all_sensors_motion', 'on') }}"
        device_class: safety
        
      all_doors_closed:
        friendly_name: "All Doors Closed"
        value_template: "{{ is_state('group.all_sensors_opening', 'on') }}"
        device_class: safety
#had to class them as safety in order to exclude them from the previous group

When at least one motion detector is on:
image

Thanks for all the input guys :slight_smile:

1 Like

Using this in a script:

    - service: light.turn_off
      entity_id:
        - all

Gives me this error:

Invalid config for [script]: not a valid value for dictionary value @ data['script']['all_lights_off']['sequence'][1]['entity_id']. Got ['all'].

Any idea?

Adjusted to this, fixed it:

    - service: light.turn_off
      entity_id: all

A little bit of an aside from group.all_*: My code checked for each explicit person to create a binary_sensor.anyone_home, which is a little easier to utilize elsewhere in triggers and conditions. I’ll probably update to your condition template approach but wanted to mention the binary_sensor in case its useful to others.

binary_sensor.yaml file

- platform: template
  sensors:
    anyone_home:
      device_class: presence
      friendly_name: 'Anyone Home'
      value_template: "{{ is_state('person.one', 'home') or is_state('person.two', 'home') }}"

Uses:

 trigger:
    platform: state
    entity_id: binary_sensor.anyone_home
    to: 'on'
 - condition: template
   value_template: "{{ is_state('binary_sensor.anyone_home', 'on') }}"

I tried many combinations but nothing works :frowning: What can I use in Lovelace to replace an entity called group.all_lights in an Entity Button Card? The reason for this is to have a single button in the UI to turn off all the lights at once.

1 Like

My question exactly! It is sad the group.all_* was thrown away as it was very simple, yet totally adequate tool for an amateur like myself. :worried:

1 Like

Just use a script as the entity. Are you trying to gain ‘on’ and ‘off’ functionality? If so, you can make a toggle script.

script:
  all_lights:
    sequence:
      service_template: >
        {% set domain = 'light' %}
        {% set state = 'on' %}
        {% set is_on = states[domain] | selectattr('state','eq', state) | list | count > 0 %}
        {{ domain }}.turn_{{ 'off' if is_on else 'on' }}
      data_template:
        entity_id: >
          {% set domain = 'light' %}
          {% set state = 'on' %}
          {% set is_on = states[domain] | selectattr('state','eq', state) | list | count > 0 %}
          {% set state = 'off' if is_on else 'on' %}
          {{ states[domain] | selectattr('state','eq',state) | map(attribute='entity_id') | list | join(', ') }}
1 Like

Here is the explanation for it.

Thanks, that did the trick :slight_smile:

Here’s what my Lovelace code looks like:

entity: script.all_lights
hold_action:
  action: none
name: Fermer les lumières
show_icon: false
show_name: true
tap_action:
  action: call-service
  service: light.turn_off
  service_data:
    entity_id: all
type: entity-button
theme: slate
1 Like

My node red automation was based upon the status of device tracker's all devices group. Specifically two mobile phones who's combined bluetooth proximity status set the group to home or not_home. This turned my security cameras on and off. It all worked beautifully. Sadly, most of the fixes mentioned in this thread are way over my head. Please can a kind soul help this non-programmer layman?

You only need to create that group manually
Info how:

see my post above

create_every_device_group:
    sequence:
      - service: group.set
        data_template:
          object_id: "all_devices"
          entities: >
            {{ states.device_tracker |  map(attribute='entity_id') | join(',') }}
create_every_light_group:
    sequence:
      - service: group.set
        data_template:
          object_id: "all_lights"
          entities: >
            {{ states.light |  map(attribute='entity_id') | join(',') }}

Edit. Thanks for that. It took me a while to realize it was a script, after adding to scripts.yaml and reloading it appears to work!

Glad you got it working, you will also have to create an automation to load those 2 scripts upon restart of home assistant

I agree with pollinolas:

For just two mobile phones, here is all that’s needed. In groups.yaml:

  all_trackers:
    entities:
      - device_tracker.first_phone
      - device_tracker.second_phone

If you get a third mobile phone, append it to the group and click:
Configuration > Server Controls > Reload Groups.

I’m assuming it’d be something on the lines of trigger type home assistant (start), call service, script.create_every_device_group?

Thanks. I initially tried that, but one device leaving Bluetooth trackers proximity triggered the automation. I needed both devices status to be in ‘conjoined’ (for want of a better word) for it to work.