"lookup table" - help and suggestion on how to

Let’s see if I can take another step forward.

i’m just working on this lookup table as example training:


t

  • o be used/reused for multipurpose
  • value stored on an attributes template sensor

From what I learnt should be a dictionary, I suppose like that:
serie1: [ “id”: 0. “cardinals”: N, “wind”: "tramontana, “type”: “string”, “meteotype”: “string”]
serie2: [ “id”: 22.5. “cardinals”: NNE, “wind”: "bora, “type”: “string”, “meteotype”: “string”]
serie3: [ “id”: 45. “cardinals”: NE, “wind”: "greco, “type”: “string”, “meteotype”: “string”]
and so on

I got a variable where is the value “degrees” and to be used as key item,
to retrieve data value eg. “Wind” based on degress
I suppose ( not so expert) should be used >> for…endfor

“I dont know” how to implement and write in right way
Help and suggestios are more than wellcome :slight_smile:

Put the following into Developer tools | Template

{%- set table = [ { "id": "0", "cardinals": "N", "wind": "tramontana", "type": "string", "meteotype": "string"},
                  { "id": "22.5", "cardinals": "NNE", "wind": "bora", "type": "string", "meteotype": "string"},
                  { "id": "45", "cardinals": "NE", "wind": "greco", "type": "string", "meteotype": "string"} ] -%}
{{ table | selectattr("id", "eq", "0") | map(attribute="wind") | list | first }}

You can look at the Jinja2 documentation to see what else can be done.
Built-in Filters and Built-in Tests

That should get you started.

great, it works perfectly :slight_smile:
and that0s more fòexble tham i expected

I mean; it takes value on both left and right

{% set goofy = "NE"  %}
{%- set table = [ { "id": "0", "cardinals": "N", "wind": "tramontana", "type": "string", "meteotype": "string"},
                  { "id": "22.5", "cardinals": "NNE", "wind": "bora", "type": "string", "meteotype": "string"},
                  { "id": "45", "cardinals": "NE", "wind": "greco", "type": "string", "meteotype": "string"} ] -%}
{{ table | selectattr("cardinals", "eq", goofy) | map(attribute="id") | list | first }}
      
          
said that......j would like to use the table as "multi purpose".
excuse me i dont have the property of right semantic.
I try to explian in better way










            

            


scenario
Test.yaml

template:

  • sensor1:
    attributes:
    a1: table [degrees]
    b1: table: table [meteo]
  • sensor2:
    attributes:
    a2: table [cardinals]
    b2: table [meteotype]

Where to write/define the table structure?
i write TABLE once time and then i call on a1,b1,a2,b2 and so on
I’m lost here on this point

I mean; on a1 “block definitions” is “easy”,
I set “goofy” with degrees value
i call “table” with the sentence:
{{ table | selectattr(“degress”, “eq”, goofy) | map(attribute=“id”) | list | first }}
I get the result as my expectations

See the “reusing templates” (aka macros) section here:

Hi,
well at the begining was a bit in “panic” but ok:

  • create custom_templates
  • create new file with jinja extensions
  • put my table inside
  • reload all configurations
    in Developer tools | Templat i wrote:
{% from 'blabla.jinja' import table %}
{{ table | selectattr("id", "eq", "0") | map(attribute="wind") | list | first }} *[u]>>> thks @Jeffcrum  :-)[/u]*

Greta it works, I see "tramontana"

I didnt put
{% macro %}
{% endmacro %}
*[u]is correct from system_view?[/u]*

Thanks in advance for support