API Sensor to make one sensor from multiple attribute path into states and attributes

Hi community, please help I’m too new with these things sorry if this may be a duplicate post.

Here’s my JSON:

  "status": "success",
  "timestamp": "2024-07-03T17:17:00.736Z",
  "currency": "MYR",
  "unit": "toz",
  "metal": "gold",
  "rate": {
    "price": 11117.613,
    "ask": 11118.2734,
    "bid": 11116.9525,
    "high": 11156.3182,
    "low": 10976.543,
    "change": 128.805,
    "change_percent": 1.17
  }
}

I’m trying to get a sensor from API request but my json data has different attributes path. I would like to get my sensor to show in the form of (last image):

State/Attributes - Timestamp
Attributes - price
Attributes - ask
Attributes - bid
Attributes - high
Attributes - low
Attributes - change
Attributes - change_percent

Currently my DIY sensors are in the sensor.yaml file.

If I were to give it an attribute path I can only get to the attributes of “rates” but no timestamp attributes (below is my current configuration in sensor.yaml).

#  Spot Gold (in Troy Oz)
- platform: rest
  resource: https://api.metals.dev/v1/metal/spot?api_key=[MY_API_KEY]&metal=gold&currency=MYR
  name: "Spot Gold"
  scan_interval: 28800
  headers:
    content-type: "application/json"
  value_template: "{{ value_json.value }}"
  json_attributes_path: "$['rate']"
  json_attributes:
    - timestamp
    - price
    - ask
    - bid
    - high
    - low
    - change
    - change_percent 

My question would be how can I make a sensor with the timestamp (in state), price (in attributes), change_percent (in attributes) with just one API request.
Alternatively everything (timestamp,price,change_percent) in attributes?

I believe this:

value_template: "{{ value_json.value }}"

Just needs to be

value_template: "{{ value_json.timestamp }}"

The reason it doesn’t work in the attributes is because you set the path to ‘rate’ and the timestamp is in the parent.

1 Like

Thank you thank you thank you !!! You’re a legend !!! Took me some time figuring out how to do it turns out its that simple thank you buddy !!!

I’m wondering if it might be better to set the price as the state and have the timestamp as attribute?
That means the price can be a graph. The timestamp will just be a text anyways so it won’t be any better having it in state than attribute.