Variable, notification, but only first 10 lines

Hi!

I am trying to make an automation, which shows me if I have HACS updates, but I want to make it only show the first 10 lines of notification.

This is what I have till now:
A variable which defines all the HACS updates

variables:
  var_mo: |
    New HACS Updates - {{now().strftime('%-d %b, %H:%M') ~ "\n"}}
    {%- for rep in state_attr('sensor.hacs', 'repositories') -%}
        - {{rep.display_name }}
        {{ "    " }}Current: {{rep.installed_version }}
        {{ "    " }}Latest: {{rep.available_version }}
    {%- endfor -%}
    {{"\n"}}------{{"\n"}}

And then the persistent notification which I want only to show me the first 10 lines

service: persistent_notification.create
data:
  message: >
    "{{var_mo | regex_findall_index('\A(?:.*\n){10}') }}"
  notification_id: "0001"

The problem is that I get an error when I try to run the automation…
image

Any idea how to achieve this, please?

Thank you!

This might work, might need some tweaking:

data:
  message: >
    {% set mo_list = var_mo.split('\n') %}
    {{ (mo_list[:-2 if mo_list|length < 12 else 12] + mo_list[-2:])|join("/n") }} 
1 Like

I have tried this into developer tools (to show only the first 5 lines)

{% set var_mo = "11\n22\n33\n44\n55\n66\n77\n88\n99"
%}
All lines
{{ var_mo }}

Only first 5 lines {% set mo_list = var_mo.split('\n') %}
{{ (mo_list[:-2 if mo_list|length <= 5 else 5] + mo_list[-2:]) | join("\n") }} 

But the result it is not good…

Edit1:

Still… if I only use “{{ (mo_list[:5]) | join(”\n") }} ", I will get the proper result… Is this ok?

My logic was to try to include your ending ------ line, which is the equivalent of the 88 and 99 in your test. So you end up with:

New HACS updates
[up to 10 lines of content]
--------

Oooooh, I understand. Nice :slight_smile:

Anyway, there is no need for that because I will do something else with the achievement, but yes, that was a nice logic. Thank you.

Edit1: I will use with in that way for another “issue” I had in the past :smiley: Soo, it was very welcome, your idea.

1 Like

By the way, maybe you know this as well :smiley: (Yeah, I know, I’m a gypsy :smiley: )

I want this text

    New HACS Updates - {{now().strftime('%-d %b, %H:%M') ~ "\n"}} {%- for rep in
    state_attr('sensor.hacs','repositories') -%} 
       - {{rep.display_name }}
       {{ "&nbsp;&nbsp;&nbsp;&nbsp;"}}Current: {{rep.installed_version }}
       {{ "&nbsp;&nbsp;&nbsp;&nbsp;"}}Latest: {{rep.available_version }}
       {{ "\n" }}
    {%- endfor -%} {{"\n"}}------{{"\n"}}

To set it as a variable in the developer tools. Something like this, but I don’t know how to do it

{% set value =

    New HACS Updates - {{now().strftime('%-d %b, %H:%M') ~ "\n"}} {%- for rep in
    state_attr('sensor.hacs','repositories') -%} 
       - {{rep.display_name }}
       {{ "&nbsp;&nbsp;&nbsp;&nbsp;"}}Current: {{rep.installed_version }}
       {{ "&nbsp;&nbsp;&nbsp;&nbsp;"}}Latest: {{rep.available_version }}
       {{ "\n" }}
    {%- endfor -%} {{"\n"}}------{{"\n"}}

%}

I want that “value” to have all that text, but not as text like I putted there, of course… The result of all those “operations”. Any idea how to achieve this as well?

You need to build it up a bit at a time; and you need to use a namespace (docs) because of the way variable scope works.

{% set ns = namespace(value="New HACS Updates - " ~ now().strftime('%-d %b, %H:%M') ~ "\n") %}
{% for rep in state_attr('sensor.hacs','repositories') %} 
  {% set ns.value = ns.value + "- " ~ rep.display_name %}
  {% set ns.value = ns.value + "&nbsp;&nbsp;&nbsp;&nbsp;Current: " ~ rep.installed_version %}
  {% set ns.value = ns.value + "&nbsp;&nbsp;&nbsp;&nbsp;Latest: " ~ rep.available_version %}
  {% set ns.value = ns.value + "\n" %}
{% endfor %}
{% set ns.value = ns.value + "\n------\n" %}

{{ ns.value }}
1 Like

Nice, thanks!

@Troon With the information from both issues, which you already helped me, I have done something different with the initial issue and I made it even better, here is the example, to see what I did:

{% set ns = namespace(value="New HACS Updates - " ~ now().strftime('%-d %b, %H:%M') ~ "\n") %}
{% for rep in state_attr('sensor.hacs','repositories') %} 
  {% set ns.value = ns.value + "\n   - " ~ rep.display_name %}
  {% set ns.value = ns.value + "\n       Current: " ~ rep.installed_version %}
  {% set ns.value = ns.value + "\n       Latest: " ~ rep.available_version %}
  {% set ns.value = ns.value + "\n" %}
{% endfor %}
{% set ns.value = ns.value + "\n--------------------------------\n\n" %}
{% set ns.value = ns.value + "\n--------------------------------\n\n" %}
{% set ns.value = ns.value + "\n--------------------------------\n\n" %}
{% set ns.value = ns.value + "\n--------------------------------\n\n" %}

{% set text = ns.value.split('\n--------------------------------\n\n') %}
{{ (text[:6]) | join("\n--------------------------------\n\n") }}

Sooo… as you already figured out, with this, If I will get more than 5 similar notifications, I can only show 5 of them and I will still have the “--------” at “the end”, if I have more than 1 notification, as you wanted me to help me in the first place.

Thank you again!

1 Like