Using a switch node with JSONata

#1

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.

0 Likes

#2

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

#3

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"}]

0 Likes

#4

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

0 Likes

#5

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, ...
0 Likes

#6

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.

0 Likes

#7

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

0 Likes

#8

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.

0 Likes

#9

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
        }
      ]
    }
  ]
}```
0 Likes

#10

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):

0 Likes

#11

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":[]}]
0 Likes

#12

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.

0 Likes

#13

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"]]}]
0 Likes

#14

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

1 Like