Unique index or primary key violation: "CONSTRAINT_4B_INDEX_4 ON PUBLIC.METRICS_DOUBLE_TYPE_DEVICE

  • As a max once per second, but usually every some minute

  • By the way, looking at the previous errors I can see many different sensor ids, so it seems like something general, not related to a specific sensor.

  • This morning I was checking the resource logs , and I noticed some odd behaviour I cannot explain:


    The same temperature is transmitted twice at the same timestamp, but my sketch reads the temperature once per second and sends it only when the absolute difference from the previous value is greater than 0.1°C.
    This setup is in place from more than 1 year, and I never saw this beahaviour...
    Any idea?

  • @Daniele Can you post your sketch?

  • /*
     * MySBootloader 1.3.0 16 MHz
     * 9-13 NRF24 (standard mysensors)
     * 1 PIR
     * 2 temperatura (DS18B20)
    #define MY_DEBUG
    #define MY_DEBUG_DETAIL
    #define MY_NODE_ID 3
    #define CHILD_ID_PIR 1
    #define CHILD_ID_TEMP 2
    #define pirPin 8
    #define TempPin 2
    #include <MyRF24_P0_GW2.h>
    #include <MySensors.h>
    #include <MyCircularBuffer.h>
    #include <OneWire.h>
    #include <DallasTemperature.h>
    #define wdt_check_min 10;
    #define wdt_check_max 150;
    #include <MyWDT.h>
    OneWire oneWire(TempPin);
    DallasTemperature sensors(&oneWire);
    DeviceAddress ds18Addr;
    MyMessage msgPIR(CHILD_ID_PIR, V_TRIPPED);
    MyMessage msgTEMP(CHILD_ID_TEMP, V_TEMP);
    int nNoUpdatesPIR = 0, nNoUpdatesTEMP = 0;
    unsigned const int FORCE_UPDATE_N_READS = 300;
    unsigned int calibrationTime = 10;
    unsigned int p;
    unsigned int p_tot;
    float temp, last_temp = 0, tempTot = 0;
    unsigned int msg;
    unsigned int last_msg = 9; //per forzare primo invio
    unsigned const int loop_cycles = 60;
    unsigned const int loop_wait = 1; //secondi tra ogni rilevazione del PIR
    unsigned int wdt_failures = 0;
    unsigned int wdt_check_interval = 30;
    unsigned int wdt_loops = 0;
    unsigned int wdt_uplink = 0;
    MyCircularBuffer<byte,loop_cycles> pirBuffer;
    MyCircularBuffer<float,10> TempBuffer;
    //CircularBuffer<byte,3> wdtBuffer;
    void setup() {
      pinMode(pirPin, INPUT);
      digitalWrite(pirPin, LOW);
      //Fase di calibrazione
      Serial.print("Calibrating sensor ");
      for(int i = 0; i < calibrationTime; i++){
        p = digitalRead(pirPin);
      Serial.println(" done");
      Serial.println("SENSOR ACTIVE");
      if (!sensors.getAddress(ds18Addr, 0)) 
        Serial.println("Unable to find address for Device 0"); 
      sensors.setResolution(ds18Addr, 11);
    void presentation()
      // Send the sketch version information to the gateway and Controller
      sendSketchInfo("P0 - PIR Studio", "1.1");
      present(CHILD_ID_PIR, S_MOTION, "P0 PIR Studio");
      present(CHILD_ID_TEMP, S_TEMP, "P0 Temperatura Studio");
    void loop() {
      p = digitalRead(pirPin);
      p_tot = 0;
      for(int i=0; i<pirBuffer.size(); i++){
        p_tot += pirBuffer[i];  
      if(p_tot > loop_cycles/4){
        msg = 1;
      else {
        msg = 0;
      if (msg != last_msg || nNoUpdatesPIR == FORCE_UPDATE_N_READS) {
        last_msg = msg;
        nNoUpdatesPIR = 0;
        send(msgPIR.set(msg), true);
        #ifdef MY_DEBUG
        Serial.print("P: ");
      } else {
      //check external temp
      //temp = sensors.getTempCByIndex(0);
      temp = sensors.getTempC(ds18Addr);
      if(temp != DEVICE_DISCONNECTED_C)
      tempTot = 0;
      for(int i=0; i<TempBuffer.size(); i++){
        tempTot += TempBuffer[i];  
      tempTot = tempTot / TempBuffer.size();
      if (abs(tempTot - last_temp) > 0.1 || nNoUpdatesTEMP == FORCE_UPDATE_N_READS) {
        last_temp = tempTot;
        nNoUpdatesTEMP = 0;
        send(msgTEMP.set(tempTot,1), true);
        #ifdef MY_DEBUG
        Serial.print("T avg: ");
      } else {
      #ifdef MY_DEBUG_DETAIL
      Serial.print("T: ");
      Serial.print(" - P:");
      wait(loop_wait * 1000);
      //wdt uplink check

  • @Daniele Hi, Sorry for the delayed response.
    Your sketch looks ok. I do not see any issue to repeat a message

    Possible causes:

    • Your gateway response might not reach easily to your temperature node, hence your temperature node keeps repeating the same message and gateway sends to MyController.
    • Restarting MyController operating system might fix this

  • @jkandasa it's probably related to some gateway issue, probably connected to the issue I asked here

    I made a hard restart of the gateway and now the error disappeared.
    Thank you!

  • @jkandasa sorry to re-open an hold discussion.
    the same error appeared again, and this time the gateway hard reset did not solve it.
    do you have any idea on how to better isolate the issue?

  • @Daniele This error happens when we receive multiple messages in the same timeframe. The first one will be inserted into the database and other messages will be dropped with this error.
    If you do not see impact on your data, just ignore [or] find a duplication situation and avoid it.

  • If others will arrive here searching for a similar issue: in my case the final solution has been changing the Arduino Uno I was using as ethernet gateway.

Suggested Topics