Motion sensor "group" template

I’m trying to create a group of the entities that show there is occupance in my home. The template works, but it is being treated as a binary sensor (true/false). I’m trying to use this new sensor in an automation that expects a motion device. Adding device_class: motion is not valid.

I’m fairly new to Home Assistant customization and my configuration which started years ago may not be correct. I have this entry included below in a sensors.yaml file and I’m not sure if that is good practice.

 - platform: template
    sensors:
      occupancy_detected:
        value_template: >
          {{ is_state('device_tracker.pixel_7_pro_m', 'home')
             or is_state('device_tracker.pixel_7_pro', 'home')
             or is_state('binary_sensor.ecobee_sensor_occupancy', 'on')
             or is_state('binary_sensor.dining_room_occupancy', 'on')
             or is_state('binary_sensor.family_room_occupancy', 'on')
             or is_state('binary_sensor.main_occupancy', 'on') 

             or is_state('binary_sensor.claires_room_occupancy', 'on')
             or is_state('binary_sensor.guest_room_occupancy', 'on')
             or is_state('binary_sensor.wesley_s_room_occupancy', 'on')
             or is_state('binary_sensor.upstairs_occupancy', 'on')
             or is_state('binary_sensor.theater_occupancy', 'on')
             }}

Create a Template Binary Sensor. I suggest you use modern format as opposed to legacy format (that’s what your example is using).

template:
  - binary_sensor:
      - name: Occupancy Detected
        device_class: motion
        state: >
          {{ [ 'device_tracker.pixel_7_pro_m', 
               'device_tracker.pixel_7_pro',
               'binary_sensor.ecobee_sensor_occupancy',
               'binary_sensor.dining_room_occupancy',
               'binary_sensor.family_room_occupancy',
               'binary_sensor.main_occupancy',
               'binary_sensor.claires_room_occupancy',
               'binary_sensor.guest_room_occupancy',
               'binary_sensor.wesley_s_room_occupancy',
               'binary_sensor.upstairs_occupancy',
               'binary_sensor.theater_occupancy' ] 
             | map('states') | select('match', '(on|home)')
             | list | count > 0 }}

If you are unfamiliar with modern format, review the documentation for Template Sensors.

1 Like

Thank you, that works. I have to make some changes to bring everything modern.

FWIW, you’re under no obligation to convert all of your existing legacy-format Template Sensors to modern format. Legacy format wasn’t deprecated and continues to be supported but it won’t be receiving enhancements. I suggest you consider using modern format when creating a new Template Sensor or when modifying an existing one that’s in legacy-format.

Be advised that legacy format allowed you to define a friendly_name and, optionally, an entity_id (with different values).

In contrast, modern format only allows you to define name which is equivalent to the old friendly_name and it automatically generates the entity_id from that. You can’t define a different entity_id in the entity’s configuration. If you don’t like the generated entity_id, you can modify it afterwards via the UI (editing the entity’s properties).

Because of this difference, converting from legacy to modern can be tricky because if not done right you can lose the entity’s history (because during the conversion, the entity’s original entity_id might not be preserved). For instructions on how to perform a lossless conversion, I recommend this post:

2 Likes

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.