Sensor: show all switches with on state and a nice , in between the names

Hi All,

I created a sensor that show me all switched that are on. Its not the best way (im not a programmer) but it work.

When 1 switch is on it need to show just the switch. When 2 switches are there must a , in between.
As you can see, I add them already because I don’t know how to do this. Maybe someone can build this better?

platform: template
sensors:
 other_active:
    friendly_name: "Other "
    value_template: >-
      {% if states('switch.dnc') == "on" %} DNC, {% endif %}
      {% if states('switch.kay_learning') == "on" %} Kay Learning, {% endif %}
      {% if states('switch.lux_low') == "on" %} Lux Low, {% endif %}
      {% if states('switch.vacation') == "on" %} Emu. Holiday, {% endif %}
      {% if states('group.circadian_lights') == "on" %} Cir. Lightning, {% endif %}
      {% if states('group.zwave_switches') == "on" %} ZWave Switches, {% endif %}
      {% if states('switch.toilet_fan') == "on" %} Toilet Fan, {% endif %}

Depends.
What is your definition of better?

Is it shorter code? Or is it a better output?
I would probably be all anal and make sure there is comma between the switches except the last one which should be “and” and end with a dot.
And make sure I only use capital letter on the first, but that is a preference.
And lastly, if one switch is on there should not be a comma.

But it’s all what you mean by “better”.

This is mean thing to make it better. Is cosmetic nicer when 1 switch is on then there is no , behind :slight_smile:

I’m not good with templates.
But I managed this far.

The issue is I don’t know how to get an array with the devices that is on. Someone will probably help you with that.


{% set arr = ["DNC","Lux Low","Toilet Fan"] %} # this needs to be dynamic somehow....

{%- if arr | count == 0 %}
    Everything is off.
{%- elif arr | count == 1 %}
    Currently {{arr[0]}} is on.
{% else %}
    {% for switch in arr -%}
        {%- if loop.first %}Currently {% elif loop.last %} and {% else %}, {% endif -%}
        {{switch}}
    {%- endfor %} is on.
{% endif -%}

Im not that good with this kind of coding. Seems nice !! Now is the trick to get the entity that are on in the set arr

This might work…

As I said, I’m not good with templates so I had to build a string that I later split to an array.
I tested it with some of my switches and it seems to work.

{% set str = "" %}

{% if states('switch.dnc') == "on" %} {% set str = str + ":DNC" %} {% endif %}
{% if states('switch.kay_learning') == "on" %} {% set str = str + ":Kay Learning" %} {% endif %}
{% if states('switch.lux_low') == "on" %} {% set str = str + ":Lux Low" %} {% endif %}
{% if states('switch.vacation') == "on" %} {% set str = str + ":Emu. Holiday" %} {% endif %}
{% if states('group.circadian_lights') == "on" %} {% set str = str + ":Cir. Lightning" %} {% endif %}
{% if states('group.zwave_switches') == "on" %} {% set str = str + ":ZWave Switches" %} {% endif %}
{% if states('switch.toilet_fan') == "on" %} {% set str = str + ":Toilet Fan" %} {% endif %}

{% set arr = str[1:].split(":") %}

{%- if str | length == 0 %}
    Everything is off.
{%- elif arr | count == 1 %}
    Currently {{arr[0]}} is on.
{% else %}
    {% for switch in arr -%}
        {%- if loop.first %}Currently {% elif loop.last %} and {% else %}, {% endif -%}
        {{switch}}
    {%- endfor %} is on.
{% endif -%}
1 Like

Add in template and get the correct output !! Your better then you think with coding :smiley:

1 change last is on changed to are on because there are more then 1 on.

1 Like

True, “are on”…
That is the second to last line after endfor just replace that part.

with my last text line i want say I already changed it .

1 Like