Multi-Toggle Script

Something I frequently want to program my buttons to do is to toggle a set of lights. If any of the lights are on, they should all turn off. Otherwise, they should all turn on.

I would like to create a re-usable script for this use case, but I’m struggling with what to put in the if:

sequence:
  - if:
      - condition: template
        value_template: "{{ target | select(\"is_state\", \"on\") | first }}"
    then:
      - action: homeassistant.turn_off
        metadata: {}
        data: {}
        target:
          entity_id: "{{ target }}"
    else:
      - action: homeassistant.turn_on
        metadata: {}
        data: {}
        target:
          entity_id: "{{ target }}"
fields:
  target:
    selector:
      entity:
        multiple: true
    required: true
alias: Smart Toggle
description: >-
  Toggle the state of a set of entities. If any one is on, turns them all off.
  Otherwise, turns them all on.

Unfortunately, this doesn’t work. The else branch always executes. How can I write a template that works with the incoming field properly?

"{{ target | select('is_state', 'on') | list | count > 0 }}"