RESt sensor -platform: rest

hey,

The past few months I’ve been drawn more and more into hass, how incredibly addictive this is! I have and ask about using the REST sensor and json in hassio.

Im try to see when the door was last opened and by whom. unfortunately they do not have a nice api and I can not call per door with last open or other options and I am bound by transactions.

my goal is to compare if a door has been open in the past 2 days, if so change state of sensor to ‘’ inside ‘’. and I want to see if I can see when a specific person was last in, so eg sensor.room3.attributes [“date”]

  - platform: rest
    name: zkteco test
    json_attributes:
      - message
      - data
    resource: http://12.12.12.12:1234/api/transaction/device/{deviceSn}?deviceSn=AJQE182660067&pageNo=1&pageSize=50&access_token=121212
    value_template: '{{ value_json.code }}'
  - platform: template
    sensors:
      data:
        friendly_name: 'zkdata'
        value_template: '{{ states.sensor.zktest_test.attributes["data"] }}'
      code:
        friendly_name: 'zkmessage'
        value_template: '{{ states.sensor.zktest_test.attributes["message"] }}'

here’s the json response i get form the api now i get this info as a attribute

{
  "code": 0,
  "message": "success",
  "data": [
    {
      "id": 947571,
      "eventTime": "2020-06-26 13:02:15",
      "pin": "123456789",
      "name": "iemand naam 5",
      "lastName": "",
      "deptName": "Team",
      "areaName": "0",
      "cardNo": "123456789",
      "devSn": "A67",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "room1",
      "readerName": "1-1",
      "accZone": null,
      "devName": "1",
      "logId": 36173
    },
    {
      "id": 947571,
      "eventTime": "2020-06-26 13:02:15",
      "pin": "123456789",
      "name": "iemand naam 2",
      "lastName": "",
      "deptName": "Team",
      "areaName": "0",
      "cardNo": "123456789",
      "devSn": "A67",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "room1",
      "readerName": "1-1",
      "accZone": null,
      "devName": "1",
      "logId": 36173
    },
    {
      "id": 947571,
      "eventTime": "2020-06-26 13:02:15",
      "pin": "123456789",
      "name": "iemand naam 2",
      "lastName": "",
      "deptName": "Team",
      "areaName": "0",
      "cardNo": "123456789",
      "devSn": "A67",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "room1",
      "readerName": "1-1",
      "accZone": null,
      "devName": "1",
      "logId": 36173
    },
    {
      "id": 947571,
      "eventTime": "2020-06-26 13:02:15",
      "pin": "123456789",
      "name": "iemand naam",
      "lastName": "",
      "deptName": "Team",
      "areaName": "0",
      "cardNo": "123456789",
      "devSn": "A67",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "room3",
      "readerName": "1-1",
      "accZone": null,
      "devName": "1",
      "logId": 36173
    }
  ]
}

as i can see my data now i can for example receive the last 50 or 100 transactions on 1 divice. my thinking is to scan these strings and adjust the status of a sensor if that room or person that the sensor is about is mentioned in one of the 50 strings.

I am completely new with this type of work and with json in general, but I love puzzles.

Is there a way to ‘read’ the json as the sensor in question recognizes and adjusts its data if applicable?

thanks a lot in advance!

it is also possible to find the data of 1 person via the api. with this data I could eg update the sensors every x hours, I don’t need to know real time what the status is. I just need to figure out how to recognize the data from ‘’ eventPointName ‘’ and then update the sensor. I suspect that this can be done via templats, but I cannot quite figure it out myself.

