Device_class motion for a group?

I stole this code from someone in the community a while back, and modified it to my needs:

script:
#...[]...
  create_group_all_motion_sensors:
    sequence:
    - service: group.set
      data_template:
        object_id: all_sensors_motion
        entities: "{{ states.binary_sensor \n    | selectattr('attributes.device_class','eq','motion')\
          \ \n    | map(attribute='entity_id') \n    | reject('eq', 'binary_sensor.all_motion_sensors_off')\n\
          \    | reject('eq', 'binary_sensor.motion_sensor_158d0003cbf138')\n    | reject('eq',\
          \ 'binary_sensor.motion_sensor_158d0002f8f2c9')\n    | reject('eq', 'binary_sensor.motion_stairs')\n\
          \    |  join(', ') }}\n"
  create_group_all_opening_sensors:
    sequence:
    - service: group.set
      data_template:
        object_id: all_sensors_opening
        entities: "{{ states.binary_sensor \n    | selectattr('attributes.device_class','eq','opening')\n\
          \    | map(attribute='entity_id') \n    | reject('eq', 'binary_sensor.all_doors_closed')\n\
          \    | join(', ') }}\n    \n    \n"

They essensially get all sensors with a specific device class, in my case motion and opening and put them in a group. I also exclude some sensors i didnt want included.

Those two scripts run on my startup routine with this automation:

- alias: Startup Stuff - Notifications
  id: cea3ed9689054b0dac9a73ac9fc63091
  trigger:
  - event: start
    platform: homeassistant
  action:
  - data:
      message: HomeAssistant is up
    service: notify.mobile_app_sm_n975f
  - alias: Create All Groups
    entity_id: script.create_group_all_motion_sensors, script.create_group_all_opening_sensors
    service: script.turn_on

Hope it helps someone.

ping @Tpm369

2 Likes