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

Guys, i have a sensor, there is an attribute that i need to make a new sensor
How can i use that attribute in a yaml config? is that possible somehow?

First example on this page:

thnx for answering , but thats not whai mean
i see this in a sensor as an attribute, how can i use that callingid , the long string, i need that to create my new sensor in the yaml file
its always different if i restart HA, so no static value

Why do you need a new sensor? Can’t you just use use that attribute? Otherwise, you can make a template sensor with "{{ state_attr('sensor.hikconnect_call_status', 'data')[0].callingId }}"

Yeah, i need a new sensor based on another sensor attribute values. :slight_smile:

Ok, i can use that, i gonna try it

Some stuff for next week :slight_smile:

to save you the trouble…

sensor:
- platform: template
  sensors:
    hikconnect_calling_id:
      unique_id: hikconnect_calling_id
      value_template: >
        {%- set data = state_attr('sensor.hikconnect_call_status', 'data') %}
        {%- if data is iterable and data is not string %}
          {{ data[0]['callingId'] }}
        {%- endif %}
      availability_template: >
        {%- set data = state_attr('sensor.hikconnect_call_status', 'data') %}
        {{ data is iterable data and is not string and data | length > 0 }}
1 Like

Love you guys :slight_smile:

@petro , 1 more :slight_smile:

i have this data, i only need the “callStatus” as an attribute, how do need to define that in the json attributes path in my sensor below?

  - platform: rest
    name: hikconnect_call_status
    scan_interval: 2
    resource: "xxxx"
    headers:
      sessionId: xxxx
    method: GET
    value_template: "Callstatus" 
    json_attributes_path: "$.data" 
    json_attributes:
      - callStatus

I’d have to see the resource’s raw data, I don’t know what that JSON viewer is doing with the data

this what you need?

{"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}}

what you have should work if that’s the raw data from the resource

hmm i doesnt, there is no info in my overview
if i do it like this , i have too much info offcourse

    #json_attributes_path: "$.data" 
    json_attributes:
      - data

or can i maybe make a new template sensor, based on this output to grab only callstatus?

@petro , that json keeps bugging me, maybe i leave it like it is, i have the below text now as an attribute

Callstatus	data: {	"apiId":	1,	"callStatus":	1,	"verFlag":	1,	"callerInfo":	{		"buildingNo":	0,		"floorNo":	0,		"zoneNo":	0,		"unitNo":	0,		"devNo":	0,		"devType":	-1,		"lockNum":	0	},	"rc":	1}
friendly_name: hikconnect_call_status

the relevant info i need is only this part of text : ‘“callstatus”: 1’
the 1 can change to another value offourse

can i make a make a template automation based on that text ?
i can do it like this as an example , not sure if below works

{{ state_attr('sensor.hikconnect_call_status[90:91]', 'data') == '1' }}

but i am also not 100% sure if its always on that same fixed place … would be better if i look for the word “callstatus” and get the value after it … somekind of find/search command?

You can grab that with

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

Or if that’s the only part you are interested in, change the template of hikconnect_call_status.

hmm, that doesnt give me any value in the template tester :

What is the response of {{ state_attr('sensor.hikconnect_call_status', 'data')}}? In text instead of image please. Might be that the attr isn’t parsed as json-object by default.

here are both :slight_smile:
isnt that correct JSON? mabye thats why i am having issues creating this sensor , i tried before with json path and atributes, but that was not working also, see some posts above

{
  "apiId": 1,
  "callStatus": 1,
  "verFlag": 1,
  "callerInfo": {
    "buildingNo": 0,
    "floorNo": 0,
    "zoneNo": 0,
    "unitNo": 0,
    "devNo": 0,
    "devType": -1,
    "lockNum": 0
  },
  "rc": 1
}