Multiple values from a sensor with value_template

Hey, Iam quite new to Home Assistant and I added all my stuff so far and created a dashboard. But I have one sensor, that doesnt have a integration.
And Iam not sure, how to add it as an device and get all the informations.

I did read a lot of documentation and I this works so far for one value:
(configuration.yaml)

sensor:      
  - platform: rest
    resource: http://192.168.XX.XX/data
    name: 'Stromsensor'
    value_template: '{{value_json.meterId}}'
    json_attributes:
      - meterId

This is the JSON i get from the rest call:

{
   "meterId":"1KFM0000130986",
   "deviceId":"EBEN1201002057",
   "msgType":"EMETER_OBIS_V2",
   "measurements":[
      {
         "timestamp":"2023-10-16T08:49:24Z",
         "values":{
            "1-0:1.8.0*255":6022.09,
            "1-0:2.8.0*255":735.245,
            "1-0:16.7.0*255":1065,
            "1-0:31.7.0*255":5.02,
            "1-0:51.7.0*255":1.42,
            "1-0:71.7.0*255":1.32,
            "1-0:32.7.0*255":231.4,
            "1-0:52.7.0*255":232.1,
            "1-0:72.7.0*255":233.9,
            "1-0:14.7.0*255":49.9,
            "1-0:81.7.1*255":121,
            "1-0:81.7.2*255":239,
            "1-0:81.7.4*255":355,
            "1-0:81.7.15*255":165,
            "1-0:81.7.26*255":345,
            "0-0:96.1.0*255":"1KFM0000130986"
         }
      }
   ]
}

Iam no deep into coding, I found some threads about similar topics, but no solution, that I could transfer to my problem.

So maybe some could help me here :slight_smile:

Thank you!

Try this (untested) version:

sensor:      
  - platform: rest
    resource: http://192.168.XX.XX/data
    name: 'Stromsensor'
    value_template: '{{value_json.meterId}}'
    json_attributes_path: '$.measurements.[0]'
    json_attributes:
      - timestamp
      - values

Let me know what it does/doesn’t report.

Hey, thank you for your help! :slight_smile:

That did help to get that:

Values
1-0:1.8.0*255: 6035.05
1-0:2.8.0*255: 735.301
1-0:16.7.0*255: 2089
1-0:31.7.0*255: 6.79
1-0:51.7.0*255: 0.49
1-0:71.7.0*255: 2.36
1-0:32.7.0*255: 233
1-0:52.7.0*255: 235
1-0:72.7.0*255: 235.3
1-0:14.7.0*255: 49.9
1-0:81.7.1*255: 120
1-0:81.7.2*255: 240
1-0:81.7.4*255: 349
1-0:81.7.15*255: 293
1-0:81.7.26*255: 336
0-0:96.1.0*255: 1KFM0000130986

Would be nice, to get every value as an Entity or an attribute, if I can use it to feed my energy integration with that.

Edit
Ah… You store it in the sensor, but you need one Template Sensor for every value, so you get different entities?

That would be one way to do it (or you can explore the RESTful Sensor integration and place each one of the sixteen values into a separate sensor).

Are there always sixteen values reported or can the quantity vary?

Should be always 16, yeah. To use Template Sensors, I need to find regex for JINJA?

Why?

Idk… The result is one big string in a value!? And somehow I have to get to the individual values?! Is there a better way?

As I said: Iam quite new to Home Assistant. Iam watching a lot of tutorials and reading the docs, but not so easy in some specific use cases^^…
And then there is the difference between the normal syntax and the JINJA syntax, thats confusing ^^’

This is how I extract 2 sensors from JSON:

rest:
  - resource: "http://192.168.178.86/rpc/Switch.GetStatus?id=0"
    scan_interval: 10
    timeout: 10
    sensor:
      - name: "Shelly Power"
        value_template: "{{ value_json.apower | round(0) }}"
        device_class: "power"
        unit_of_measurement: "W"
        icon: "mdi:solar-power"
        # force_update: true
      - name: "Shelly Temperatur"
        value_template: "{{ value_json.temperature.tC | round(0) }}"
        device_class: "temperature"
        unit_of_measurement: "°C"
        icon: mdi:temperature-celsius

So to get the values as sensor add them like this:

{{ value_json['measurements'][0]['values']['1-0:1.8.0*255'] }}

returns:

6022.09

Based on the appearance of the result you posted above, it appears to be a dictionary.

A dictionary consists of keys and values. In your result, the first key is 1-0:1.8.0*255 and its associated value is 6035.05.

eXtatic has provided you with an example of how to use the RESTful Sensor integration (as you may recall, I had suggested that you explore using it) to put each one of the sixteen values into a separate sensor.