Extract from a json file

Hello

I am trying to extract data from a json file… Here is the file:


{
  "devices": [
    {
      "deviceid": "060B4E613A75",
      "lastseen": 1625863477,
      "lowbattery": false,
      "measurement": {
        "idx": 3364560,
        "ts": 1625863470,
        "c": 1625863477,
        "lb": false,
        "t1": 19.6,
        "t2": 65295.0,
        "h": 72.0
      }
    }
  ],
  "success": true
}

I want to extract the t1 value, what should I put to replace the ??? in the following code to get the senor equal to 19.6

sensor:
  - platform: file
    name: temp_piscine_m_new
    file_path: /config/files/piscine_montreal.json
    value_template: '{{ value_json.???? }}'
    unit_of_measurement: "°C"

Thanks…

Try

value_template: '{{ value_json["devices"][0]["measurement"]["t1"] }}'

where you read a key in the object named "devices", followed by the first element in the list (indicated by the square brackets), etc. I didn’t test this and you may need extra logic in case you don’t get a successful response.

EDIT: Changed tl to t1.

Thanks… It’s getting better (no error messages so the entity is created), but I am getting a “unknown” state…We are progressing !

Note that I made a typo that I fixed now above.

Does your logs show any errors?

Also keep in mind if you don’t get the exact same schema JSON that you’ve provided the template could fail in different ways. Without seeing what that could be, I can’t help with that part. You’re welcome to send that.

Then, check the section on “processing incoming data” here. There’s example code right at the end that you can use and take your JSON to experiment with it in the template editor of HA.

Thank you for your help… What is strange is that I am not getting any error message… Just the state of the entity is “unknown”… I tried different alternatives with various fields: “lastseen”, “t1”, “t2” and for each I am getting a “unknown” state… I will investigate your recommendation and try to use the developer tools as well to debug…

A “file” sensor is not what you need, here.
See the description: Only the last line of the file is used
It’s meant for CSV like files.

Can’t you get the JSON via a REST (http) call?

EDIT: If you can’t, or it’s just for testing, removing any CR/LF from the file, so putting the whole json on a single line, should work

EDIT2: Lol. Browet talking to Browet :rofl:

1 Like

If I copy paste the content of the files in the developer tools, I am getting the correct value… Very strange…

The file is created using a curl command…

curl -X POST https://www.data199.com/api/pv1/device/lastmeasurement -d ‘deviceids=myid&phoneid=myphone’ > /config/files/piscine_montreal.json

Funny, “Browet talking to Browet”… Where are you from ? I am from Belgium…

I’m from Belgium, too :wink:
My father’s family is from Genappe / Loupoigne, I guess we are cousins at some degree :slight_smile:

Yeah, so you should use

Thanks for spotting this. Totally missed that.

“Bottom-up” thinking. Your template is correct, so what else could be wrong … :wink:

1 Like