# Notecard template formatting

**URL:** https://discuss.blues.com/t/notecard-template-formatting/717
**Category:** Firmware
**Created:** [March 4, 2022, 3:26am UTC](https://discuss.blues.com/t/notecard-template-formatting/717 "2022-03-04T03:26:38Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![rberkelm](https://avatars.discourse-cdn.com/v4/letter/r/0ea827/32.png) [@rberkelm](https://discuss.blues.com/u/rberkelm)
#### Post date: [March 4, 2022, 3:26am UTC](https://discuss.blues.com/t/notecard-template-formatting/717/1 "2022-03-04T03:26:38Z")

</div>

Dear friends,  
Can anyone please help me fix a template issue? I had something working but decided to add a new field “key” to my template and body of my note.add json. Now I get an error:  
{“err”:“error adding note: body fields not found in template: key {template-incompatible}”}  
Clearly I have something wrong with my template formatting, but I don’t know what?

```auto

#include <Notecard.h>
#include <Wire.h>
#define serialDebugOut Serial
#define myProductID "com.gmail.rberkelm:berkelbees1"
Notecard notecard;

void setup()
{
#ifdef serialDebugOut
    serialDebugOut.begin(115200);
    notecard.setDebugOutputStream(serialDebugOut);
#endif

    // Initialize the physical I/O channel to the Notecard
#ifdef serialNotecard
    notecard.begin(serialNotecard, 9600);
#else
    Wire.begin();
    notecard.begin();
#endif
  pinMode(LED_BUILTIN, OUTPUT);

    J *req = notecard.newRequest("hub.set");
    JAddStringToObject(req, "product", myProductID);
    JAddStringToObject(req, "mode", "periodic"); 
    JAddStringToObject(req, "file", "data.qos");
    notecard.sendRequest(req);

  // Optimize voltage variable behaviors for LiPo battery
  {
    J * req = notecard.newRequest("card.voltage");
    JAddStringToObject(req, "mode", "lipo");
    notecard.sendRequest(req);
  }

    // Establish a template to optimize queue size and data usage
  {
    J * req = notecard.newRequest("note.template");
    JAddStringToObject(req, "file", "data.qos");
    J * body = JAddObjectToObject(req, "body");
    JAddNumberToObject(body, "weight", 12.1);
    JAddNumberToObject(body, "temp", 12.1);
    JAddNumberToObject(body, "bat", 12.1);
    JAddNumberToObject(body, "netrawwt", 14);
    JAddStringToObject(body, "key", "AAAAAAAAAAAAAAAA");
    notecard.sendRequest(req);
  }
  
  serialDebugOut.println ("end Setup");
}
void loop()
{
  serialDebugOut.println ("Starting loop");
  digitalWrite(LED_BUILTIN, HIGH);
//temp from Notecard
    double temperature = 0;
    J *rsp = notecard.requestAndResponse(notecard.newRequest("card.temp"));
    if (rsp != NULL) {
        temperature = JGetNumber(rsp, "value");
        notecard.deleteResponse(rsp);
    }

 // Voltage from Notecard on its V+ pin.
    double voltage = 0;
    rsp = notecard.requestAndResponse(notecard.newRequest("card.voltage"));
    if (rsp != NULL) {
        voltage = JGetNumber(rsp, "value");
        notecard.deleteResponse(rsp);
    }

    float weight = (double) random(800, 850) / 10.0; 
    long netrawwt = (double) random(410000, 460000);
    J *req = notecard.newRequest("note.add");
    if (req != NULL) {
        JAddBoolToObject(req, "sync", true);
        J *body = JCreateObject();
        if (body != NULL) {
            JAddItemToObject(req, "body", body);
            JAddNumberToObject(body, "temp", temperature);  
            JAddNumberToObject(body, "bat", voltage);
            JAddNumberToObject(body, "weight", weight);
            JAddNumberToObject(body, "netrawwt", netrawwt);
            JAddStringToObject(body, "key","ABCDABCDABCDABCD");
        }
        notecard.sendRequest(req);
    }
digitalWrite(LED_BUILTIN, LOW);   
serialDebugOut.println ("Going to sleep");
ESP.deepSleep(900*1000*1000, WAKE_RF_DEFAULT); //sec to sleep
}

```

---

<div class="post-metadata">

### Author: ![gwolff](https://avatars.discourse-cdn.com/v4/letter/g/a88e57/32.png) [@gwolff](https://discuss.blues.com/u/gwolff)
#### Post date: [March 4, 2022, 2:54pm UTC](https://discuss.blues.com/t/notecard-template-formatting/717/2 "2022-03-04T14:54:47Z")

</div>

Hi Ray! Thanks for posting on the forum.

I don’t see anything jumping out at me.

Can you send the JSON messages that appear in the Serial Monitor? (I’m assuming you are using the Arduino IDE or something similar) Since the culprit may include the `setup` function, it is best to start the serial monitor, and then reset the MCU.

Also, can you provide the firmware version running on Notecard? You can do this using the [Notecard Playground](https://dev.blues.io/notecard-playground) if you plug in the Notecard via USB to your laptop.

```auto
{"req":"card.version"}

```

Or you can check the Devices list in your Notehub project, and select the “Notecard Firmware” tab

---

<div class="post-metadata">

### Author: ![rberkelm](https://avatars.discourse-cdn.com/v4/letter/r/0ea827/32.png) [@rberkelm](https://discuss.blues.com/u/rberkelm)
#### Post date: [March 5, 2022, 3:07am UTC](https://discuss.blues.com/t/notecard-template-formatting/717/3 "2022-03-05T03:07:26Z")

</div>

Thanks for the quick response! The Notecard firmware version is: 1.5.6.13782.  
The output from the serial monitor is:

```auto
12:49:00.260 -> {}
12:49:00.260 -> {"req":"card.voltage","mode":"lipo"}
12:49:00.401 -> {"minutes":7,"hours":649,"mode":"normal","value":3.796010154485245,"vmin":3.79,"vmax":4.9400000000000007,"vavg":4.556425269645609,"daily":-0.02,"weekly":-0.91}
12:49:00.401 -> {"req":"note.template","file":"data.qos","body":{"weight":12.1,"temp":12.1,"bat":12.1,"netrawwt":14,"key":"AAAAAAAAAAAAAAAA"}}
12:49:00.590 -> {"bytes":42}
12:49:00.590 -> end Setup
12:49:00.590 -> Starting loop
12:49:00.590 -> {"req":"card.temp"}
12:49:00.637 -> {"value":31.5,"calibration":-1.0}
12:49:00.637 -> {"req":"card.voltage"}
12:49:00.731 -> {"minutes":7,"hours":649,"mode":"normal","value":3.7982544713494845,"vmin":3.79,"vmax":4.9400000000000007,"vavg":4.556425269645609,"daily":-0.02,"weekly":-0.91}
12:49:00.731 -> {"req":"note.add","sync":true,"body":{"temp":31.5,"bat":3.798254471349485,"weight":81,"netrawwt":422311,"key":"ABCABCABCABCABCA"}}
12:49:00.952 -> {"err":"error adding note: body fields not found in template: key {template-incompatible}"}
12:49:00.952 -> Going to sleep

```

When I remove the line “JAddStringToObject(body, “key”,“ABCABCABCABCABCA”);” from my note.add json, the error disappears and the Notecard syncs fine. However when I check what is received on Notehub it appears that the new template has not “taken” because the last note.add data transaction still shows “file”: “data.qo” instead of “file”: “data.qos” (another change I made to the template).

```auto
"event": "482be34a-4c61-494b-b64e-893cc049faa1",
    "session": "025e3fb1-67f6-49c5-862a-64c21d1cdf7f",
    "best_id": "dev:864475040536624",
    "device": "dev:864475040536624",
    "product": "product:com.gmail.rberkelm:berkelbees1",
    "received": 1646448672.313646,
    "routed": 1646448675,
    "req": "note.add",
    "when": 1646448633,
    "file": "data.qo",
    "updates": 1,
    "body": {
        "bat": 3.796875,
        "netrawwt": 423221,
        "temp": 31.5,
        "weight": 80.5
    },
    "best_location_type": "tower",
    "best_lat": -28.0311625,
    "best_lon": 153.268578125,
    "best_location": "Lower Beechmont",
    "best_country": "AU",
    "best_timezone": "Australia/Brisbane",
    "tower_when": 1646448671,
    "tower_lat": -28.0311625,
    "tower_lon": 153.268578125,
    "tower_country": "AU",
    "tower_location": "Lower Beechmont",
    "tower_timezone": "Australia/Brisbane",
    "tower_id": "505,1,28687,147283213"

```

---

<div class="post-metadata">

### Author: ![rberkelm](https://avatars.discourse-cdn.com/v4/letter/r/0ea827/32.png) [@rberkelm](https://discuss.blues.com/u/rberkelm)
#### Post date: [March 18, 2022, 1:41am UTC](https://discuss.blues.com/t/notecard-template-formatting/717/4 "2022-03-18T01:41:59Z")

</div>

So as not to leave this thread hanging, I managed to overcome my template issue by hooking the Notecard up to the in-browser terminal and clearing the template followed by a sync. Now it will run my MCU code and send the “key” field. I guess the Notecard just got itself into a bad state?  
However it still sends the Note as a “.qo” rather than a “.qos”. It doesn’t particularly worry me as I understand that “.qo” Notes are still secure in that they go through a VPN? All the same, if anyone can identify what I’m doing wrong, I’d love to hear it!

---

<div class="post-metadata">

### Author: ![zfields](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.blues.com/zfields/32/29_2.png) [@zfields](https://discuss.blues.com/u/zfields)
#### Post date: [March 18, 2022, 12:01pm UTC](https://discuss.blues.com/t/notecard-template-formatting/717/5 "2022-03-18T12:01:07Z")

</div>

In the logs you provided, it doesn’t look like you specified a `file` parameter to the `note.add` operation.

```auto
{"req":"note.add","sync":true,"body":{"temp":31.5,"bat":3.798254471349485,"weight":81,"netrawwt":422311,"key":"ABCABCABCABCABCA"}}

```

When this occurs, it will pick the default Notefile, `data.qo`, for you.

Try using this request instead…

```auto
{"req":"note.add","file":"data.qos", "sync":true,"body":{"temp":31.5,"bat":3.798254471349485,"weight":81,"netrawwt":422311,"key":"ABCABCABCABCABCA"}}

```

---

<div class="post-metadata">

### Author: ![rberkelm](https://avatars.discourse-cdn.com/v4/letter/r/0ea827/32.png) [@rberkelm](https://discuss.blues.com/u/rberkelm)
#### Post date: [March 18, 2022, 11:27pm UTC](https://discuss.blues.com/t/notecard-template-formatting/717/6 "2022-03-18T23:27:51Z")

</div>

Aahh… yep, that will do it! Thanks for pointing this out. Another lesson under the belt…
