lovelace-auto-entities icon indicating copy to clipboard operation
lovelace-auto-entities copied to clipboard

Usage of Markdown card

Open hasechris opened this issue 1 year ago • 3 comments

Hi,

i know, this is used for entities. but please hear me out.

I use an integration named "NINA" which is used in germany for the anouncement of weather events and hazards. This integration has a user-selectable amount of message-slots (i know, thats ugly).

If the user selects 5 slots, the integration will generate this list of entities:

  • binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1
  • binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_2
  • binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_3
  • binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_4
  • binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_5

Normally the state of these entities is "off" and the attribute list is this:

device_class: safety
friendly_name: Warning: Leinfelden-Echterdingen, Stadt (Esslingen - Baden-Württemberg) 1

I case of a weather hazard the attributes look like this:

headline: Amtliche WARNUNG vor STURMBÖEN
description: Es treten oberhalb 800 m Sturmböen mit Geschwindigkeiten zwischen 60 km/h (17 m/s, 33 kn, Bft 7) und 70 km/h (20 m/s, 38 kn, Bft 8) anfangs aus südwestlicher, später aus westlicher Richtung auf.
sender: Deutscher Wetterdienst
severity: Moderate
recommended_actions: ACHTUNG! Hinweis auf mögliche Gefahren: Es können zum Beispiel einzelne Äste herabstürzen. Achten Sie besonders auf herabfallende Gegenstände.
id: dwd.2.49.0.0.276.0.DWD.PVW.1691254800000.02b893ad-717b-437b-ae71-3b66c416096d.MUL
sent: 2023-08-05T18:59:08+02:00
start: 2023-08-05T19:00:00+02:00
expires: 2023-08-07T00:00:00+02:00
device_class: safety
friendly_name: Warning: Leinfelden-Echterdingen, Stadt (Esslingen - Baden-Württemberg) 1

So i thougth i could use your auto-entities plugin to include all these binary_sensor entities and then specify, which attributes i want to show in the markdown card.

Is this possible?

Just for Info: This is how i use the entities at the moment:

type: conditional
conditions:
  - entity: >-
      binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1
    state_not: 'off'
card:
  type: markdown
  content: >
    {% if
    is_state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'severity', 'Extreme') %} <ha-alert alert-type="error"> {% endif %}      {%
    if
    is_state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'severity', 'Severe') %} <ha-alert alert-type="error"> {% endif %}      {%
    if
    is_state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'severity', 'Moderate') %} <ha-alert alert-type="error"> {% endif %}      {%
    if
    is_state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'severity', 'Minor') %} <ha-alert alert-type="error"> {% endif %}

    {{state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'headline')}}</ha-alert>


    {{state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'description')}}


    **Schwere:**
    {{state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'severity')}}

    **Start:**
    {{state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'start')}}

    **Warnungsende:**
    {{state_attr('binary_sensor.warning_leinfelden_echterdingen_stadt_esslingen_baden_wurttemberg_1',
    'expires')}}

And this is how that looks: image

Greetings hasechris

hasechris avatar Aug 05 '23 19:08 hasechris

Similar questions should be asked in the dedicated Community thread - https://community.home-assistant.io/t/auto-entities-automatically-fill-cards-with-entities/147801, GitHub should be left for reporting bugs & proposing FRs.

If a markdown card is used to show a static content:

  - type: custom:auto-entities
    card_param: cards
    card:
      type: vertical-stack
    filter:
      include:
        - entity_id: input_boolean.test_boolean_*         ### define your own mask
          options:
            type: conditional
            conditions:
              - entity: this.entity_id
                state_not: 'off'
            card:
              type: markdown
              content: some content

Same when the content is dynamic (i.e. some jinja templates used) & does NOT depend of the currently processing entity_id:

            ...
            card:
              type: markdown
              content: >-
                {{ .... }}

or when the processed entity_id is NOT a part of a template:

            ...
            card:
              type: markdown
              content: >-
                {{ ... }}
                xxx: this.entity_id
                {{ ... }}

The problem is that that this.entity_id variable is not resolved inside these templates.

If your templates DO depend on the currently processed entity_id (as you have in your case) - then you need to use the template option:

  - type: custom:auto-entities
    card_param: cards
    card:
      type: vertical-stack
    filter:
      template: >-
        {% for entity in states.input_boolean|selectattr('entity_id','search','input_boolean.test_boolean_') -%}
          {%- if (entity.entity_id.split('test_boolean_')[1])|int(0) > 4 -%}
            {%- set CONTENT = 'xx: ' + entity.entity_id -%}
          {%- else -%}
            {%- set CONTENT = 'yy: ' + entity.entity_id -%}
          {%- endif -%}
          {{
            {
              'type': 'conditional',
              'conditions': [
                {
                  'entity': entity.entity_id,
                  'state_not': 'off'
                }
              ],
              'card': {
                'type': 'markdown',
                'content': CONTENT
              }
            }
          }},
        {%- endfor %}

That part simulates a dynamically defined content:

          {%- if .... -%}
            {%- set CONTENT = ... -%}
          {%- else -%}
            {%- set CONTENT = ... -%}
          {%- endif -%}

ildar170975 avatar Aug 05 '23 21:08 ildar170975

It is possible to use this.entity_id in the "content"-section of a markdown-card. You need to put quotation marks around this.entity_id.

Like this:

            ...
            card:
              type: markdown
              content: >-
                {{ ... }}
                xxx: "this.entity_id"
                {{ ... }}

works fine for me with for example this card:

type: custom:auto-entities
card_param: cards
sort:
  method: none
card:
  type: vertical-stack
filter:
  include:
    - domain: binary_sensor
      group: binary_sensor.turen
      state: 'off'
      options:
        type: markdown
        entity_id:
          - this.entity_id
        card_mod:
          style:
            .: |
              ha-markdown.no-header {
                padding: 0px !important; 
                }
              ha-markdown {
                padding: 0px 0px 0px !important; 
                }
              ha-card {
                overflow: hidden !important;
                background: none !important;
                #box-shadow: none !important;
              } 
        content: <ha-alert alert-type="error">{{ state_attr("this.entity_id","friendly_name") }} ist geöffnet!</ha-alert>

guevara777 avatar Jan 04 '24 14:01 guevara777

works fine for me with for example this card:

This may only work in yaml mode. In storage made in UI editor you will definitely see errors since Markdown card does not support "entity_id" option.

Also, #box-shadow is a wrong commenting way since this is a string, you cannot comment a part of a string. A proper way is using /* ... */.

It is possible to use this.entity_id in the "content"-section of a markdown-card. You need to put quotation marks around this.entity_id.

This is a good catch!

ildar170975 avatar Jan 04 '24 14:01 ildar170975