Map ID to User recevied via json webhook in an automation

I try to notify, may be later also trigger some action, who used the Fingerprint to open the Door. Info is send via webhook as json but has to be mapped first.

In the Dev Tools template works but in the automation seem the mapping is not executed result is always unknown.
Json path seems fine, the values matches when logged.

Any suggestion what might be wrong, or different way to map the values?

actions:
  - variables:
      vu: "{{ trigger.json.params.userId }}"
      euser: |-
        {% set mapper =  {
         'asdfljk34l' : 'User A',
         'asdd45k34l' : 'User B',
          'asdfljk34l' : 'User C' } %}
         {% set u = "{{ vu }}" %}  
         {{ mapper[u] if u in mapper else 'Unbekannte Person' }}
      einput: |-
        {% set i = "{{ trigger.json.type | string }}" %}
          {{ {10: 'Finger',
              20: 'Digital Input'}.get(i, 'Unkown Input') }}
      eresult: |-
        {% set r = "{{ trigger.json.result }}" %} {{ {0: 'Unknown',
             10: 'Match',
             20: 'Filtered Match',
             30: 'No Match'}.get(r, 'Unknown Result') }}
  - action: logbook.log
    metadata: {}
    data:
      name: ekey
      message: >-
        {{ euser }} + {{ einput }} +  {{ eresult }} + {{
        trigger.json.params.userId }} + {{ trigger.json.type }} + {{
        trigger.json.result }} + {{vu}}

Don’t nest templates, just use the variable directly.

{% set u = vu %}

Your original was setting the value of u to the string “{{ vu }}”, not the value of the variable vu.

You could also set your mappings in script variables instead of doing it all together, but that comes down to personal preference:

Using Script Variable Mapping
actions:
  - variables:
      vu: "{{ trigger.json.params.userId }}"
      user_map:
        asdfljk34l : User A
        asdd45k34l : User B
        asdfljk34l : User C
      euser: |-
        {{ user_map.get(u, 'Unbekannte Person') }}
      input_map:
        10: 'Finger',
        20: 'Digital Input'
      einput: |-
        {% set i = trigger.json.type %}
        {{ input_map.get(i, 'Unkown Input') }}
      result_map:
        0: Unknown
        10: Match
        20: Filtered Match
        30: No Match
      eresult: |-
        {% set r = trigger.json.result %} 
        {{ result_map.get(r, 'Unknown Result') }}
  - action: logbook.log
    metadata: {}
    data:
      name: ekey
      message: >-
        {{ euser }} + {{ einput }} +  {{ eresult }} + {{
        trigger.json.params.userId }} + {{ trigger.json.type }} + {{
        trigger.json.result }} + {{vu}}

WOW thanks, it worked right away. Spend hours before, being cluesless what happens.

Thanks again for the explanation an alternativ, makes it easier to read but I’m getting an Error:

“Fehler: UndefinedError: ‘annotatedyaml.objects.NodeStrClass object’ has no attribute ‘get’”

Am I missing something?

  finger_map: |-
    -5 : Left Little Finger
    -4 : Left Ring Finger
    -3 : Left Middle Finger
    -2 : Left Index Finger
    -1 : Left Thumb
    0 : None
    1 : Right Thumb
    2 : Right Index Finger
    3 : Right Middle Finger
    4 : Right Ring Finger
    5 : Right Little Finger
  fresult: |-
    {% set f = trigger.json.params.fingerIndex %} 
    {{ finger_map.get(f, 'Unknown Finger') }}

That’s a multiline-string indicator, it does not belong there for finger_map. It’s turning your mapping into a string.

Error gone, but result is “Unkown Finger”, any more Idea?

json:
    type: 10
    result: 10
    params:
      userId: abc12345
      fingerIndex: 1
  finger_map:
    "0": None
    "1": Right Thumb
    "2": Right Index Finger
    "3": Right Middle Finger
    "4": Right Ring Finger
    "5": Right Little Finger
    "-5": Left Little Finger
    "-4": Left Ring Finger
    "-3": Left Middle Finger
    "-2": Left Index Finger
    "-1": Left Thumb
  fresult: |-
    {% set f = trigger.json.params.fingerIndex %} 
    {{ finger_map.get(f, 'Unknown Finger') }}

Try:

{% set f = trigger.json.params.fingerIndex | string %}

No, still unkown

The Numners got automatically resorted on saving and put into " "

finger_map:
    "0": None
    "1": Right Thumb
    "2": Right Index Finger
    "3": Right Middle Finger
    "4": Right Ring Finger
    "5": Right Little Finger
    "-5": Left Little Finger
    "-4": Left Ring Finger
    "-3": Left Middle Finger
    "-2": Left Index Finger
    "-1": Left Thumb
  fresult: |-
    {% set f = trigger.json.params.fingerIndex | string %} 
    {{ finger_map.get(f, 'Unknown Finger') }}

Changed the finger_map:

…
‘1’: ‘Right Thumb’
…

Now it is working. Thx a again for the swift support.

1 Like