Split data from attributes

All those attributes are empty… what are you trying to get?

do you mean the attr’ name is RouterOS 6.43.2 and so on?

so i do i get the attr’ name to be display in the sensor instead of the state?

I’m asking you:

What information are you trying to get?

The goal is To make notification when new releases available.
Screenshot_2018-09-24-17-40-59

When there’s a new update, the state changed.(20,21,22…)
From that i can make a notification that show the new releases from that attribute…

Edited the above :point_up_2:

This is painful but this should get you the first [Stable] result:

{% set sorted_releases = states.sensor.engineering_feed.attributes.keys() | sort | reverse %}
{%- for release in sorted_releases if release | regex_match('.*\[Stable\].*') %}
  {%- if loop.first %}
    {{- release | replace('[Stable]','') }}
  {%- endif %}
{%- endfor %}

so inside a template sensor:

sensor:
  - platform: template
    sensors:
      next_router_os_release:
        value_template: >
          {% set sorted_releases = states.sensor.engineering_feed.attributes | sort | reverse %}
          {%- for release in sorted_releases if release | regex_match('.*\[Stable\].*') %}
            {%- if loop.first %}
              {{- release | replace('[Stable]','') }}
            {%- endif %}
          {%- endfor %}

Your output should be:

RouterOS 6.43.2
1 Like

Wow, it works just like I wanted.
Thank you very much .
routeros

1 Like

That’s strange that you get an empty string when you cast the answer as a string. I tried to put the output you provided as my source and I can make it work.
If the list you have is always ordered, then this would work :
states.sensor.engineering_feed.attributes| string).split('[Stable]')[0].split("'")[-1]

If it needs sorting, this should work for latest stable :

states.sensor.engineering_feed.attributes | sort(reverse=True) | string ).split('[Stable]')[0].split("'")[-1] }}

`

  1. states.sensor.engineering_feed.attributes is a dictionary. It’s not sorted.
  2. Splitting on ‘[Stable]’ may not work because what happens when the site removes everything but 1 ‘[Stable]’?

1 - The structure itself ain’t but I supposed the source ordered the items. See that the latest in each category comes first (Stable, Testing, long term…). Anyway, the sorted version should work though I don’t use sort in my configuration.

2 - What do you mean ?
This :
{'RouterOS 6.43.2 [Stable]': {} }
It will work as [Stable] gets removed, the [0] item is still what’s on the left side and there’s still the ’ to get the string.

Of course, if there is no “[Stable]” item in the list, it will fail, displaying a ] ! But I found this quite unlikely in that case.

What happens in this case:

{'RouterOS 6.44.11 [Testing]': {} ,'RouterOS 6.43.2 [Stable]': {} }

That’s my point. Your solution doesn’t cover all cases, which is why you need to search for [Stable] in the name, filter the list, and choose the highest revision. Doing a simple split in this case with your method would result in:

RouterOS 6.44.11 [Testing] RouterOS 6.43.2

I think this dict shows my case better:

{‘RouterOS 6.44.11 [Testing]’: {} ,‘RouterOS 6.43.2 [Stable]’: {} ,‘RouterOS 6.40.9 [Long-term]’: {}}

Sorted looks like:

‘RouterOS 6.40.9 [Long-term]’, ‘RouterOS 6.43.2 [Stable]’, ‘RouterOS 6.44.11 [Testing]’

resulting in this:

‘RouterOS 6.40.9 [Long-term]’, 'RouterOS 6.43.2

As long as you have at least 1 Stable version, it works. In your example I have this result. I splitted in several actions so that you see how it cuts and selects the parts.

And see why it fails when there is no “stable” entry :

Ah, missed the split("'"), that still doesn’t guarentee that the character ’ will be the splitting factor as you are relying on the interpreter to set the quotes. It could set the interior quotes as ". Also, the solution returns a character instead of empty if there is no stable version. While it is shorter, it still has it’s pitfalls. Using regex will return the same answer regardless of these variables.

Hi,
I have a problem since some time ago. In the past information into the engineering feed was as it’s on the screenshots above, but few months ago it changes as follow:


And I’m not sure who to break those objects into the strings.

That’s an error directly from your router. object Object is a object in js. That’s either an error in your router, or an error in the component you are using to get the information from your router. There’s nothing you can do other than:

  1. Update your router firmware and hope it fixes the issue.
  2. Update the component, if it’s a custom component.
  3. Log an issue with the component after you do step 1 and step 2 to verify that it’s not your setup.

This is information from the rss downloaded from MikroTik site.
Here is declaration of the sensor:

  - platform: feedparser
    name: "latest_mikrotik_version"
    feed_url: 'https://mikrotik.com/download.rss'
    date_format: '%a, %b %d %I:%M %p'
    inclusions:
      - title
#      - link
    exclusions:
      - language

thats a custom component… did you do what i said to do? Update and try again?

I am having trouble getting this code to work. I copied and pasted it into my yaml file:

## Check Latest Mikrotik Firmware
  - platform: feedparser
    name: Engineering Feed
    feed_url: 'https://mikrotik.com/download.rss'
    date_format: '%a, %b %d %I:%M %p'
    inclusions:
      - title
      - link
    exclusions:
      - language
      
  - platform: template
    sensors:
      next_router_os_release:
        value_template: >
          {% set sorted_releases = states.sensor.engineering_feed.attributes | sort | reverse %}
          {%- for release in sorted_releases if release | regex_match('.*\[Stable\].*') %}
            {%- if loop.first %}
              {{- release | replace('[Stable]','') }}
            {%- endif %}
          {%- endfor %}

I get the following output in Homeassistant:


I cannot figure out what the problem is. Can anyone assist?

Thanks,

Rick

The correct syntax for the line would be

          {% set sorted_releases = state_attr('sensor.engineering_feed', 'entries') | sort(attribute='title') | reverse %}

but your sort isn’t doing anything worthwhile because you’re sorting a list of dictionaries.

but… if you’re just trying to get the latest release…

{% set keyword =  '[Stable]' %}
{% set item = state_attr('sensor.engineering_feed', 'entries') | selectattr('title', 'search', keyword) | list | sort(attribute='title') | reverse | first %}
{{ item.title.replace(keyword, '') }}