Using a switch node with JSONata

I'm using a switch and the input is msg.payload from a NetAtmo account. The payload object starts with:

{
    "compact": {
        "outdoor": {
            "temperature": 18.4,
            "humidity": 23,
            "battery_percent": 57,
            "rf_status": 60
        },
        "rain": {},
        "temperature": 22.7, ...

The #1 output is a JSONata expression:

But when deployed, nothing gets sent to the output.

Help.

It will not be possible to help you unless you:

1- Fix the image that is not showing up in your post

2- Please provide the whole flow so we can have a look inside the switch node (as well as the dataset). Remember that to post code you need to add three backticks characters before and three backticks after the code.

```

Your code

```
1 Like

The limitation of new users only being able to post one image is a problem ...

The flow is simple:

[{"id":"ba849ec9.74862","type":"inject","z":"3f0121a.5f1f7de","name":"","topic":"","payload":"","payloadType":"date","repeat":"600","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":340,"wires":[["494665fe.6ad52c"]]},{"id":"494665fe.6ad52c","type":"netatmo-dashboard","z":"3f0121a.5f1f7de","creds":"3c55e9f5.f75e06","x":340,"y":340,"wires":[["3ba6b11e.29e53e"]]},{"id":"3ba6b11e.29e53e","type":"switch","z":"3f0121a.5f1f7de","name":"","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"compact.outdoor.temperature","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":530,"y":340,"wires":[["867590c2.e3edd"]],"inputLabels":["xxx"],"outputLabels":["yyy"]},{"id":"867590c2.e3edd","type":"debug","z":"3f0121a.5f1f7de","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":690,"y":340,"wires":[]},{"id":"3c55e9f5.f75e06","type":"netatmo-config-node","z":"","client_id":"5ad1426a8e178263588b46d8","client_secret":"XXX","username":"XXX","password":"XXX"}]

Can you wire the output of the NetAtmo node to a Debug node and show us its output?

The output of the NetAtmo node is the payload object I showed above ...

{
    "compact": {
        "outdoor": {
            "temperature": 18.4,
            "humidity": 23,
            "battery_percent": 57,
            "rf_status": 60
        },
        "rain": {},
        "temperature": 22.7, ...

If that is the payload of the message (which the Debug sidebar would have told us), then your jsonata expression needs to be payload.compact.outdoor.temperature.

I tried that and there's still no switch output. That expression also fails in the expression editor using the NetAtmo output object.

In the screen shot of the expression editor, you have put the compact object at the top level of the message object. It is not under the payload property as I would expect it to be coming from the NetAtmo node.

You can use the Debug node to confirm the exact message structure you have.

Here's the full NetAtmo payload object (the opening "(" gets cut off in the forum editor's rendering for some reason):

  "compact": {
    "outdoor": {
      "temperature": 15.2,
      "humidity": 19,
      "temperatureTrend": "stable",
      "battery_percent": 57,
      "rf_status": 71
    },
    "rain": {},
    "temperature": 20.7,
    "temperatureTrend": "stable",
    "co2": 486,
    "humidity": 37,
    "noise": 44,
    "pressure": 1026,
    "pressureTrend": "down",
    "station_name": "XXX",
    "last_status_store": 1542206768
  },
  "detailed": [
    {
      "_id": "70:ee:50:00:ea:18",
      "cipher_id": "XXX",
      "date_setup": 1371091374,
      "last_setup": 1371091374,
      "type": "NAMain",
      "last_status_store": 1542206768,
      "module_name": "XXX",
      "firmware": 132,
      "last_upgrade": 1440121433,
      "wifi_status": 50,
      "co2_calibrating": false,
      "station_name": "XXX",
      "data_type": [
        "Temperature",
        "CO2",
        "Humidity",
        "Noise",
        "Pressure"
      ],
      "place": {
        "altitude": 120.08533983542,
        "city": "XXX",
        "country": "US",
        "timezone": "XXX",
        "location": [
          XXX,
          XXX
        ]
      },
      "dashboard_data": {
        "time_utc": 1542206753,
        "Temperature": 20.7,
        "CO2": 486,
        "Humidity": 37,
        "Noise": 44,
        "Pressure": 1026,
        "AbsolutePressure": 1011.5,
        "min_temp": 20.6,
        "max_temp": 21.4,
        "date_min_temp": 1542204365,
        "date_max_temp": 1542182477,
        "temp_trend": "stable",
        "pressure_trend": "down"
      },
      "modules": [
        {
          "_id": "02:00:00:00:f8:c4",
          "type": "NAModule1",
          "module_name": "Outdoor",
          "data_type": [
            "Temperature",
            "Humidity"
          ],
          "last_setup": 1371091749,
          "dashboard_data": {
            "time_utc": 1542206723,
            "Temperature": 15.2,
            "Humidity": 19,
            "min_temp": 14.8,
            "max_temp": 17.3,
            "date_min_temp": 1542203724,
            "date_max_temp": 1542189727,
            "temp_trend": "up"
          },
          "firmware": 44,
          "last_message": 1542206762,
          "last_seen": 1542206723,
          "rf_status": 71,
          "battery_vp": 4965,
          "battery_percent": 57
        }
      ]
    }
  ]
}```

And to make sure you believe that's the NetAtmo node output, here's an image of the expanded debug output (I removed the station name):

I find no issue to extract the temperature, in many different ways.

Please have a look in below flow.

Flow:

[{"id":"56970bdc.c12284","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"b90fcda6.e6b22","type":"inject","z":"56970bdc.c12284","name":"","topic":"","payload":"{\"compact\":{\"outdoor\":{\"temperature\":15.2,\"humidity\":19,\"temperatureTrend\":\"stable\",\"battery_percent\":57,\"rf_status\":71},\"rain\":{},\"temperature\":20.7,\"temperatureTrend\":\"stable\",\"co2\":486,\"humidity\":37,\"noise\":44,\"pressure\":1026,\"pressureTrend\":\"down\",\"station_name\":\"XXX\",\"last_status_store\":1542206768},\"detailed\":[{\"_id\":\"70:ee:50:00:ea:18\",\"cipher_id\":\"XXX\",\"date_setup\":1371091374,\"last_setup\":1371091374,\"type\":\"NAMain\",\"last_status_store\":1542206768,\"module_name\":\"XXX\",\"firmware\":132,\"last_upgrade\":1440121433,\"wifi_status\":50,\"co2_calibrating\":false,\"station_name\":\"XXX\",\"data_type\":[\"Temperature\",\"CO2\",\"Humidity\",\"Noise\",\"Pressure\"],\"place\":{\"altitude\":120.08533983542,\"city\":\"XXX\",\"country\":\"US\",\"timezone\":\"XXX\",\"location\":[\"XXX\",\"XXX\"]},\"dashboard_data\":{\"time_utc\":1542206753,\"Temperature\":20.7,\"CO2\":486,\"Humidity\":37,\"Noise\":44,\"Pressure\":1026,\"AbsolutePressure\":1011.5,\"min_temp\":20.6,\"max_temp\":21.4,\"date_min_temp\":1542204365,\"date_max_temp\":1542182477,\"temp_trend\":\"stable\",\"pressure_trend\":\"down\"},\"modules\":[{\"_id\":\"02:00:00:00:f8:c4\",\"type\":\"NAModule1\",\"module_name\":\"Outdoor\",\"data_type\":[\"Temperature\",\"Humidity\"],\"last_setup\":1371091749,\"dashboard_data\":{\"time_utc\":1542206723,\"Temperature\":15.2,\"Humidity\":19,\"min_temp\":14.8,\"max_temp\":17.3,\"date_min_temp\":1542203724,\"date_max_temp\":1542189727,\"temp_trend\":\"up\"},\"firmware\":44,\"last_message\":1542206762,\"last_seen\":1542206723,\"rf_status\":71,\"battery_vp\":4965,\"battery_percent\":57}]}]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["9ea08632.091e18","a6ef2b61.e87538","78fc3ea5.262be","fcea5f3d.3a6b2"]]},{"id":"9ea08632.091e18","type":"switch","z":"56970bdc.c12284","name":"","property":"payload.compact.outdoor.temperature","propertyType":"msg","rules":[{"t":"nnull"}],"checkall":"true","repair":false,"outputs":1,"x":390,"y":180,"wires":[["8087331a.f3de5"]],"inputLabels":["xxx"],"outputLabels":["yyy"]},{"id":"a6ef2b61.e87538","type":"debug","z":"56970bdc.c12284","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload.compact.outdoor.temperature","x":510,"y":120,"wires":[]},{"id":"8087331a.f3de5","type":"debug","z":"56970bdc.c12284","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":530,"y":180,"wires":[]},{"id":"78fc3ea5.262be","type":"change","z":"56970bdc.c12284","name":"Set result","rules":[{"t":"set","p":"result","pt":"msg","to":"payload.compact.outdoor.temperature","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":240,"wires":[["7695b562.09585c"]]},{"id":"7695b562.09585c","type":"debug","z":"56970bdc.c12284","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"result","x":610,"y":240,"wires":[]},{"id":"fcea5f3d.3a6b2","type":"change","z":"56970bdc.c12284","name":"Set output","rules":[{"t":"set","p":"output","pt":"msg","to":"payload.compact.outdoor.temperature","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":390,"y":300,"wires":[["77af2f5a.c5a35"]]},{"id":"77af2f5a.c5a35","type":"debug","z":"56970bdc.c12284","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"output","x":610,"y":300,"wires":[]}]

At the top of the screenshot it says msg.payload - so this whole compactobject is under msg.payload.

If you hover over any message element, a button appears on the right side which can be used to copy the path to the element to your clipboard.

You'll find you get payload.compact.outdoor.temperature

That aside, the problem is your Switch node configuration. You are asking it to compare the value of msg.payload with the result of compact.outdoor.temperature .. which won't work.

What is the actual comparison you want to make?

Or do you want to set msg.payload to the value of msg.payload.compact.outdoor.temperature ? If that is what you want, you should use a Change node, not a Switch node.

Note this explanation from the switch node help:

A JSONata Expression can be provided that will be evaluated against the whole message and will match if the expression returns a true value.

Perhaps you want to use this jsonata expression in your switch node ?

payload.compact.outdoor.temperature > 0

Flow:

[{"id":"d9ab29db.5928c8","type":"tab","label":"Flow 4","disabled":false,"info":""},{"id":"9740667f.9f4988","type":"switch","z":"d9ab29db.5928c8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"jsonata_exp","v":"payload.compact.outdoor.temperature > 0","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":1,"x":390,"y":120,"wires":[["fc75236b.53b84"]],"inputLabels":["xxx"],"outputLabels":["yyy"]},{"id":"fc75236b.53b84","type":"debug","z":"d9ab29db.5928c8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":570,"y":120,"wires":[]},{"id":"605ed803.bdb988","type":"comment","z":"d9ab29db.5928c8","name":"**Explanation**","info":"A JSONata Expression can be provided that will be evaluated against the whole message and will match if the expression returns a true value.","x":200,"y":60,"wires":[]},{"id":"8f0b622e.a2e7a","type":"inject","z":"d9ab29db.5928c8","name":"","topic":"","payload":"{\"compact\":{\"outdoor\":{\"temperature\":15.2,\"humidity\":19,\"temperatureTrend\":\"stable\",\"battery_percent\":57,\"rf_status\":71},\"rain\":{},\"temperature\":20.7,\"temperatureTrend\":\"stable\",\"co2\":486,\"humidity\":37,\"noise\":44,\"pressure\":1026,\"pressureTrend\":\"down\",\"station_name\":\"XXX\",\"last_status_store\":1542206768},\"detailed\":[{\"_id\":\"70:ee:50:00:ea:18\",\"cipher_id\":\"XXX\",\"date_setup\":1371091374,\"last_setup\":1371091374,\"type\":\"NAMain\",\"last_status_store\":1542206768,\"module_name\":\"XXX\",\"firmware\":132,\"last_upgrade\":1440121433,\"wifi_status\":50,\"co2_calibrating\":false,\"station_name\":\"XXX\",\"data_type\":[\"Temperature\",\"CO2\",\"Humidity\",\"Noise\",\"Pressure\"],\"place\":{\"altitude\":120.08533983542,\"city\":\"XXX\",\"country\":\"US\",\"timezone\":\"XXX\",\"location\":[\"XXX\",\"XXX\"]},\"dashboard_data\":{\"time_utc\":1542206753,\"Temperature\":20.7,\"CO2\":486,\"Humidity\":37,\"Noise\":44,\"Pressure\":1026,\"AbsolutePressure\":1011.5,\"min_temp\":20.6,\"max_temp\":21.4,\"date_min_temp\":1542204365,\"date_max_temp\":1542182477,\"temp_trend\":\"stable\",\"pressure_trend\":\"down\"},\"modules\":[{\"_id\":\"02:00:00:00:f8:c4\",\"type\":\"NAModule1\",\"module_name\":\"Outdoor\",\"data_type\":[\"Temperature\",\"Humidity\"],\"last_setup\":1371091749,\"dashboard_data\":{\"time_utc\":1542206723,\"Temperature\":15.2,\"Humidity\":19,\"min_temp\":14.8,\"max_temp\":17.3,\"date_min_temp\":1542203724,\"date_max_temp\":1542189727,\"temp_trend\":\"up\"},\"firmware\":44,\"last_message\":1542206762,\"last_seen\":1542206723,\"rf_status\":71,\"battery_vp\":4965,\"battery_percent\":57}]}]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":170,"y":120,"wires":[["9740667f.9f4988"]]}]

I realize that I've misunderstood how JSONata works with the switch node. Gentlemen, many thanks for your help.

1 Like

Sorry to resurrect a very old thread.
I'm trying to do something very similar and thought I would copy this flow to understand how to do it.

I can see that the query is
payload.compact.outdoor.temperature > 0
and that the value for this in the example is 20.7 - so it works as you decribed.

I've tried to change the query to
payload.compact.outdoor.temperature = "20.7"
as this aligns with what I am trying to achieve and I expected that this would match - but it doesn't. I have tried a couple of permutations but can't work this out.

Are you able to tell me where I am going wrong?

Ignore me.....it 15.2!

using = is an assignment and will result as 'true'. You would want to use == or ===

Thanks - I now have this working in a switch node!

Incidentally, is it possible to achieve the same / similar where I am only looking for one value using a Current State node?

You’ll have to explain a bit more, an example maybe??

I've exported my two test flows

[{"id":"5f5de67.df35618","type":"change","z":"7a2dace0.8a2f84","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"data.attributes.ap_mac = \"b4:fb:e4:43:0f:7a\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":850,"y":2100,"wires":[["37de49e8.6789b6"]]},{"id":"37de49e8.6789b6","type":"debug","z":"7a2dace0.8a2f84","name":"switch","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1070,"y":2100,"wires":[]},{"id":"c73b81c0.72287","type":"api-current-state","z":"7a2dace0.8a2f84","name":"","server":"8ca832e4.1e6b4","version":2,"outputs":1,"halt_if":"","halt_if_type":"str","halt_if_compare":"is","entity_id":"device_tracker.unifi_92_40_6c_e1_5f_25_rkgtdu9r","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":690,"y":2180,"wires":[["5f5de67.df35618","a6aa00a4.00703"]]},{"id":"f11d0060.cc1d1","type":"inject","z":"7a2dace0.8a2f84","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":320,"y":2180,"wires":[["c73b81c0.72287"]]},{"id":"a6aa00a4.00703","type":"debug","z":"7a2dace0.8a2f84","name":"from","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1050,"y":2200,"wires":[]},{"id":"6842148c.dd679c","type":"api-current-state","z":"7a2dace0.8a2f84","name":"","server":"8ca832e4.1e6b4","version":2,"outputs":2,"halt_if":"data.attributes.ap_mac = \"b4:fb:e4:43:0f:7a\"","halt_if_type":"jsonata","halt_if_compare":"jsonata","entity_id":"device_tracker.unifi_92_40_6c_e1_5f_25_rkgtdu9r","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":710,"y":2340,"wires":[["bd6bc924.7b9008","edbb3c9c.4e0f6"],["edbb3c9c.4e0f6"]]},{"id":"4735b7ec.b22388","type":"inject","z":"7a2dace0.8a2f84","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":340,"y":2340,"wires":[["6842148c.dd679c"]]},{"id":"bd6bc924.7b9008","type":"api-call-service","z":"7a2dace0.8a2f84","name":"ManCave Mobile","server":"8ca832e4.1e6b4","version":3,"debugenabled":false,"service_domain":"notify","service":"mobile_app_aidan_phone","entityId":"","data":"{\"message\":\"Mancave AP\",\"title\":\"New AP\",\"data\": {\"timeout\": 60}}","dataType":"jsonata","mergecontext":"","mustacheAltTags":false,"outputProperties":[],"queue":"none","x":1090,"y":2340,"wires":[[]]},{"id":"edbb3c9c.4e0f6","type":"debug","z":"7a2dace0.8a2f84","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1100,"y":2420,"wires":[]},{"id":"8ca832e4.1e6b4","type":"server","name":"Home Assistant","version":1,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

The first takes the current state and pushes it to the change node to evaluate whether the condition is met - 'ap_mac' = x

I'm wondering if it is possible to use the IF state in a Current node to do the same? I can't work out the syntax to do it - if it is