Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 95925

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

$
0
0

@Mariusthvdb wrote:

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!

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 95925

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>