Advanced rest sensor help wanted

Hi,

I am trying to get some data from an API. Most of it is no problem, but in this case I need exactly the following:

I need the user_router_id value of user-routers of the type: “incoming” with active: “true”

{
  "code": 200,
  "status": "success",
  "data": {
    "user-routers": [
      {
        "account_id": "a1b57caa-7257-4220-b570-123456789012",
        "user_id": "472525e0-7c87-4cc4-a81e-0123456789012",
        "user_router_id": "32372b2e-16da-4632-8a3f-84be6d5d9d9c",
        "name": "Kantoor",
        "description": "",
        "type": "incoming",
        "choices": [
          {
            "router_choice_id": "2f4ac919-f312-41ad-b7c5-8b20de707830",
            "router": {
              "id": "c476f2ba-2b53-4fea-ad41-d1e2ad2b2b21",
              "commands": [
                {
                  "name": "dialgeneric",
                  "data": {
                    "account_id": "a1b57caa-7257-4220-b570-123456789012",
                    "phones": [
                      "229aa78f-4e15-4288-89c1-91bba7ac0814",
                      "4ce45847-c12d-4925-a9cc-8dbc332ceedd"
                    ],
                    "numbers": [],
                    "time": "40",
                    "isUserRouter": true,
                    "keep": "1",
                    "user_id": "472525e0-7c87-4cc4-a81e-123456789012"
                  }
                },
                {
                  "name": "voicemailbox",
                  "data": {
                    "box": "59eeb5f3-dbeb-4851-bc9b-0e75221238cd",
                    "play_intro": "1"
                  }
                }
              ]
            },
            "trigger": "begin"
          }
        ],
        "active": false
      },
      {
        "account_id": "a1b57caa-7257-4220-b570-123456789012",
        "user_id": "472525e0-7c87-4cc4-a81e-0123456789012",
        "user_router_id": "1e88f86f-b764-4365-b3af-5aee0b84be47",
        "name": "Thuiswerken",
        "description": "",
        "type": "incoming",
        "choices": [
          {
            "router_choice_id": "4655e7a9-9685-4a10-877d-a883b065aca8",
            "router": {
              "id": "cc11a73f-1270-4693-8987-ae8dc25d034f",
              "commands": [
                {
                  "name": "dialgeneric",
                  "data": {
                    "account_id": "a1b57caa-7257-4220-b570-123456789012",
                    "phones": [
                      "cd95e0da-4b8b-487d-aa5d-cdbe35b2d8cd",
                      "4d9dbfef-484d-46e6-bd54-d8cabdf88dd1",
                      "4ce45847-c12d-4925-a9cc-8dbc332ceedd",
                      "flex",
                      "1cb3916e-3261-4428-b388-07bd5a6e59f1"
                    ],
                    "numbers": [],
                    "time": "40",
                    "isUserRouter": true,
                    "keep": "1",
                    "user_id": "472525e0-7c87-4cc4-a81e-0123456789012"
                  }
                },
                {
                  "name": "voicemailbox",
                  "data": {
                    "box": "59eeb5f3-dbeb-4851-bc9b-0e75221238cd",
                    "play_intro": "1"
                  }
                }
              ]
            },
            "trigger": "begin"
          }
        ],
        "active": true
      },
      {
        "account_id": "a1b57caa-7257-4220-b570-123456789012",
        "user_id": "472525e0-7c87-4cc4-a81e-0123456789012",
        "user_outgoing_router_id": "a6cfb86b-c971-40bd-8969-68a80805b65c",
        "name": "Standaard uitgaande routering",
        "type": "outgoing",
        "description": "",
        "choices": [a1b57caa-7257-4220-b570-123456789012
          {
            "router_choice_id": "c4b8e27f-98de-4c00-90be-d9e0261ec301",
            "router": {
              "id": "ea85215f-dd5d-4b98-b506-4c9430e7cf75",
              "commands": [
                {
                  "name": "calleridnumberown-external",
                  "data": {
                    "number_id": "8c4c8db2-7b06-41b2-84ca-bc5e3a060022",
                    "account_id": "a1b57caa-7257-4220-b570-123456789012"
                  }
                },
                {
                  "name": "calleridnumberown",
                  "data": {
                    "number_id": "5467c822-8edf-4ad6-83e9-b14e4e1a2fcd",
                    "account_id": "a1b57caa-7257-4220-b570-123456789012"
                  }
                },
                {
                  "name": "calleriddisplayname",
                  "data": []
                }
              ]
            },
            "trigger": "begin"
          }
        ],
        "active": true
      }
    ],
    "total": 3
  },
  "links": {
    "self": "/api/1/user-routers?api_key=xxxxxxxxxxxxxxxxxx&page=1",
    "prev": "",
    "next": "",
    "last": "/api/1/user-routers?api_key=xxxxxxxxxxxxxxxxxx&page=1"
  }
}

I am able to extract json data from the API and put it into a rest sensor, but how exactly would I only get the user_router_id of the ACTIVE incoming router in a sensor?

For now I got to this, but the value template doesn’t work.

Anybody who could help me out?

# Get active user router
- platform: rest
  name: "Active router"
  resource: https://IPADDRESS/api/1/user-routers
  headers:
    X-Auth-Token: !secret clearvox_nexxt_api_token
    accept: 'application/json' 
  value_template: >-
      {% for router in value_json.data.user-routers %}
          {% if  router.active == true and router.type == 'incoming' %}
              {{router.name}}
          {% endif %}
      {% endfor %}
  method: GET

A couple of things. First, the JSON you show isn’t valid JSON due to this line:

        "choices": [a1b57caa-7257-4220-b570-123456789012

I’m assuming the stuff after the bracket shouldn’t be there.

Next, you can’t do this:

value_json.data.user-routers

You need to do this instead:

value_json.data['user-routers']

BTW, you can also simplify a bit:

# Get active user router
- platform: rest
  name: "Active router"
  resource: https://IPADDRESS/api/1/user-routers
  headers:
    X-Auth-Token: !secret clearvox_nexxt_api_token
    accept: 'application/json' 
  value_template: >
      {% for router in value_json.data['user-routers']
         if router.active and router.type == 'incoming' %}
           {{ router.name }}
      {% endfor %}
  method: GET

That works… thanks!

1 Like