Switch node jsonata predicate match

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:

{
  "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..

You have to use the parent selector - %

roominfo.dimmers[device_ieee = %.%.payload.event.device_ieee].type

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

$$.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.

2 Likes

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