Routing data from a field unique to just one device

I have a number of remote devices all sending the same data to ThingSpeak. The single route works great. Now I have one device where I want just one of the fields to be different from all the rest. So instead of Field 5 being “body.EC” I would like it to be “body.LUX” – but just for the one device.

I thought I could use the substitution feature as described in this post:

So I changed this line in the route: “field5”: body.EC,
To: “field5”: $field_5_data,

Then I added an environmental variable called field_5_data and set it to body.lux for the one remote where I’d like Field 5 to send lux data, and set all the other device field_5_data env variables to body.EC and as they will continue to send EC data.

I now see on Notehub that indeed, I’m getting either EC or Lux values according to the body / JSON tabs in the Event log. However, the route log response from ThingSpeak says it’s only receiving the variable name – body.EC or body.LUX – and not the actual value.

I have read through a number of forum posts and the docs, but still puzzled. Appreciate any suggestions as to what I’m missing. thanks!
…Norm…

Hello @noforan ,

Are you familiar with https://try.jsonata.org/?

I used to struggle getting my data in the right shape, until I came across that website.

If you’re not familiar, you can grab the entire Note JSON and paste it on the left hand side of the screen, then you can paste your JSONata expression at the top. Finally you will see the result of the JSONata expression in the bottom panel.

Once you have that setup, can you grab a screenshot, so we can see what expression you are using to create the values you are expecting and how our implementation is different.

Thanks!
Zak

I am not familiar with that site, but I’ll give it go and report back.
thanks!

I’ve spent some with JSONata Exerciser and see how it’s useful. The two things I’ve gleaned from it are that: (1) even though it works, it doesn’t like one of my variable names, so I will change it. And (2) I can tell that it doesn’t at all see the field I’m trying to substitute. Strangely, Notehub itself displays the correct result, for both variations, in the body tab of the Event.

So clearly I’m using the wrong expression to try to substitute variables. I’m guessing this is less a Blues Wireless question and more of a general JSON question, however not being all that familiar with JSON, I think I’m not asking the right question. I’ve searched “JSON variable substitution” in a variety of other forums but not finding anything that looks like the solution.

Any other suggestions or examples you could point me towards? Thanks!

Stumbled upon the answer…

Using Conditional Logic

This is a kind of IF statement where you use one variable if it’s available, and if it’s not, then use another one.

In my case it looks like:

{
“api_key”: $device_key,
“field1”: body.airTmpC,
“field2”: body.humid,
“field3”: body.wTmpC@Sonde,
“field4”: body.DO,
“field5”: body.EC ? body.EC : body.LUX,
“field6”: body.Salty,
“field7”: body.cVoltge,
“field8”: body.pH
}

Which in words, to me, means – body.EC is available?
If yes – use it : if not – use body.LUX

I tested it and it did route the proper data in both cases.

cheers!

1 Like