{
  "code": 0,
  "message": "success",
  "data": [
    {
      "id": 947329,
      "eventTime": "2020-06-26 12:22:13",
      "pin": "12345",
      "name": "iemands naam",
      "lastName": "",
      "deptName": "wij",
      "areaName": "0",
      "cardNo": "123456",
      "devSn": "1234561",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "1-1",
      "readerName": "1",
      "accZone": null,
      "devName": "1",
      "logId": 32821
    },
    {
      "id": 947028,
      "eventTime": "2020-06-26 11:31:42",
      "pin": "12345",
      "name": "iemands naam",
      "lastName": "",
      "deptName": "wij",
      "areaName": "0",
      "cardNo": "123456",
      "devSn": "1234561",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "1-1",
      "readerName": "1",
      "accZone": null,
      "devName": "1",
      "logId": 32821
    },
    {
      "id": 946491,
      "eventTime": "2020-06-26 09:00:58",
      "pin": "12345",
      "name": "iemands naam",
      "lastName": "",
      "deptName": "wij",
      "areaName": "0",
      "cardNo": "123456",
      "devSn": "1234561",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "1-4",
      "readerName": "4",
      "accZone": null,
      "devName": "1",
      "logId": 32821
    },
    {
      "id": 946486,
      "eventTime": "2020-06-26 09:00:16",
      "pin": "12345",
      "name": "iemands naam",
      "lastName": "",
      "deptName": "wij",
      "areaName": "0",
      "cardNo": "123456",
      "devSn": "1234561",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "1-1",
      "readerName": "1",
      "accZone": null,
      "devName": "1",
      "logId": 32821

    },
    {
      "id": 946422,
      "eventTime": "2020-06-26 08:48:24",
      "pin": "12345",
      "name": "iemands naam",
      "lastName": "",
      "deptName": "wij",
      "areaName": "0",
      "cardNo": "123456",
      "devSn": "1234561",
      "verifyModeName": "Only Card",
      "eventName": "Normal Verify Open",
      "eventPointName": "1-1",
      "readerName": "1",
      "accZone": null,
      "devName": "1",
      "logId": 32821
    }
  ]
}

I’m not sure I understand what you are asking for.

With that level of complexity, I’d suggest you take a look at Appdaemon. Makes it easy to integrate complex Python scripts that can process data like this and create & update sensors directly in Home Assistant.

Hi @Troon and @Hellis81 thanks for the fast reply, i got a lot of help from @sanderhuisman in Discord dutch chanel. I’m going to try to clearly put his point here. hopefully this will help others move forward with the same type of question.

to use the data I get from the api call i wil set this:

{% set mydata = states.sensor.zktest_test.attributes["data"] %}
{% set open_events = mydata  | selectattr('eventName','eq', 'Normal Verify Open') | list %}

this is the json string:

{% set my_json = {
  'code': 0,
  'message': 'success',
  'data': [
    {
      'id': 947571,
      'eventTime': "2020-06-26 13:02:15",
      'pin': "123456789",
      'name': "iemand naam 5",
      'lastName': "",
      'deptName': "Team",
      'areaName': "0",
      'cardNo': "123456789",
      'devSn': "A67",
      'verifyModeName': "Only Card",
      'eventName': "Normal Verify Open",
      'eventPointName': "room1",
      'readerName': "1-1",
      'accZone': null,
      'devName': "1",
      'logId': 36173
    },
    {
      'id': 947571,
      'eventTime': "2020-06-26 13:02:15",
      'pin': "123456789",
      'name': "iemand naam 2",
      'lastName': "",
      'deptName': "Team",
      'areaName': "0",
      'cardNo': "123456789",
      'devSn': "A67",
      'verifyModeName' : "Only Card",
      'eventName' : "Normal Verify Open",
      'eventPointName': "room1",
      'readerName': "1-1",
      'accZone': null,
      'devName': "1",
      'logId': 36173
    },
  ]
} %}

{% set open_events = my_json['data'] | selectattr('eventName','eq', 'Normal Verify Open') | list %}

{{ open_events | selectattr('eventPointName','eq', 'room1') | list }}

test in template editor from HA:

with | selectattr(‘eventName’,‘eq’, ‘Normal Verify Open’) i can filteren on normal verify open events

and with selectattr(‘eventPointName’,‘eq’, ‘room1’) you wil have room1; if you want to know if thare where events in you query:

{{ open_events | selectattr('eventPointName','eq', 'room1') | list | length > 0}}

–> you wil know if thare are more then 0 events

withe person you can; query for last event and then you can parse datetime

{% set tm = "2020-06-26 13:02:15" %}
{{ strptime(tm, "%Y-%m-%d %H:%M:%S")}}
1 Like