Template to Entity/sensor?

Is it possible to get an entity/sensor or anything that I can use in the UI with this template?

I tried it and the config dident give me any errors, but nothing shows. I made other one and that worked as an sensor.

{{ states.light | selectattr('state', 'eq', 'on')
  | rejectattr('attributes.is_hue_group', 'true')
  | map(attribute='entity_id')
  | list | count }} / {{ states.light
  | rejectattr('attributes.is_hue_group', 'true')
  | map(attribute='entity_id')
  | list | count }}

Best regards
Thekholm

Where did you try it?

try a markdown card, this is what I have:

type: entities
title: Hue light groups
card_mod:
  style: |
    .card-header {
      background-color: var(--background-color-off);
      color: var(--text-color-off);
      padding-top: 0px;
      padding-bottom: 0px;
    }
entities:
  - type: custom:hui-element
    card_type: markdown
    card_mod:
      style: |
        ha-card.type-markdown {
          box-shadow: none;
          margin-top: 8px;
          overflow-y: scroll;
          height: 300px;
        }
    content: |
      {% set group_list = states.light
        |selectattr('attributes.is_hue_group','eq',true)|map(attribute='entity_id')|list%}

      {%- for g in group_list -%}
      {% set ns2 = namespace(lights=[]) %}
      {%- for name in state_attr(g,'lights') -%}
      {%- for l in states.light
        if l.attributes['friendly_name'] == name %}
      {% set ns2.lights = ns2.lights + [l.entity_id] %}
      {% endfor %}
      {% endfor %}
      **{{state_attr(g,'friendly_name')}}** (*{{states(g)}}*):
        {%- for l in ns2.lights %}
        > {{state_attr(l,'friendly_name')}}: *{{states(l)}}* since {{as_timestamp(states[l].last_changed,none)|timestamp_custom('%H:%M:%S %d %b',default=none)}}
        {%- endfor %}
      {% endfor %}

if you want to be puristic about it you can also use expand(integration_entities('Philips Hue 1')) in stead of states.light but that shows a lot of extra entities to be watched by the template editor (over 200 scenes alone…). So, using states.light cuts down on that for sure

Alternatively you can create a template sensor:


template:

  - sensor:
      - name: How many lights are on?
        unique_id: how_many_lights_on
        icon: |
          {% set l = states.light
          |selectattr('state', 'eq', 'on')
          |rejectattr('attributes.is_hue_group', 'true')
          |map(attribute='entity_id')
          |list
          %}
          {{ (l == 'on') |iif ('mdi:lightbulb-on-outline', 'mdi:lightbulb-off') }}        
        state: |
          {{ states.light | selectattr('state', 'eq', 'on')
            | rejectattr('attributes.is_hue_group', 'true')
            | map(attribute='entity_id')
            | list | count }} / {{ states.light
            | rejectattr('attributes.is_hue_group', 'true')
            | map(attribute='entity_id')
            | list | count }}
        attributes:
          path: /packages/0_sensoren/1_testsensor.yaml

how would that icon template work? it returns a list doesnt it? can not test that for being ‘on’ ?

          {% set l = states.light
          |selectattr('state', 'eq', 'on')
          |rejectattr('attributes.is_hue_group', 'true')
          |list|count
          %} {{l != 0}}

would suit ok?

Both ways are working:

EDIT: You also can kick the last two lines:


          {% set l = states.light
          |selectattr('state', 'eq', 'on')
          |rejectattr('attributes.is_hue_group', 'true')
          %}

Hi again Tom :slight_smile:

I tried it in the configuration file as a template.

template:
  - sensor:
    - name: Lights on of total amount. 
      state: >
        {{ states.light | selectattr('state', 'eq', 'on')
        | rejectattr('attributes.is_hue_group', 'true')
        | map(attribute='entity_id')
        | list | count }} / {{ states.light
        | rejectattr('attributes.is_hue_group', 'true')
        | map(attribute='entity_id')
        | list | count }}

I have it working in the UI as a Chip true Mushroom Cards (from hacs). But in that card I can put in the template code directly.

Best regards
Thekholm

Thanks Mariusrhbdb,
That didn’t get me to want I wanted, but cool to learn that I worked that way.

Even make the on states as bold text, nice :grin:

Best regards
Thekholm

Hi pedolsky,

I will try your template and see if that works as I want it.

Many thanks for your reply and help :+1:

Best regards
Thekholm

Okay. Now I’m really confused. If you have it working why did you ask this?

I don’t want to use Chip for this! I want to use it in entitie card if possible and maybe to an automation.

And also I want to learn what’s possible so I can use it on other devices.

Best regards
Thekholm

Small appendix:

If you want to see which lights are on, add this as an attribute:

      attributes: 
        lights_on: |
          {{ states.light
          |selectattr('state', 'eq', 'on')
          |rejectattr('attributes.is_hue_group', 'true')
          |map(attribute='name')
          |join('\n')
          }}

Nice :slightly_smiling_face:

Thanks for all help, if I want to exlude one light from list, can I do that?

best regards:
Thekholm

Yes, that’s possible. Let’s say, I don’t want my „Kugellampe“ exposed:


          |rejectattr('entity_id', 'search', 'kugel')

search finds all kinds of expressions.

Worked great!

Do I do like this? If I want to exlude more than one, I just add this line again (but with other devices)?

{{ states.light
|selectattr('state', 'eq', 'on')
|rejectattr('entity_id', 'search', 'rum_group')
|rejectattr('attributes.is_hue_group', 'true')
|map(attribute='name')
|join('\n')
}}

/Thekholm

Yes, you could. But try this kind:


          |rejectattr('entity_id', 'search', 'kugel|hue|lese')

kicks out all my lights with kugel, hue and lese as part of their entity_id.

That’s great, I where thinking it should be something like that. I did it like this
|rejectattr('entity_id', 'search', 'kugel', 'hue', 'lese')

Many thanks again for all the help!

/Thekholm

as an update, the template I posted above needs:

{% set group_list = states.light
        |selectattr('attributes.is_hue_group','defined')|map(attribute='entity_id')|list %}

nowadays. otherwise it errors with

UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'is_hue_group'

because not all light entities have that attribute. Somehow that is exactly what I wanted to do of course, select those entities that Do have that attribute…

the earlier method no longer works, we need to use the ‘defined’ test.

see: Template filters fail when some state objects don't are missing an attribute · Issue #71921 · home-assistant/core · GitHub posting here for reference.