The automation of the air purifier according to the each sensoe8

How you every thing goes well.

I came here to ask you the correct script

I have two air purifier and total 4 dust sensor. And it is available to turn on/off the air purifier according to the figure of dust from each sensor. But, as I am adding to the window sensor , the issue was made.

What I would like to do is…

If the window is opened, the air purifier should not activated, though the dust from dust sensor is high.

Please review my script below, and let me know. The orininal script before I add the script related to window sensor was working. But the script now is not working.

- alias: Dust_airpurifier_ONOFF
  initial_state: True
  trigger:
    - platform: template
      value_template: "{% if states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20 %} true {% endif %}"    
    - platform: template
      value_template: "{% if states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20 or is_state("binary_sensor.door_window_sensor_158d0002a4fb2f", "on") % } true {% endif %}"    
    - platform: template
      value_template: "{% if states.sensor.pm10_1.state | float < 10 and states.sensor.pm10_2.state | float < 10 and states.sensor.pm25_1.state | float < 10 and states.sensor.pm25_2.state | float < 10 % } true {% endif %}" 
  action:       
    - service: notify.telegram
      data_template:
        message: >
          {% if states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20  or is_state("binary_sensor.door_window_sensor_158d0002a4fb2f", "on")  %}
            " high dust. But will not be working due to opened wibdow ."
          {% elif states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20 %}
            " Air purifier On."
          {% endif %}
          {% if states.sensor.pm10_1.state | float < 10 and states.sensor.pm10_2.state | float < 10 and states.sensor.pm25_1.state | float < 10 and states.sensor.pm25_2.state | float < 10  %}
            " Air purifier Off."
          {% endif %}
    - delay:
        seconds: 3 
    - service_template: >
        {% if states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20 and is_state("binary_sensor.door_window_sensor_158d0002a4fb2f", "on" %}
          switch.turn_on
        {% elif states.sensor.pm10_1.state | float < 10 and states.sensor.pm10_2.state | float < 10 and states.sensor.pm25_1.state | float < 10 and states.sensor.pm25_2.state | float < 10 %}
          switch.turn_off
        {% endif %}
      entity_id: 
        - switch.sonoff1_airpurifier1
        - switch.sonoff2_airpurifier2      
          
     ```

I think you might want:

- alias: Dust_airpurifier_ONOFF
  initial_state: True
  trigger:
    - platform: template
      value_template: "{{ states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20 }}"
    - platform: template
      value_template: "{{ states.sensor.pm10_1.state | float < 10 and states.sensor.pm10_2.state | float < 10 and states.sensor.pm25_1.state | float < 10 and states.sensor.pm25_2.state | float < 10 }}"
  action:
    - service: notify.telegram
      data_template:
        message: >
          {% if states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20 %}
            {% if is_state('binary_sensor.door_window_sensor_158d0002a4fb2f', 'on') %}
              High dust, but will not be working due to opened window.
            {% else %}
              Air purifier On.
            {% endif %}
          {% else %}
            Air purifier Off.
          {% endif %}
    - delay:
        seconds: 3
    - service_template: >
        {% if (states.sensor.pm10_1.state | float > 20 or states.sensor.pm10_2.state | float > 20 or states.sensor.pm25_1.state | float > 20 or states.sensor.pm25_2.state | float > 20) and is_state('binary_sensor.door_window_sensor_158d0002a4fb2f', 'off') %}
          switch.turn_on
        {% else %}
          switch.turn_off
        {% endif %}
      entity_id: 
        - switch.sonoff1_airpurifier1
        - switch.sonoff2_airpurifier2      

I really appreciate it. As I am in the office, I didn’t check. But there is no error in log after adding this script in ymal. Thank you very much and I am sure that it works.