REST sensor JSON help

Hi all

I’m trying to connect to a REST endpoint. That in itself is not that difficult. I’ve got the following:

    #ALARM
  - platform: rest
    name: ClimaxSensor
    resource: 'http://172.16.1.19/action/deviceListGet'
    authentication: basic
    username: !secret climax_user
    password: !secret climax_pass
    value_template:  '{{ value_json.senrows }}'
    json_attributes:
      - senrows

  - platform: template
    sensors:
      cs_status1:
        friendly_name: "Entrevindue"
#        value_template: '{{states.sensor.climaxsensor[0][3]["status"]}}'
        value_template: >
          {{ state_attr('sensor.climaxsensor', 'senrows')[3]['status']}}
      cs_status2:
        friendly_name: "Peter vindue"
        value_template: '{{states.sensor.climaxsensor.senrows[4]["status"]}}'

But the returned JSON is for longer than the 255 char limit: (refer to bottom code block).

Question: how do I access the lot? I’ve figured that I cannot create jsonpath expressions against the JSON with a filter (eg. [?(@name==“Marie vindue”)] <-- I cannot make that work.

I figured I need something like senrows in the json_attributes attribute in the sensor declaration… but that does not do the trick for me… Can anybody help how to access the array? I’ve tried for several days, but I cannot find out how to do it… :neutral_face:

Thanks in advance

{
  "senrows": [
    {
      "area": 1,
      "zone": 15,
      "type": 27,
      "type_f": "IR Camera",
      "name": "Alrum foto",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 9",
      "status": "",
      "id": "XXXXX593530",
      "su": 1
    },
    {
      "area": 1,
      "zone": 2,
      "type": 37,
      "type_f": "Keypad",
      "name": "Bryggers",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 9",
      "status": "",
      "id": "XXXXXdb5170",
      "su": 0
    },
    {
      "area": 1,
      "zone": 1,
      "type": 37,
      "type_f": "Keypad",
      "name": "Entre",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Good, 5",
      "status": "",
      "id": "XXXXXdb7470",
      "su": 0
    },
    {
      "area": 1,
      "zone": 7,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Entrevindue",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Good, 4",
      "status": "Door Close",
      "id": "XXXXX75cd10",
      "su": 1
    },
    {
      "area": 1,
      "zone": 13,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Peter vindue",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 9",
      "status": "Door Close",
      "id": "XXXXX75ce10",
      "su": 1
    },
    {
      "area": 1,
      "zone": 6,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Alrumd\u00f8r trp",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 8",
      "status": "Door Close",
      "id": "XXXXX75d210",
      "su": 1
    },
    {
      "area": 1,
      "zone": 3,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Bryggersd\u00f8r",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 9",
      "status": "Door Close",
      "id": "XXXXX75e310",
      "su": 1
    },
    {
      "area": 1,
      "zone": 4,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Bryggersvindue",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 9",
      "status": "Door Close",
      "id": "XXXXX75fd10",
      "su": 1
    },
    {
      "area": 1,
      "zone": 8,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Entred\u00f8r",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 6",
      "status": "Door Close",
      "id": "XXXXX762810",
      "su": 1
    },
    {
      "area": 1,
      "zone": 14,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Badv\u00e6relse",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 9",
      "status": "Door Close",
      "id": "XXXXX763910",
      "su": 1
    },
    {
      "area": 1,
      "zone": 11,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Alrumd\u00f8r",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 7",
      "status": "Door Close",
      "id": "XXXXX764710",
      "su": 1
    },
    {
      "area": 1,
      "zone": 5,
      "type": 4,
      "type_f": "Door Contact",
      "name": "K\u00f8kkend\u00f8r",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 9",
      "status": "Door Close",
      "id": "XXXXX764910",
      "su": 1
    },
    {
      "area": 1,
      "zone": 12,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Peter terrasse",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 9",
      "status": "Door Close",
      "id": "XXXXX765b10",
      "su": 1
    },
    {
      "area": 1,
      "zone": 9,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Marie vindue",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 8",
      "status": "Door Close",
      "id": "XXXXX765e10",
      "su": 1
    },
    {
      "area": 1,
      "zone": 10,
      "type": 4,
      "type_f": "Door Contact",
      "name": "Marie terrassed\u00f8r",
      "cond": "",
      "cond_ok": "1",
      "battery": "",
      "battery_ok": "1",
      "tamper": "",
      "tamper_ok": "1",
      "bypass": "No",
      "temp_bypass": "0",
      "rssi": "Strong, 8",
      "status": "Door Close",
      "id": "XXXXX9b7110",
      "su": 1
    }
  ]
}

Two comments…

First, if the only reason for sensor.climaxsensor is to get at the senrows attribute, then just put whatever in value_template.

Second, the value_template in your second template sensor is wrong. You should make it like the first.

OK, tried that, but to no avail… I get this in the log:

homeassistant.exceptions.InvalidStateError: Invalid state encountered for entity id: sensor.climaxsensor. State max length is 255 characters.

What should I do? Thanks

What does the code look like now?

    #ALARM
  - platform: rest
    name: ClimaxSensor
    resource: 'http://172.16.1.19/action/deviceListGet'
    authentication: basic
    username: !secret climax_user
    password: !secret climax_pass
    value_template:  '{{ value_json.senrows }}'
    json_attributes:
      - senrows

  - platform: template
    sensors:
      cs_status1:
        friendly_name: "Entrevindue"
#        value_template: '{{states.sensor.climaxsensor[0][3]["status"]}}'
        value_template: >
          {{ state_attr('sensor.climaxsensor', 'senrows')[3]['status']}}
      cs_status2:
        friendly_name: "Peter vindue"
        value_template: >
          {{ state_attr('sensor.climaxsensor', 'senrows')[4]['status']}}

Pretty much the same :thinking:
I’m just not that good with this error :upside_down_face: To be frank - I need help on writing the right lines :hugs:


2019-08-24 16:04:15 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Entrevindue: UndefinedError: None has no element 3,
2019-08-24 16:04:14 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Peter vindue: UndefinedError: None has no element 4

I also get this… but that kinda makes sense as it doesn’t accept more than 255 chars…
Just for the info

Take a look at this thread Need help storing URL for use in generic IP camera (over 255 chars) that might be helpful.

You didn’t implement my first suggestion. Change value_template in the rest sensor.

You’re right!
And somehow that kinda did the trick! :slight_smile: In combo with the other suggeston as well…
Now it looks like this: :smiley:

Wee, I’m of to another automation!
Thanks all!

That actually brought me the solution. Thanks!