Expert help with Jinja2

Hi,

I’m facing the following challenge in Jinja2, and I just can’t seem to figure it out. I’m actually wondering if it’s even possible, so I’m trying it this way in the hope that someone can help me.

Here’s the situation:

Entity: sensor.sm_x200_active_notification_count 
State: 1
State attributes:
io.homeassistant.companion.android_1162729296_category: 'null'
io.homeassistant.companion.android_1162729296_channel_id: general
io.homeassistant.companion.android_1162729296_group_id: 'null'
io.homeassistant.companion.android_1162729296_is_clearable: true
io.homeassistant.companion.android_1162729296_is_ongoing: false
io.homeassistant.companion.android_1162729296_post_time: 1693379843940
android.bigText_io.homeassistant.companion.android_1162729296: This is a test

Now, what I want to achieve is to display this text based on the post_time attribute. Whenever a new message arrives with a newer post_time, I want the new sensor to display that message. If a new message arrives, it will have a more recent post_time, and I want to show that message. Does anyone have an idea?

I got some text showing up with:

{% for i in states.sensor.sm_x200_active_notification_count.attributes %}
  {% if 'android.bigText_io.homeassistant.companion.android' in i %}
      {% set x = state_attr('sensor.sm_x200_active_notification_count', i) %}
      {% set y = x %}
      {{ y }}
    {% endif %}
{% endfor %}

Unfortunately without post_time

Not sure i understand this exactly. Doesn’t the message text attribute change when a new message comes in? In which case can you sensor not just show this attribute and then it will always have the last message?

EDIT: oh, do you get multiple sets of attributes when you have more than 1 notification? In which case, does the number of each notification set always increase? Ie the 1162729296 bit of the last notification is the highest number? If so, you can enumerate all the text entities and look foe the one with the biggest number and then pass that number back into a state_attr call. I will try and post you an example shortly.

Playing with this, can you not use the last notification sensor? If you want to just have HA notifications, just restrict it to those in the companion app sensor settings.

Try something like this. Have left some extra at the end so you can see if working in the dev tools. There maybe neater ways to do but doing it on tablet.

{% set ns = namespace(max_post=0, attr='') %}      
{% for m in states.sensor.sm_t860_active_notification_count.attributes if m is search('io.homeassistant.companion.') and m is search('post_time') %}
  {% if state_attr('sensor.sm_t860_active_notification_count', m) > ns.max_post %}
    {% set ns.max_post = state_attr('sensor.sm_t860_active_notification_count', m) %}
    {% set ns.attr = m %}
  {% endif %}
{% endfor %}

{{ns.attr}}
{{ ns.attr | replace('_post_time','')}}
{{ state_attr('sensor.sm_t860_active_notification_count', 'android.bigText_' ~ ns.attr | replace('_post_time','') )}}

Thanks for your assistance so far.
For some reason, I can’t get this to work, no idea why?


But I do receive the notifications and see the readings from the other sensor, so that’s why I’m trying it through this more complicated route.

I will try to work on this further, for now, it doesn’t seem to be functioning and I’m receiving the error message: UndefinedError: ‘None’ has no attribute ‘attributes’

Attempting to provide more context;
Full State Attributes:

state_class: measurement
android.appInfo_android_0: ApplicationInfo{697bb52 android}
android.appInfo_com.wssyncmldm_3: ApplicationInfo{16e1bb4 com.wssyncmldm}
android.appInfo_de.ozerov.fully_8989: ApplicationInfo{98ad3dd de.ozerov.fully}
android.appInfo_io.homeassistant.companion.android_1162373195: ApplicationInfo{d6ad087 io.homeassistant.companion.android}
android.appInfo_io.homeassistant.companion.android_1162729296: ApplicationInfo{896d8c6 io.homeassistant.companion.android}
android.bigText_android_0: >-
  Als je niet wilt dat Fully Kiosk Browser deze functie gebruikt, tik je om de
  instellingen te openen en zet je de functie uit.
android.bigText_com.wssyncmldm_3: 'null'
android.bigText_io.homeassistant.companion.android_1162373195: Test
android.bigText_io.homeassistant.companion.android_1162729296: Second Test
android.foregroundApps_android_0:
  - de.ozerov.fully
