Webhook, JSON & Sensors to store the data received

Hi,

I’m trying to work out how to store all the information receivesd in JSON via a Webhook to a Sensor so that I can use that data for instance on dashboards, or to update the Device_Tracker and have the data stored during a reboot of HA.

This is the JSON received:

[{'BatteryLevel': 100, 'BatteryVoltage': 4.011, 'ChannelId': 1225350, 'CodecId': 142, 'DeviceId': 5998436, 'DeviceName': 'Robs Car', 'DeviceTypeId': 2334, 'EngineIgnitionStatus': False, 'EventEnum': 240, 'EventPriorityEnum': 1, 'ExternalPowersourceVoltage': 0, 'GsmMcc': 234, 'GsmMnc': 15, 'GsmOperatorCode': '23415', 'GsmSignalLevel': 60, 'Ident': '8646360657336459', 'MovementStatus': False, 'Peer': '85.255.237.214:63364', 'PositionAltitude': 49, 'PositionDirection': 357, 'PositionLatitude': 51.123456, 'PositionLongitude': -1.123456, 'PositionSatellites': 12, 'PositionSpeed': 0, 'ProtocolId': 14, 'ServerTimestamp': 1728647840.668736, 'Timestamp': 1728647838}]

As this data is received I would like to write all the different values into a Sensor. Is there a way to parse the data and create attributes in the sensor for each value, so even if the JSON changed (add or subtract values) all the values would be stored?

I’m new go all of these and learning as I go, so if you can provide any instructions as gto where to create anything you suggest that would be really helpful.

Thanks for the help.

Look up the second entry under templating.

Its not for the faint of heart but it works.

That said *saving the data off somewhere isn’t usually the best course of action for HA. HA works best with events - not polling

Let us know what your source data is and how you get it and someone will definitely point te way.

This seems to be exactly what you need:

1 Like

Hi

The source data is coming from a vehicle tracker. It’s routed via a 3rd party who converts the raw stream from the tracker to JSON and then POSTs the JSON to a webhook I created on HA.

From there I extract the longitude and latitude data and update a device_tracker which is displayed on a map on a dashboard.

That part works flawlessly, but along with the positioning data there is a whole host of other info I would like to store in a sensor so I can display it alongside the map and also create automations, for instance if the vehicle leaves a geofenced area there will be an instant alert and I’d like to then alert via HA to various locations.

This is the data received.

[
{
"BatteryLevel":95,
      "BatteryVoltage":3.905,
      "ChannelId":1225350,
      "CodecId":142,
      "DeviceId":5998436,
      "DeviceName":"Robs Car",
      "DeviceTypeId":2334,
      "EngineIgnitionStatus":false,
      "EventPriorityEnum":0,
      "ExternalPowersourceVoltage":0,
      "GsmMcc":234,
      "GsmMnc":15,
      "GsmOperatorCode":"23415",
      "GsmSignalLevel":60,
      "Ident":"864636065799777",
      "MovementStatus":false,
      "Peer":"85.255.237.123:23273",
      "PositionAltitude":49,
      "PositionDirection":357,
      "PositionLatitude":51.99997,
      "PositionLongitude":-1.82427,
      "PositionSatellites":14,
      "PositionSpeed":0,
      "ProtocolId":14,
      "ServerTimestamp":1728651530.192758,
      "Timestamp":1728651438
}
]

If those square brackets are correct, you’ll need to use, for example:

trigger.json[0]['BatteryLevel']

If your device tracker is working, use Zones.

Thanks for the help so far. Been looking over the links and suggestions and watched a few videos, but still stuck on this.

I have created a templates.yaml file and linked that in the configuration.yaml, and have tried to add the template witht he webhook trigger in there.

I don’t seem to be able to get this to work.

Here is what i have in the templates.yaml

#### Store Car Tracker Attibutes ####################
  - trigger: webhook
      Webhook_id: -2spsF8QBr-xxx_xxxxxxxxxx
    sensor:
      - name: "Car Tracker Sensor"
        state: "{{as_datetime(trigger.json[0]['Timestamp'])}}"
        json_attributes_path: "$.0"
        json_attributes:
          - "BatteryLevel"
          - "BatteryVoltage"
          - "EngineIgnitionStatus"
          - "ExternalPowersourceVoltage"
          - "MovementStatus"
          - "PositionAltitude"
          - "PositionDirection"
          - "PositionLatitude"
          - "PositionLongitude"

And this is the link in the configuration.yaml

#### Yaml File Locations ############################################
automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
homeassistant:
  customize: !include customize.yaml
template: !include templates.yaml

Does this look correct as I’m trying to coble together bits of information from various places as I can’t find an example of what I am trying to do.

Thanks for the help.

Look again at the example in the link I posted. You’re missing another trigger, and webhook_id needs to be lower case.

does this look better?

#### Store Car Tracker Attibutes ####################
- trigger:
    - trigger: webhook
      webhook_id: -2spsF8QBr-KxL_yGKryIkJIj
  sensor:
    - unique_id: a1ed2d7d-bd35-4c3c-8e6f-9c1416b6af3c
      name: "Car Tracker Sensor"
      state: "{{as_datetime(trigger.json[0]['Timestamp'])}}"
      json_attributes_path: "$.0"
      json_attributes:
        - "BatteryLevel"
        - "BatteryVoltage"
        - "EngineIgnitionStatus"
        - "ExternalPowersourceVoltage"
        - "MovementStatus"
        - "PositionAltitude"
        - "PositionDirection"
        - "PositionLatitude"
        - "PositionLongitude"

Does it work? Might need to put that webhook ID in quotes

webhook_id: "-2spsF8QBr-KxL_yGKryIkJIj"

Hopefully I’m looking in the right place, Setting > Devices & Services > Entities and searching for “sensor.car_tracker_sensor”.

If thats the right place I’m not seeing the sensor being created.

I can see this in the logs:

2024-10-11 22:13:58.639 ERROR (MainThread) [homeassistant.config] Invalid config for 'template' at templates.yaml, line 10: 'json_attributes' is an invalid option for 'template', check: sensor->0->json_attributes
Invalid config for 'template' at templates.yaml, line 9: 'json_attributes_path' is an invalid option for 'template', check: sensor->0->json_attributes_path

So, I just deleted some of the code so it was as follows:

#### Store Car Tracker Attibutes ####################
- trigger:
    - trigger: webhook
      webhook_id: "-2spsF8QBr-KxL_yGKryIkJIj"
  sensor:
    - unique_id: a1ed2d7d-bd35-4c3c-8e6f-9c1416b6af3c
      name: "Car Tracker Sensor"
      state: "Event Fired"

and this now works and i can see the sensor

result

Just need to work out how to add the JSON values as attributes.

Ah yes, no json_attributes in a template sensor, missed that. You’ll need to call out the attributes individually:

      ...
      name: "Car Tracker Sensor"
      state: "{{as_datetime(trigger.json[0]['Timestamp'])}}"
      attributes:
        battery_level: "{{ trigger.json['0']['BatteryLevel'] }}"

Docs for attributes: Template - Home Assistant

Or as separate sensors like in the linked example.

1 Like

Thanks for the guidenace, all working now.

1 Like