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?
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.
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 -%}
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 -%}