custom-cards / secondaryinfo-entity-row

Custom entity row for HomeAssistant, providing additional types of data to be displayed in the secondary info area of the Lovelace Entities card
169 stars 15 forks source link

[Help] Cannot parse entity name with config.entity #51

Closed chemelli74 closed 7 months ago

chemelli74 commented 1 year ago

Seems I cannot use the object config.entity from custom:auto-entities in custom:secondaryinfo-entity-row. While it works fine for ":host"

Any hint ?

The goal is to extract a part of the entity name and show it as secondary row (due to ESPhome not handling attributes...).

Simone

type: custom:auto-entities
card:
  type: entities
  title: Stato batterie CR-2032
  show_header_toggle: false
  state_color: true
filter:
  include:
    - entity_id: '*xiaomi*battery*'
      options:
        style: |
          :host {
            --paper-item-icon-color: 
             {% if states(config.entity) | int(0) >= 90 %}
                  green
             {% elif states(config.entity) | int(0) >= 15 %}
                  yellow
             {% else %}
                  red
             {% endif %}; 
          }
        type: custom:secondaryinfo-entity-row
        secondary_info: "{{ config.entity[22:-14] }}"
sort:
  method: state
  reverse: true
  numeric: true
ildar170975 commented 1 year ago

Works on my side.

  - type: custom:auto-entities
    card:
      type: entities
      state_color: false
    filter:
      include:
        - entity_id: '*testing_device_class_battery*'
          domain: sensor
          options:
            type: custom:secondaryinfo-entity-row
            secondary_info: xxxxx
            card_mod:
              style: |
                :host {
                  {% if states(config.entity) | int(0) >= 67 -%}
                  --paper-item-icon-color: green;
                  {% elif states(config.entity) | int(0) >= 34 -%}
                  --paper-item-icon-color: yellow;
                  {% else -%}
                  --paper-item-icon-color: red;
                  {% endif %}
                }
    sort:
      method: state
      reverse: true
      numeric: true

Note that sensor entities of device_class: battery since 2022.12 have their own colors for icons dependingly on a state. It works when state_color: true and has hard-coded levels (find yourself these changes in a code).

chemelli74 commented 1 year ago
        type: custom:secondaryinfo-entity-row
        secondary_info: xxxxx

The issue in only related to the parsing of "xxxxx" value ;-)

Sorry for the bed title, just updated.

Simone

chemelli74 commented 1 year ago

Log report:

2022-12-11 22:39:55.126 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'config' is undefined when rendering '{{ config.entity[22:-14] }}'

ildar170975 commented 1 year ago

The config.entity is NOT related to auto-entities. What you used here was a card-mod feature and may be used inside card-mod templates only - unless this config object is documented as available like it is for custom:template-entity-row.

chemelli74 commented 1 year ago

Godo point, thx.

So which is the correct object to use ? this.entity doesn't work as well.

Simone

ildar170975 commented 1 year ago

Try to achieve your result w/o using another plugins - only with secondaryinfo-entity-row.

ildar170975 commented 1 year ago

So which is the correct object to use ? this.entity doesn't work as well.

Have not tried i by myself. If this variable does not work - then probably it happens due to "templates in the secondaryinfo-entity-card are resolved AFTER resolving this". I think so because secondary_info: this.entity_id is resolved into displaying the entity's id, but when placed inside a template ({..}, [[[..]]) - does not work.

For some cases a documented way may be used - i.e. [[ {entity}.state .... ]].

chemelli74 commented 1 year ago

For some cases a documented way may be used - i.e. [[ {entity}.state .... ]].

This works:

secondary_info: "[[ {entity}.entity_id ]]"

but the following not:

secondary_info: "[[ {entity}.entity_id[22:-14] ]]"

Any further idea ?

Simone

ildar170975 commented 1 year ago

entity_id[22:-14]

If this is jinjia - then you are mixing it with non-jinjia templates

chemelli74 commented 1 year ago

You have a suggested solution then ? Seems to me that non-jinjia is lacking the substring handling, and jinjia doesn't work with entity substitution.

Simone

ildar170975 commented 1 year ago

Seems to me that non-jinjia is lacking the substring handling, and jinjia doesn't work with entity substitution.

The best way is using auto-entities with a template filter to define a STRING which then will be used as a secondary-info. So you will use jinjia to make that "entity_id[22:-14]" operation.

Go to auto-entities community thread - you will find plenty of examples for that "template" filter.

chemelli74 commented 1 year ago

Go to auto-entities community thread - you will find plenty of examples for that "template" filter.

I must be blind, will keep searching; but if you have a link it would be much appreciated.

Simone

chemelli74 commented 1 year ago

Last test shows:

secondary_info: "[[ {entity} ]]" -> OK secondary_info: "{{ states('sensor.xiaomi_sensore_4_battery_level') }}" -> OK

secondary_info: '{{ states({entity}) }}' -> Nothing shown on UI, no error logged.

Simone

ildar170975 commented 1 year ago

link it would be much appreciated.

https://community.home-assistant.io/t/auto-entities-automatically-fill-cards-with-entities/147801

secondary_info: '{{ states({entity}) }}'

Mix of 2 alternative styles will not work.

chemelli74 commented 1 year ago

https://community.home-assistant.io/t/auto-entities-automatically-fill-cards-with-entities/147801

:-) I mean link to the comment with the example, not to the thread...

secondary_info: '{{ states({entity}) }}'

Mix of 2 alternative styles will not work.

So back to the main issue, there is no variable to replace the entity in jinjia style.

Simone

ildar170975 commented 1 year ago

I mean link to the comment with the example, not to the thread...

Search thread for examples.

there is no variable to replace the entity in jinjia style.

Good point, suggest you to close this issue and create a new FR to add a variable which may be used in jinjia templates.

Btw, I strongly suggest not to use GitHub for questions. Use GitHub for bugs & FRs only. Ask questions in the HA Community (there is a dedicated thread for this card) - there you will get answers faster (not to mention a fact that posting there = sharing knowledge with other people).