I need help with Jinja2 templates

I want to pick the sensor entity before ‘=whzk’ and see where the entity (as a number) in the array presence_entities is.
Then I want it to search in the notification_entities array for the location with the number and then give me the entry.
do you have an idea how can do this?

At the end I would like to get notification entity for the presence entity.

sensor.espresense_mi_band_5 belongs to notify.mobile_app_a
sensor.espresense_mi_pad_5 belongs to notify.mobile_app_b

What I already have:

{% set presence_entities_str = "sensor.espresense_mi_band_5, sensor.espresense_mi_pad_5" %}
{% set notification_entities_str = "notify.mobile_app_a, notify.mobile_app_b," %}
{% set presence_entities = presence_entities_str.split(',') | map('trim') | list %}
{% set notification_entities = notification_entities_str.split(',') | map('trim') | list %}
{% set presence_states = ("whzk").split(',') | map('trim') | list %}

{{ expand(presence_entities) | selectattr('state','in',presence_states) | list }}

outputs:
[<template TemplateState(<state sensor.espresense_mi_band_5=whzk; distance=0.45, friendly_name=ESPresense Mi Band 5 @ 2022-01-15T14:39:43.854961+01:00>)>, <template TemplateState(<state sensor.espresense_mi_pad_5=whzk; distance=2.85, friendly_name=ESPresense Mi Pad 5 @ 2022-01-15T14:48:36.851026+01:00>)>]

Thanks for help.

It’s far easier to achieve if you use a single dictionary as a structure to store the data (sensor and related notifier) instead of two lists.

{% set x = { 'sensor.espresense_mi_band_5': 'notify.mobile_app_a',
             'sensor.espresense_mi_pad_5': 'notify.mobile_app_b' } %}
{% set y = expand(x.keys()) | selectattr('state', 'in', ['whzk'])
           | map(attribute='entity_id') | first %}
{{ x.get(y) if y | length > 0 else 'none' }}
1 Like

Thanks for your help and this great approach!

1 Like