# Switch node jsonata predicate match

**URL:** <https://discourse.nodered.org/t/switch-node-jsonata-predicate-match/82290>\
**Category:** General\
**Tags:** switch-node, jsonata\
**Created:** [23 October 2023 16:05 UTC](https://discourse.nodered.org/t/switch-node-jsonata-predicate-match/82290 "2023-10-23T16:05:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevegroom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevegroom/32/19591_2.png) [@stevegroom](https://discourse.nodered.org/u/stevegroom)\
**Post date:** [23 October 2023 16:05 UTC](https://discourse.nodered.org/t/switch-node-jsonata-predicate-match/82290/1 "2023-10-23T16:05:21Z")

</div>

I am trying to get a predicate match in a switch node to look up a device\_ieee and determine the device type - in this case Hue v1 or Hue v2.

msg object has been supplemented with roominfo where the device\_ieee can be looked up.

Switch node:  
Property: `roominfo.dimmers[device_ieee = payload.event.device_ieee].type`

Msg object is:

```auto
{
  "payload": {
    "event_type": "zha_event",
    "event": {
      "device_ieee": "00:17:88:01:0b:59:ef:1a",
      "unique_id": "00:17:88:01:0b:59:ef:1a:1:0xfc00",
      "device_id": "aabffa27aa141698895a8f24c8f8d9f2",
      "endpoint_id": 1,
      "cluster_id": 64512,
      "command": "on_short_release",
      "args": {
        "button": "on",
        "press_type": "short_release",
        "command_id": 0,
        "args": [
          1,
          3145728,
          2,
          33,
          1,
          0
        ]
      },
      "params": {}
    },
    "origin": "LOCAL",
    "time_fired": "2023-10-23T15:53:20.620406+00:00",
    "context": {
      "id": "01HDEKV6ZCX5J7BWM22RZ4SSDR",
      "parent_id": null,
      "user_id": null
    }
  },
  "topic": "zha_event",
  "room": "light.steves_office",
  "_msgid": "0d5ca34c8bc903e2",
  "roominfo": {
    "area": "steves_office",
    "dimmers": [
      {
        "name": "dimmer1",
        "type": "Hue v1",
        "device_ieee": "00:17:88:01:04:af:95:a0"
      },
      {
        "name": "dimmer2",
        "type": "Hue v2",
        "device_ieee": "00:17:88:01:0b:59:ef:1a"
      }
    ],
    "illuminance_sensors": [
      {
        "name": "binary_sensor.steves_office_sensor_illuminance"
      },
      {
        "name": "binary_sensor.steves_office_sensor2_illuminance"
      }
    ],
    "light": "light.steves_office",
    "occupancy_sensors": [
      {
        "name": "binary_sensor.steves_office_sensor_occupancy"
      },
      {
        "name": "binary_sensor.steves_office_sensor2_occupancy"
      }
    ],
    "occupancy_timer": "timer.steves_office_occupancy"
  }
}

```

The switch node has output 1 == 'Hue v1' , output 2 == 'Hue v2',  
But my message ends up in Otherwise..

---

<div class="post-metadata">

**Author:** ![stevegroom](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/stevegroom/32/19591_2.png) [@stevegroom](https://discourse.nodered.org/u/stevegroom)\
**Post date:** [23 October 2023 16:32 UTC](https://discourse.nodered.org/t/switch-node-jsonata-predicate-match/82290/2 "2023-10-23T16:32:21Z")

</div>

You have to use the parent selector - %

roominfo.dimmers[device\_ieee = %.%.payload.event.device\_ieee].type

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [23 October 2023 16:55 UTC](https://discourse.nodered.org/t/switch-node-jsonata-predicate-match/82290/3 "2023-10-23T16:55:33Z")

</div>

You really should tell JSOnata which scope context to look in, as in `[]` you are in $ scope context local loop. But payload is not in that context it is in Base scope context $$  
That is why i always add the context, people think I am crazy, but if you had you would of come up with

```auto
$$.roominfo.dimmers[$.device_ieee = $$.payload.event.device_ieee].type

```

which would have worked straight off

Notice i let JSONata know where it should be looking, even if it is not required.  
Looking at expression you can see where in scope context things are.

It will save you a lot of head scratching in the future.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 November 2023 16:56 UTC](https://discourse.nodered.org/t/switch-node-jsonata-predicate-match/82290/4 "2023-11-06T16:56:29Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