android.infoText_io.homeassistant.companion.android_1162373195: 'null'
android.infoText_io.homeassistant.companion.android_1162729296: 'null'
android.largeIcon_android_0: Icon(typ=BITMAP size=54x54)
android.largeIcon_io.homeassistant.companion.android_1162373195: 'null'
android.largeIcon_io.homeassistant.companion.android_1162729296: 'null'
android.progressIndeterminate_io.homeassistant.companion.android_1162373195: false
android.progressIndeterminate_io.homeassistant.companion.android_1162729296: false
android.progressMax_io.homeassistant.companion.android_1162373195: 0
android.progressMax_io.homeassistant.companion.android_1162729296: 0
android.progress_io.homeassistant.companion.android_1162373195: 0
android.progress_io.homeassistant.companion.android_1162729296: 0
android.reduced.images_android_0: true
android.reduced.images_com.wssyncmldm_3: true
android.reduced.images_de.ozerov.fully_8989: true
android.reduced.images_io.homeassistant.companion.android_1162373195: true
android.reduced.images_io.homeassistant.companion.android_1162729296: true
android.remoteInputHistory_io.homeassistant.companion.android_1162373195: 'null'
android.remoteInputHistory_io.homeassistant.companion.android_1162729296: 'null'
android.showChronometer_io.homeassistant.companion.android_1162373195: false
android.showChronometer_io.homeassistant.companion.android_1162729296: false
android.showWhen_io.homeassistant.companion.android_1162373195: true
android.showWhen_io.homeassistant.companion.android_1162729296: true
android.subText_io.homeassistant.companion.android_1162373195: 'null'
android.subText_io.homeassistant.companion.android_1162729296: 'null'
android.template_android_0: android.app.Notification$BigTextStyle
android.template_com.wssyncmldm_3: android.app.Notification$BigTextStyle
android.template_io.homeassistant.companion.android_1162373195: android.app.Notification$BigTextStyle
android.template_io.homeassistant.companion.android_1162729296: android.app.Notification$BigTextStyle
android.text_android_0: >-
  Als je niet wilt dat Fully Kiosk Browser deze functie gebruikt, tik je om de
  instellingen te openen en zet je de functie uit.
android.text_com.wssyncmldm_3: 'null'
android.text_de.ozerov.fully_8989: Running in Priority Mode
android.text_io.homeassistant.companion.android_1162373195: Test
android.text_io.homeassistant.companion.android_1162729296: Second Test
android.title_android_0: Fully Kiosk Browser wordt weergegeven vóór andere apps
android.title_com.wssyncmldm_3: Uw tablet bijwerken
android.title_de.ozerov.fully_8989: Fully Kiosk Browser
android.title_io.homeassistant.companion.android_1162373195: Test
android.title_io.homeassistant.companion.android_1162729296: Second Test
android_0_category: 'null'
android_0_channel_id: com.android.server.wm.AlertWindowNotification - de.ozerov.fully
android_0_group_id: 'null'
android_0_is_clearable: false
android_0_is_ongoing: true
android_0_post_time: 1693327755655
androidx.core.app.extra.COMPAT_TEMPLATE_io.homeassistant.companion.android_1162373195: androidx.core.app.NotificationCompat$BigTextStyle
androidx.core.app.extra.COMPAT_TEMPLATE_io.homeassistant.companion.android_1162729296: androidx.core.app.NotificationCompat$BigTextStyle
com.wssyncmldm:bundle_com.wssyncmldm_3: Bundle[{taskId=d2c}]
com.wssyncmldm_3_category: 'null'
com.wssyncmldm_3_channel_id: IDM_NOTIFICATION_CHANNEL_NO_SOUND_NO_VIBRATION
com.wssyncmldm_3_group_id: 'null'
com.wssyncmldm_3_is_clearable: false
com.wssyncmldm_3_is_ongoing: false
com.wssyncmldm_3_post_time: 1693327801527
de.ozerov.fully_8989_category: 'null'
de.ozerov.fully_8989_channel_id: '4565'
de.ozerov.fully_8989_group_id: 'null'
de.ozerov.fully_8989_is_clearable: false
de.ozerov.fully_8989_is_ongoing: true
de.ozerov.fully_8989_post_time: 1693327763159
io.homeassistant.companion.android_1162373195_category: 'null'
io.homeassistant.companion.android_1162373195_channel_id: general
io.homeassistant.companion.android_1162373195_group_id: 'null'
io.homeassistant.companion.android_1162373195_is_clearable: true
io.homeassistant.companion.android_1162373195_is_ongoing: false
io.homeassistant.companion.android_1162373195_post_time: 1693379487900
io.homeassistant.companion.android_1162729296_category: 'null'
io.homeassistant.companion.android_1162729296_channel_id: general
io.homeassistant.companion.android_1162729296_group_id: 'null'
io.homeassistant.companion.android_1162729296_is_clearable: true
io.homeassistant.companion.android_1162729296_is_ongoing: false
io.homeassistant.companion.android_1162729296_post_time: 1693379843940

Not an answer to your problem, but a suggestion: when you format the yaml code in your post this improves the readability.
See also How to help us help you - or How to ask a good question, and especially the info about formatting code.

1 Like

You’re absolutely right, thank you!

Did you change the sensor names. This was mine for testing?

It may also need a catch if there are no notifications.

Try it as is (with your sensor names) in the dev templates console and make sure working before adding to a template sensor.

This should do the trick.

{% set attributes = states['sensor.sm_x200_active_notification_count'].attributes %}
{% set ns = namespace(items=[]) %}
{% for k, v in attributes.items() %}
  {% set l = k | regex_findall("([a-zA-Z]+_[0-9]+)_post_time") %}
  {% if l %}
     {% set ns.items = ns.items + [{'id': l[0], 'value': v} ] %}
  {% endif %}
{% endfor %}
{% set latest = ns.items | sort(attribute='value', reverse=True) | map(attribute='id') | first | default("Error") %}
{{ attributes.items() | selectattr('0', 'search', latest) | selectattr('0', 'search', 'bigText') | map(attribute='1') | first | default("Error") }}

Great! That’s indeed the golden tip! Thank you very much!