Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get data from API call #220

Open
tunerooster opened this issue Feb 17, 2024 · 9 comments
Open

Can't get data from API call #220

tunerooster opened this issue Feb 17, 2024 · 9 comments

Comments

@tunerooster
Copy link

I am trying to use curl to read a zigbee attribute from a device.

From within Home Assistant, it works. I.e., this service request:

service: zha_toolkit.attr_read
data:
  ieee: switch.tz3210_j4pdtz9v_ts0001_switch
  endpoint: 1
  cluster: 0
  attribute: 3

Returns this response:

zha_toolkit_version: v1.1.8
zigpy_version: 0.60.4
zigpy_rf_version: 0.37.6
ieee_org: switch.tz3210_j4pdtz9v_ts0001_switch
ieee: a4:c1:38:09:a5:0d:19:c9
command: attr_read
command_data: null
start_time: "2024-02-17T01:39:31.705066+00:00"
errors: []
params:
  endpoint_id: 1
  cluster_id: 0
  attr_id: 3
  dir: 0
  tries: 1
  expect_reply: true
  args: []
  read_before_write: true
  read_after_write: true
attr_type: "0x20"
write_is_equal: false
result_read:
  - "3": 1
  - {}
success: true

However when I try to do the same thing via the Home Assistant API, as follows:

curl -X POST -H "$BEARERAUTH" -H "Content-Type: application/json" --data '{"ieee": "switch.tz3210_j4pdtz9v_ts0001_switch", "endpoint": 1, "cluster": 0, "attribute": 3}' http://homeassistant:8123/api/services/zha_toolkit/attr_read

It prints only:

[]

Which means the request succeeded, but there is no data in the response.
I want to get a full json response with the same data as the Home Assistant interactive service request.

Any advice is appreciated!

@mdeweerd
Copy link
Owner

When used in automations, if you want a reply, you need to add expect_reply.
I'ld expect that at best that is possible in the HA webservice as well.

If not, we would need to check the documentation how an integration can impact the web reply.

Other approaches I think of:

@tunerooster
Copy link
Author

tunerooster commented Feb 17, 2024

Thanks for your reply! My main reason for doing this is to determine if the zigbee decice is connected. The device occasionally drops out, and I want to determine (in a script) if that has happened, without changing the state of the device. When I query the device state directly, if replies "on" even if the device is powered off (unplugged). Also, reading from the cache would not likely serve my purpose for the same reason. I will look into the references you suggest...

Where do you put the "expect_reply" in the call to curl? Like this:

curl -H "$BEARERAUTH" -H "Content-Type: application/json" -d '{"ieee": "switch.tz3210_j4pdtz9v_ts0001_switch", "endpoint": 1, "cluster": 0, "attribute": 3, "expect_reply": "true"}' http://homeassistant:8123/api/services/zha_toolkit/attr_read

The above expect_reply did not work. Same "[]" response.

Do you know of any better ways to test the zigbee connection status? Thanks again!

@mdeweerd
Copy link
Owner

You could also look into the webhooks functionnality.

I made a mistake about expect_reply - that's to indicate that the service call should wait for a reply for the device (I did not check again).

The option is actually "response_variable" for use in scripts, so maybe you can set that in your request.

The functionality for the response data was announced here and implemented in zha-toolkit at about that period:
https://www.home-assistant.io/blog/2023/07/05/release-20237/

@mdeweerd
Copy link
Owner

mdeweerd commented Feb 17, 2024

Here are two other approaches:

Read value, write to CSV file

See https://github.com/mdeweerd/zha-toolkit/blob/main/examples/script_read_basic_cluster.yaml for an example where a value is read and written to a CSV file that is put in a file accessible as a URL.

Update entity attributes, get entity attributes

As you can write values read directly to a state, you could update a state on a regular basis and read that state using the W.

Use zha_devices service from zha

See
https://github.com/mdeweerd/zha-toolkit/blob/main/examples/script_use_zha_devices_response.yaml .
You can use that information to help determine the state of your device and report it.

Use the ha_set_state service from zha-toolkit to update a state that you can read through the web service.

@tunerooster
Copy link
Author

tunerooster commented Feb 17, 2024

I tried this, as you suggested, but still no data:

curl -H "$BEARERAUTH" -H "Content-Type: application/json" -d '{"ieee": "switcurl -H "$BEARERAUTH" -H "Content-Type: application/json" -d '{"ieee": "switch.tz3210_j4pdtz9v_ts0001_switch", "endpoint": 1, "cluster": 0, "attribute": 3, "response_variable": "true"}' http://homeassistant:8123/api/services/zha_toolkit/attr_read
ch.tz3210_j4pdtz9v_ts0001_switch", "endpoint": 1, "cluster": 0, "attribute": 3, "response_variable": "true"}' http://homeassistant:8123/api/services/zha_toolkit/attr_read

[]

Is the curl call correct? What should the value of "response_variable" be?

@mdeweerd
Copy link
Owner

It is possible that the HA web interface does not return a value. The value for response_variable is a string here which is ok because the value is the name of the variable to set, however, "true" may not be an accepted name for a variable.

@tunerooster
Copy link
Author

I can't afford to change the state of the device, as any change will trigger/activate the device. (It is a fingerbot which interprets any on/off/toggle of its state to be a "click" and a button is pushed which turns on/off another device.)

Are you saying to use a HA script to call the service and output the data using csvout: "{{ csv }}"? Is "csvout" a zha_toolkit attribute? I'm not quite understanding, even looking at the script_read_basic_cluster.yaml script.

The response_variable sounds the most promising, but I can't see how to use it, even after reading the reference you provided.

I very much appreciate your help!

@mdeweerd
Copy link
Owner

I can't afford to change the state of the device, ...

That's not what I am suggesting - you can create another entity/state with the value that you want to observe.

Are you saying to use a HA script to call the service and output the data using csvout: "{{ csv }}"? Is "csvout" a zha_toolkit attribute?

'csvout' is a parameter to several zha-toolkit services - including the read attribute - it will write the information to the indicated CSV file. "{{ csv }}" is a template indicating to Home Assistant to return the value of 'csv' which is an input/parameter to the example script.

The response_variable sounds the most promising, but I can't see how to use it, even after reading the reference you provided.

Maybe the response is not available in a simple curl request - I guess it is available if you are using a websocket (which means that you have a continuous connection). I shared the python script I developed for another project that could help with that - but it seems to be above your skillset.

@tunerooster
Copy link
Author

tunerooster commented Feb 18, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants