Script bug - any input appreciated

The following code checks through a number of sensors to see if any are open. If any are open the “all_closed” is set to false and a message is created to tell me which sensors are open.
The bug is that I can see in the first loop that “all_closed” is evaluating to false (as I have some sensors open when testing) in the loop be is somehow evaluating to true before it choses what do. I assume it is re initialising the “all_closed” variable at the start of the script but I can’t see how

alias: Check Sensors Status
variables:
  message: ""
  all_closed: true
  sensors:
    - entity_id: binary_sensor.s_up_back_win
      alias: Back Windows
    - entity_id: binary_sensor.s_back_gar_door
      alias: Rear Garage Door
    - entity_id: binary_sensor.s_toilet_win
      alias: Bathroom Window
    - entity_id: binary_sensor.s_dine_play_room
      alias: Dining Room Playroom Windows
    - entity_id: binary_sensor.s_up_front_win
      alias: Upstairs Front Windows
    - entity_id: binary_sensor.s_main_gar_door
      alias: Main Garage Door
    - entity_id: binary_sensor.s_side_gar_win
      alias: Side Garage Window
    - entity_id: binary_sensor.s_kitchen_win
      alias: Kitchen Window
    - entity_id: binary_sensor.s_living_room
      alias: Living Room Window
    - entity_id: binary_sensor.s_master_win
      alias: Master Bedroom Window
    - entity_id: binary_sensor.s_piz_area_door
      alias: Pizza Area Door

sequence:
  - repeat:
      for_each: "{{ sensors }}"
      sequence:
        - choose:
            - conditions:
                - condition: template
                  value_template: "{{ is_state(repeat.item.entity_id, 'on') }}"
              sequence:
                - variables:
                    all_closed: false
                - data:
                    title: Checking sensors...
                    message: "{{ repeat.item.alias }} is open. {{ all_closed }}"
                  action: persistent_notification.create
                - data:
                    entity_id: input_text.open_sensors_message
                    value: >
                      {{ message }}{{ repeat.item.alias }} is open.. compiled
                      message. {{ all_closed }}
                  action: input_text.set_value
  
  - action: notify.persistent_notification
    metadata: {}
    data:
      message: Status of all_closed variable .. {{ all_closed }}
  
  - choose:
      - conditions:
          - condition: template
            value_template: "{{ all_closed }}"
        sequence:
          - service: input_boolean.turn_on
            data:
              entity_id: input_boolean.all_sensors_closed
          - service: persistent_notification.create
            data:
              title: Sensor Check Result
              message: All sensors are closed.
      - conditions:
          - condition: template
            value_template: "{{ not all_closed }}"
        sequence:
          - service: input_boolean.turn_off
            data:
              entity_id: input_boolean.all_sensors_closed
          - service: persistent_notification.create
            data:
              title: Sensor Check Result
              message: "{{ states('input_text.open_sensors_message') }}"

A group does this with a lot less mucking around.

Default behaviour is any binary sensor on → binary sensor group on.

2 Likes

Thanks, i hadn’t thought about using a group but now that you mention it. It makes better sense …
Will give that a go later …