Help Needed - Hassio json templating with jinja - processing incoming data

Hassio configuration
json templating with jinja - processing incoming data.
How to loop through a json values array to trigger a sensor.
value_template syntax:

Hassio: 0.100.2
R Pi.

Device: NodeMCU with WIFI Sniffer:

https://github.com/SensorsIot/Wi-Fi-Sniffer-as-a-Human-detector/tree/master/WiFi_Sniffer

Hey all,

Im wondering if anyone smarter than me (most of you) can help me quickly;

I want an alternative device tracker for my HA setup, sniffing all probe requests in my location.

My sniffer is up and running but now I need to extract the MAC address data for my sensor.

I need help with the json value template:

The Sniffer sends the following json over MQTT

{
    "MAC": [
        "b8:69:f4:0c:3a:7b",
        "ba:69:f4:0c:3a:7b",
        "80:7d:3a:42:d4:1b",
        "3c:15:c2:e9:16:08",
        "4c:17:44:04:ed:d4",
        "d8:0f:99:82:08:14"
    ]
}

I need to extract these MAC address out to trigger a sensor if a particular MAC address is anywhere in the array, Im not sure how to write the syntax so it loops through the array

something like this:

- platform: mqtt
    name: "My mother-in-law in in the area!"
    state_topic: "Sniffer"

    value_template: >-
    {%for i = 0; i < MAC.length; i++; x += MAC[i]%}
    {%- endfor %}

    payload_on: "b8:69:f4:0c:3a:7b"
    payload_off: 
    qos: 1   

Cheers
P

No need for a loop.
Play with the json data in Dev Tools/templates

{% set value_json = {
    "MAC": [
        "b8:69:f4:0c:3a:7b",
        "ba:69:f4:0c:3a:7b",
        "80:7d:3a:42:d4:1b",
        "3c:15:c2:e9:16:08",
        "4c:17:44:04:ed:d4",
        "d8:0f:99:82:08:14"
    ]
} %}
{{ "b8:69:f4:0c:3a:7b" in value_json.MAC }}

Returns: True

Hi VDRainer!
Thank-you very much for your help. That did the trick, I really appreciate that!

Here’s my YAML:

binary_sensor:

  • platform: mqtt
    name: “She’s in the area”
    state_topic: “Sniffer”
    value_template: ‘{{ “ac:57:75:25:xx:xx” in value_json.MAC }}’
    payload_on: true
    payload_off: false
    qos: 1

Happy Weekend from rainy Cape Town!
Cheers
P

1 Like