Template - IF JSON DEFINED THEN ELSE

Hi,

I am trying to store the data from JSON sent to a Webhook in a Sensor and its attributes.

The issue is I have 2 versions of the JSON sent to the Webhook so I need to test if the attribute in the JSON exists, if not then set it to the value of the secondary JSON. I have quite a few of these to store and I’m not able to change the JSON being delivered as it is being forwarded to me from a 3rd Party.

The issue I am having is that it will save the data from the fisrt JSON but the failes when it I send the secondary JSON data.

Example

 trigger:
    - trigger: webhook
      webhook_id: "234kdm246434xcnksjfhw"
  sensor:
    - unique_id: a13msykld-bw55-4l2c-8ydf-9c1mai59f3c
      name: "Carvan Tracker Sensor"
      state: "Online"
      attributes:
        Timestamp: {% if trigger.json.position.serverTime is defined %} {{ trigger.json.position.serverTime }} {% else %} {{ trigger.json.serverTime }} {% endif %}

Basically everything after the {% else %} doesn’t work.

I have tried testing with simply changing this to return a simple value for testing, but this also fails.

Timestamp: {% if trigger.json.position.serverTime is defined %} {{ trigger.json.position.serverTime }} {% else %} 'None' {% endif %}

The working JSON is:

{
  "position": {
    "id": 6xxxxx391,
    "attributes": {
      "priority": 1,
      "sat": 14,
      "event": 240,
      "ignition": false,
      "motion": true,
      "rssi": 5,
      "gpsStatus": 1,
      "pto": true,
      "di2": true,
      "batteryLevel": 100,
      "idle": false,
      "power": 13.311,
      "battery": 3.967,
      "ain1": 13.154,
      "temp1": 8,
      "operator": 23415,
      "distance": 0,
      "totalPtoDistance": 19748.3,
      "totalDistance": 19763.16,
      "ignitionStateChange": 1728210884010,
      "hours": 4431000,
      "idleHours": 2577000,
      "ptoHours": 3604448000,
      "auxHours": -3600000,
      "alarm": "TempHigh",
      "temp": 0.8,
      "geofenceIds": ""
    },
    "deviceId": 5671,
    "type": null,
    "protocol": "teltonika",
    "serverTime": "2024-131-09T19:25:05.496+00:00",
    "deviceTime": "2024-11-09T19:25:00.000+00:00",
    "fixTime": "2024-11-09T19:25:00.000+00:00",
    "outdated": false,
    "valid": true,
    "latitude": 52.67233,
    "longitude": -1.5199,
    "altitude": 50,
    "speed": 0,
    "course": 282,
    "address": "",
    "accuracy": 0,
    "network": null
  },
  "event": {
    "id": 160891223,
    "attributes": {},
    "deviceId": 5671,
    "type": "deviceMoving",
    "serverTime": "2024-11-09T19:30:03.908+00:00",
    "positionId": 681858391,
    "geofenceId": 0,
    "maintenanceId": 0
  },
  "device": {
    "id": 5671,
    "attributes": {
      "ICCID": "8988228066612833977",
      "subscriptionStart": "2024-08-08T09:42:38",
      "originalName": "FMC234-1049",
      "make": "Teltonika",
      "model": "FMC234",
      "billing": {
        "profileId": "1376",
        "nextRecursOn": "2025-08-09",
        "frequency": "Annually",
        "recurringStatus": "Active"
      },
      "fwd.url": "https://hooks.nabu.casa/xxxxxxxxxx",
      "fwd.enable": "true"
    },
    "groupId": 0,
    "name": "Caravan",
    "uniqueId": "864636065411049",
    "status": "online",
    "lastUpdate": "2024-11-09T19:30:03.577+00:00",
    "positionId": 681860868,
    "geofenceIds": [],
    "phone": "882xxxxxxx7",
    "model": "",
    "contact": "",
    "category": "caravan",
    "disabled": false,
    "inspectionTwelveMonth": null,
    "inspectionTwelveWeek": null
  }
}

The secondary shorter JSON is:

{
  "id": 6xxxxxxxx8,
  "attributes": {
    "priority": 0,
    "sat": 0,
    "event": 0,
    "ignition": false,
    "motion": false,
    "gpsStatus": 3,
    "pto": true,
    "di2": true,
    "batteryLevel": 100,
    "power": 14.021,
    "battery": 3.986,
    "io6": 13810,
    "temp1": 121.80000000000001,
    "distance": 0,
    "totalPtoDistance": 19744.37,
    "totalDistance": 19759.23,
    "idle": false,
    "ignitionStateChange": 1728210884010,
    "hours": 4431000,
    "idleHours": 2577000,
    "ptoHours": 822449000,
    "auxHours": -3600000,
    "alarm": "TempHigh",
    "temp": 12.180000000000001
  },
  "deviceId": 5671,
  "type": null,
  "protocol": "teltonika",
  "serverTime": "2024-12-08T14:37:37.427+00:00",
  "deviceTime": "2024-10-08T13:38:21.000+00:00",
  "fixTime": "2024-10-08T13:38:21.000+00:00",
  "outdated": false,
  "valid": false,
  "latitude": 52.556483,
  "longitude": -1.52354,
  "altitude": 0,
  "speed": 0,
  "course": 0,
  "address": null,
  "accuracy": 0,
  "network": null
}

These are the states after sending each of the different JSON payloads.

trigger:
    - trigger: webhook
      webhook_id: "234kdm246434xcnksjfhw"
  sensor:
    - unique_id: a13msykld-bw55-4l2c-8ydf-9c1mai59f3c
      name: "Carvan Tracker Sensor"
      state: "Online"
      attributes:
        Timestamp: >
          {% if trigger.json.position is defined %}
            {{ trigger.json.position.serverTime }}
          {% else %}
            {{ trigger.json.serverTime }}
          {% endif %}

Thanks,

That worked perfectly.