How to use a attribute of a sensor as a value in yaml?

It is correct JSON but HA might not parse it as a JSON-object. Try:

{{ (state_attr('sensor.hikconnect_call_status', 'data')|from_json).callStatus }}
1 Like

I’ve had a play and eventually succeeded in the template editor. I think it’s an issue with a mixture of strings and strings-interpreted-as-objects…

{% set a = {"data":"{\t\"apiId\":\t1,\t\"callStatus\":\t2,\t\"verFlag\":\t1,\t\"callerInfo\":\t{\t\t\"buildingNo\":\t1,\t\t\"floorNo\":\t0,\t\t\"zoneNo\":\t1,\t\t\"unitNo\":\t1,\t\t\"devNo\":\t0,\t\t\"devType\":\t1,\t\t\"lockNum\":\t1\t},\t\"rc\":\t1}","meta":{"code":200,"message":"操作成功","moreInfo":null}} %}
{{ ((a.data)|from_json).callStatus }} <-- this gives 2
{{ ((a.data)|from_json).callerInfo.zoneNo }} <-- this gives 1
1 Like

indeed, now its working
how do you guys now this? :slight_smile:

{{ (state_attr('sensor.hikconnect_call_status', 'data')|from_json).callStatus }}

Experience, good documentation and playing in the template editor. It took a few goes, and I spent some time trying to get rid of the tab characters which wasn’t needed. I ended up with more than ten steps to get to the result, and then worked backwards to eliminate unnecessary ones.

@septillion got there first, though :grin:.

1 Like

guys, one more question/issue
so i have a rest sensor, that logs me in each day on the server; to get the sessionid, that works :

this one command gives me the session id in the template editor , that works too , so far so good

{{ state_attr('sensor.hikconnect_login', 'loginSession').sessionId }}

if i use that command now as a header for my next sensor, that nees polling every 5 sec, to get other data , it doesnt work

  - platform: rest
    name: hikconnect_last_calltime
    scan_interval: 5
    resource: "https://apiieu.hik-connect.com/v3/calling/listByUser?leastTime=&msgStatus=-1&pageSize=1"
    headers:
      sessionId: "{{ state_attr('sensor.hikconnect_login', 'loginSession').sessionId }}"
    method: GET
    value_template: "Hikvision Last Calltime"
    json_attributes_path: "$.data[0]"    
    json_attributes:
      - callingTime

i get this error then :
2021-05-02 10:05:20 WARNING (MainThread) [homeassistant.components.rest.sensor] JSON result was not a dictionary or list with 0th element a dictionary

if i make the sessionid static , like below, then the sensor works :

so whats the issue? why cant i use it there as variable? why does it work when i put it static?

  - platform: rest
    name: hikconnect_last_calltime
    scan_interval: 5
    resource: "https://apiieu.hik-connect.com/v3/calling/listByUser?leastTime=&msgStatus=-1&pageSize=1"
    headers:
      #sessionId: "{{ state_attr('sensor.hikconnect_login', 'loginSession').sessionId }}"
      sessionId: eyJhbGciOiJIUzM4NCJ9.eyJhdWQiOiJjZDBmZDgxMjc0YWxxxxxxxx
    method: GET
    value_template: "Hikvision Last Calltime"
    json_attributes_path: "$.data[0]"    
    json_attributes:
      - callingTime

you can only template value_template.

any idea how i can fi this or workaround it? also note , the sessionid is longer then 255 chars, so i need attributes :s

You have to hardcode the sessionId or make a custom integration.

crap, i am not a good developer
the sessionid changes every day, so i cant hardcode it right?

you can, but you’d need to change it every day and restart.

hmm , not an ideal solution :slight_smile:

why aren’t you just using the integration for hikvision?

EDIT: Nevermind, not the same as hik-connect.

yeah, this is indeed verry new, i simulated the hikconnect app with fiddler , and then using the same login sessions and get the status of my video intercom, like last calls , callstatus, door open events…

@petro, i am now using a command line sensor, no issues anymore with templating, one last thing, since you are the template export

  1. How can i use this as a value template? The problem is that if i use it know, it cant be rendered, since the hikconnect_login sensor is not ready yet… approx after 5 sec after a restart of HA
    {{ (state_attr('sensor.hikconnect_callstatus', 'data')|from_json).callStatus }}

  2. Also i receive once on startup this error below, also quite normal, since the template is not ready yet, any idea how i can fix that, so my logbook doesnt contain a warning?


2021-05-03 14:55:51 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'None' has no attribute 'sessionId' when rendering '--header "sessionId: {{ state_attr('sensor.hikconnect_login', 'loginSession').sessionId }}" "https://apiieu.hik-connect.com/v3/devconfig/v1/call/Qxxxxx/status"'
  - platform: command_line
    command: >
      curl
      --header "sessionId: {{ state_attr('sensor.hikconnect_login', 'loginSession').sessionId }}"
      "https://apiieu.hik-connect.com/v3/devconfig/v1/call/Q02xxxx/status"
    name: hikconnect_callstatus
    scan_interval: 5
    value_template: "Callstatus"
    json_attributes:
      - data