How to trigger alarm when two or more sensors activate

Hey everyone, I have been looking for a generic script to trigger my alarm system when two or more sensors activate at the same (period of) time.
So far I have tried this and no luck:

I have tried to write a code myself, but I can’t get the grip of it.

Anyone has come across a good script or can give some pointers to write one.

Appreciate!!

Post what you have tried so far. please make sure to properly format the code block.

I use the same logic that the link I show before

here is the code:

binary_sensors.yaml

  - platform: template
    sensors:
      motion_arriba_on_prolonged:
        value_template: "{{ is_state('binary_sensor.motion_arriba_occupancy', 'on') }}"
        delay_off:
          seconds: 30

      motion_cocina_on_prolonged:
        value_template: "{{ is_state('binary_sensor.motion_cocina_occupancy', 'on') }}"
        delay_off:
          seconds: 30

      motion_comedor_on_prolonged:
        value_template: "{{ is_state('binary_sensor.motion_comedor_occupancy', 'on') }}"
        delay_off:
          seconds: 30

      motion_hall_on_prolonged:
        value_template: "{{ is_state('binary_sensor.motion_hall_occupancy', 'on') }}"
        delay_off:
          seconds: 30

      motion_living_on_prolonged:
        value_template: "{{ is_state('binary_sensor.motion_living_occupancy', 'on') }}"
        delay_off:
          seconds: 30

      motion_escalera_on_prolonged:
        value_template: "{{ is_state('binary_sensor.motion_escalera_occupancy', 'on') }}"
        delay_off:
          seconds: 30

      motion_pieza_on_prolonged:
        value_template: "{{ is_state('binary_sensor.motion_pieza_occupancy', 'on') }}"
        delay_off:
          seconds: 30

      pir_cross_sensor:
        entity_id:
          - binary_sensor.motion_arriba_occupancy
          - binary_sensor.motion_cocina_occupancy
          - binary_sensor.motion_comedor_occupancy
          - binary_sensor.motion_hall_occupancy
          - binary_sensor.motion_living_occupancy
          - binary_sensor.motion_escalera_occupancy
          - binary_sensor.motion_pieza_occupancy
          - group.cross_sensors_group
        value_template: >-
          {% set ns = namespace(found=0) %}
          {% for entity_id in expand('group.cross_sensors_group') if is_state(entity_id, 'on') and (ns.found < 2) %}
            {% set ns.found = ns.found + 1 %}
          {% endfor %}
          {{ ns.found == 2 }}

  - platform: group
    name: "cross sensors group"
    entities:
    - binary_sensor.motion_arriba_on_prolonged
    - binary_sensor.motion_cocina_on_prolonged
    - binary_sensor.motion_comedor_on_prolonged
    - binary_sensor.motion_hall_on_prolonged
    - binary_sensor.motion_living_on_prolonged
    - binary_sensor.motion_escalera_on_prolonged
    - binary_sensor.motion_pieza_on_prolonged

when I activate two sensors it does not activate pir_cross_sensor.

The entity_id option for a Template Sensor no longer exists.

The only entity monitored for sensor.pir_cross_sensor is group.cross_sensors_group.

I suggest you consider using a Trigger-based Template Sensor.

I follow. I try to add this to the trigger section but my lack off expirence wont let me. Can you give me a little help once again? I fail to indent and I keep getting all sort of errors. I still have huge problems when is in splited files like binary_sensors.yaml.

trigger:
  - platform: state
    entity_id:
      - binary_sensor.motion_arriba_occupancy
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.motion_cocina_occupancy
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.motion_comedor_occupancy
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.motion_hall_occupancy
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.motion_living_occupancy
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.motion_escalera_occupancy
    to: "on"
  - platform: state
    entity_id:
      - binary_sensor.motion_pieza_occupancy
    to: "on"

Template Sensors can be defined two ways:

  1. Legacy format
    This is the way you have configured your Template Sensors and is the traditional, or “legacy”, method. The configuration of legacy method Template Sensors is placed under the sensor: key.
  2. Modern format
    This is the new way of configuring a Template Sensor and it is placed under the template: key (not the sensor: key). Its option names are different from legacy method. Although Home Assistant accepts Template Sensors configure din either legay or modern format, only the modern format can be used to configure a Trigger-based Template Sensor, not the legacy method.

Here is binary_sensor.pir_cross_sensor defined as Trigger-based Template Sensor in modern format. Add it to configuration.yaml file. However, if your configuration.yaml file contains template: !include templates.yaml then add it to templates.yaml (but in that case, remove the first line shown below).

template:
  - trigger:
    - platform: state
      entity_id:
        - binary_sensor.motion_arriba_occupancy
        - binary_sensor.motion_cocina_occupancy
        - binary_sensor.motion_comedor_occupancy
        - binary_sensor.motion_hall_occupancy
        - binary_sensor.motion_living_occupancy
        - binary_sensor.motion_escalera_occupancy
        - binary_sensor.motion_pieza_occupancy
      to: "on"
      binary_sensor:
        - name: pir cross sensor
          state: "{{ expand('group.cross_sensors_group') | selectattr('state', 'eq', 'on') | list | count >= 2 }}"

For more information, I recommend you review the documentation for Template Sensor, especially the initial section describing the new way of configuring them.