REST api - multiple sensors from one json

Hi!

I’m trying to get the sensors from my alarm system to show up in home assistant, but as I just recently started using HA, I’m quite overwhelmed with the configuration options.

I found out that HA can be setup to read sensor data via a REST api that my alarm system provides. The REST JSON looks like this:

[
    {
        "id": "201",
        "name": "ET",
        "state": "closed",
        "inner": false,
        "omittable": false,
        "omitted": false
    },
    {
        "id": "202",
        "name": "SZ1",
        "state": "open",
        "inner": false,
        "omittable": false,
        "omitted": false
    },
    {
        "id": "203",
        "name": "SZ2",
        "state": "closed",
        "inner": false,
        "omittable": false,
        "omitted": false
    }
]

So it’s reporting one block for each zone (=door). for each there is a value “state” that tells me if the door is opened or closed. I would need to parse this json and get a sensor/entity (?) for each of the doors, ideally named with whatever is set in the name attribute of the json.

I managed to get HA to connect, authenticate and parse the json. but i could only make it work with json_path settings (e.g. $.[1].state) to read one state and afaik this would mean I would have to set it up in a way where it sends a rest call for each door. I thought that this might be possible using HA’s templates, but this is where I get stuck as I don’t grasp the concept of how to generate multiple sensors from the json file.

i worked through the docs and examples but did not find an example that worked in my case, so any help there would be highly appreciated.

thanks!
andreas

Using the first example here: https://www.home-assistant.io/integrations/rest/

rest:
  - authentication: basic
    username: "admin" ### change this ###
    password: "password" ### change this ###
    scan_interval: 60
    resource: http://192.168.1.12/status.xml ### change this ###
    sensor:
      - name: "ET"
        value_template: "{{ value_json[0].state }}"
      - name: "SZ1"
        value_template: "{{ value_json[1].state }}"
      - name: "SZ2"
        value_template: "{{ value_json[2].state }}"

dear tom_i - thanks for the reply and help!

i got it working but was wondering if there would also be some kind of generic way to do this? right now, if the order of the elements in the json changes, things will get mixed up in home assistant without a real chance to notice it. so a method that takes all elements listed and adds them as sensors with names from the name attribute would be the most flexible. and I thought that this should be possible with the template examples from the docs, but got stuck with the examples on how templates work.

thanks again for the quick and detailed answer!
andreas

Hi @andreasd1 , did you find a way to dinamically create sensors from json file? I need exactely the same thing and I can’t hard-code the name of each sensor…
I don’t have the skills to create a custom component/integration so I am wondering if there is an easy/doable way to create multiple (and dinamically) sensors from a json file. I can format the json as needed if it is required.
Thanks!