Attaching Timestamp to delayed value array

Hi Everyone,

I got great help earlier today and now I need a little bit more of everyone expertise.

My sensor is measuring every 15 Minutes but only does an uplink every hour. The payload looks like this, please take note of the four value keys:

{
  "end_device_ids": {
    "device_id": "eui-bc97",
    "application_ids": {
      "application_id": "klax-1"
    },
    "dev_eui": "BC9740",
    "join_eui": "70B3D5",
    "dev_addr": "260B7"
  },
  "correlation_ids": [
    "gs:uplink:01HPRZG71HTSG"
  ],
  "received_at": "2024-02-16T12:50:44.094863791Z",
  "uplink_message": {
    "session_key_id": "AY1o2VXaOfg8o",
    "f_port": 3,
    "f_cnt": 344,
    "frm_payload": "BEVVEQMJAUVTWREDm8SFAREPS38Qq0t/EE1Lfw/gS38PdA==",
    "decoded_payload": {
      "header": {
        "batteryPerc": 100,
        "configured": true,
        "connTest": false,
        "deviceType": "SML Klax",
        "meterType": "SML",
        "version": 1
      },
      "msgInfo": {
        "msgCnt": 1,
        "msgIdx": 85,
        "msgNum": 1
      },
      "payloads": [
        {
          "id": "090145535911039bc485",
          "type": "serverID"
        },
        {
          "register": {
            "filterActive": true,
            "filterId": 0,
            "unit": "Wh",
            "values": [
              {
                "valid": true,
                "value": 16715947
              },
              {
                "valid": true,
                "value": 16715853
              },
              {
                "valid": true,
                "value": 16715744
              },
              {
                "valid": true,
                "value": 16715636
              }
            ]
          },
          "type": "filter"
        }
      ],
      "type": "app"
    },
    "rx_metadata": [
      {
        "gateway_ids": {
          "gateway_id": "eui-58a0",
          "eui": "58A0CB"
        },
        "time": "2024-02-16T12:50:43.775126934Z",
        "timestamp": 4243676867,
        "rssi": -91,
        "channel_rssi": -91,
        "snr": 9.5,
        "location": {
          "latitude": 50.950,
          "longitude": 11.0,
          "source": "SOURCE_REGISTRY"
        },
        "uplink_token": "CiIKIAoUZXVpLTU4YTBjYmZmZmU4MDU0NTISCFigy//+gFRSEMO9xecPGgwIo7S9r",
        "received_at": "2024-02-16T12:50:43.863979522Z"
      }
    ],
    "settings": {
      "data_rate": {
        "lora": {
          "bandwidth": 125000,
          "spreading_factor": 7,
          "coding_rate": "4/5"
        }
      },
      "frequency": "868300000",
      "timestamp": 4243676867,
      "time": "2024-02-16T12:50:43.775126934Z"
    },
    "received_at": "2024-02-16T12:50:43.890499663Z",
    "consumed_airtime": "0.092416s",
    "version_ids": {
      "brand_id": "alpha-omega-technology",
      "model_id": "klax",
      "hardware_version": "2.0",
      "firmware_version": "2.0",
      "band_id": "EU_863_870"
    },
    "network_ids": {
      "net_id": "000013",
      "ns_id": "EC656E0000000181",
      "tenant_id": "ttn",
      "cluster_id": "eu1",
      "cluster_address": "eu1.cloud.thethings.network"
    }
  }
}

I can’t even imagine a way on how to deal with this. Basically I need to extract each value item and then associate it with a Timestamp that has an offset of x minutes to the arrival of the uplink. Finally I would have to save the data to HA.

For now I have resorted to just saving the first array Value:

mqtt:
  sensor:
    - name: "Stromverbrauch"
      state_topic: "ttn/v3/klax-1@ttn/devices/eui-bc9740d2c7/up"
      value_template: "{{ value_json['uplink_message']['decoded_payload']['payloads'][1]['register']['values'][0]['value'] }}"
      unit_of_measurement: "Wh"
      device_class: "energy"
      state_class: "total_increasing"
      unique_id: klax_keller
      device:
        identifiers: "klaxhome"
        manufacturer: "AO-T"
        name: "Stromzähler"

As you can see I have a grasp on the concept but I have no idea how to make this happen. Any advice is appreciated.

HA does not have a way to alter the timestamps for incoming data. It is not impossible, but would need you to directly alter the timestamps in the database, which is not easy and not recommended.

Depending what this data represents, you could combine the four 15-min readings into a single hourly reading and record that, if that would work for you? It looks as though this is an energy consumption sensor, though — your “reasonable” options are:

  • accept hourly resolution
  • get your sensor to send more frequently

Ok. thank you!

Would be possible to use a script to receive and format the data before it gets written to the HA database?

An incoming state update is written with a timestamp reflecting when it is received. The update itself does not include the timestamp to which the data is associated.

You could include the “real” timestamp as an attribute of the state update, but that won’t affect how HA sees it for graphs, statistics etc.

You can also check out this thread; it’s still not clear to me if the method mentioned there can be successfully used in an automation or not. But feel free to try and report back.

1 Like