Turn template to use 'off' instead of 'on'

using @petro 's template for a button-card, I am struggling to have it show/count the ‘off’ entities and find the correct template to show that on the button…

this is the template in its regular form:
count lights ‘on’:

counter_lights:
  template: button_body
  layout: icon_state
  show_state: false
  show_name: true
  show_label: true
  label: >
    [[[
      var i;
      var status = entity.state;
      var entities = entity.attributes.entity_id;
      var count = 0;
      for (i = 0; i < entities.length; i++) {
        var state = states[entities[i]].state;
        if (state === 'on') {
          count += 1;
        }
      }
      var phrase = (count === 1 ? ' light ' : ' lights ');
      return (status === 'on' ? count.toString() + phrase + status : status);
    ]]]

so I thought to replace ‘on’ with ‘off’ would count my ‘off’ hubs,

  label: >
    [[[
      var i;
      var status = entity.state;
      var entities = entity.attributes.entity_id;
      var count = 0;
      for (i = 0; i < entities.length; i++) {
        var state = states[entities[i]].state;
        if (state === 'off') {
          count += 1;
        }
      }
      var phrase = (count === 1 ? ' Hub ' : ' Hubs ');
      return (status === 'off' ? count.toString() + phrase + status : status);
    ]]]

but then it won’t show 0 offline, instead it shows:
37

I’ve tried something like:

  label: >
    [[[
      var i;
      var status = entity.state;
      var entities = entity.attributes.entity_id;
      var count = 0;
      for (i = 0; i < entities.length; i++) {
        var state = states[entities[i]].state;
        if (state === 'off') {
          count += 1;
        }
      }
      var phrase = (count === 1 ? ' hub ' : ' hubs ');
      return (status === 'off' ? count.toString() + phrase + status : 'All hubs online');
    ]]]

and then I always see the else clause:

16
what am I doing wrong here?
thanks for having a look!

can you share all the entities inside the group and their states?

hi! got pinged…

sure:

hubs_binary_pinged:
  name: Hubs pinged binary
  icon: mdi:server-network
  entities:
    - binary_sensor.hassio_rpi4
    - binary_sensor.hassio_mqtt
    - binary_sensor.hassio_z_wave
    - binary_sensor.asus_router
    - binary_sensor.iungo
    - binary_sensor.solaredge
    - binary_sensor.synology
    - binary_sensor.timecapsule
    - binary_sensor.ikea_tradfri
    - binary_sensor.philips_hue
    - binary_sensor.kantoor_thermostat

and they’re all ‘on’ right now. Used in this Lovelace setup

          - type: custom:button-card
            entity: group.hubs_binary_pinged
            template: counter_hubs

with the above mentioned templates

wait, it now dawns on me, should I maybe use all: true for the group? It then would be ‘off’ if 1 of the entities would be off, and make the template work as desired?

update

this now seems to do it, but maybe you dont agree or see an error:

      var phrase = (count === 1 ? ' Hub ' : ' Hubs ');
      return (status === 'off' ? count + phrase + status : count + phrase +' offline');

it looks fine, probably can remove the () as they aren’t needed. But the logic doesn’t make sense. You mine as well just remove the last if statement because you’re always displaying the count and status.

not completely: if the group is on, no hubs are ‘off’. So, if I need to show 0 hubs offline, I must hard code the ’ offline’ and use the count. Guess it all depends on what you want the button to signal. I like the conformation ‘all is well’, read: nothing is offline ;-), Or maybe nicer, ‘All online’ in which case the else could be 'All ' + status

thanks!

1 question though: why do you use the count.toString() instead of the simple count ? Seems not to make a difference in the button template.

I’m not 100% familiar with js. Usually adding a string and an int will produce an error. Converting the int to a string safely allows for string addition. If it’s not needed, remove it.

ok, thanks, will keep that in mind for sure, so far so good though.