How to format decimal places of numbers sent to notehub?

I’m using the Feather/Notecarrier-F setup. My Arduino sketch successfully reads a temperature and humidity sensor and sends the data to the notehub. The data sent has many decimal places that I would to truncate to 2 or less before sending to the notehub. I’m wondering if there is a way to do this in “JAddNumberToObject()” commands or some other way.

The Arduino Serial Monitor shows this:

Temp: 78.83 *F
Humidity: 40.19 %
{"req":"note.add","file":"sensors.qo","sync":true,"body":{"temp":78.82719459533692,"humidity":40.19379043579102}}
{"total":1}

Here is my code:

void loop() {

  sensors_event_t humidity, temp;

  htu.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data

  usbSerial.print("Temp: "); 
  usbSerial.print((temp.temperature)*1.8+32);
  usbSerial.println(" *F");
  
  usbSerial.print("Humidity: ");
  usbSerial.print(humidity.relative_humidity);
  usbSerial.println(" %");


  J *req = notecard.newRequest("note.add");
if (req != NULL)
{
  JAddStringToObject(req, "file", "sensors.qo");
  JAddBoolToObject(req, "sync", true);
  J *body = JAddObjectToObject(req, "body");
  if (body)
  {
    JAddNumberToObject(body, "temp", temp.temperature*1.8+32);
    JAddNumberToObject(body, "humidity", humidity.relative_humidity);
  }
  notecard.sendRequest(req);
}
 delay(1800000); //30 minutes
}

Hi @tonyweil,

Welcome to the Blues Wireless community! Someone else may have a more elegant solution, but since your temp data is a float, it doesn’t have a specified number of decimal places to round it to. My hack would be to use an int instead. Multiply your temp value by 100, store it as an int, send that to Notehub, and then when you need it, divide it by 100. Or just perform the rounding with JSONata as it gets routed out of Notehub?

Again, someone might chime in with a better solution :slight_smile:

Thanks,
Rob

JSONata is the way to go! The following rounds temp to 1 decimal place.

$round(body.temp, 1)
3 Likes