extended_openai_conversation
extended_openai_conversation copied to clipboard
How to use rest api to get sensor data
In the initial prompt I set to use REST api passing the bearer token adding
Do not use MQTT.
Do not use Service sensor.get.
Use REST api to get sensor data. For authorization use bearer token with value: <MyBearerToken>
It calls a non existant generic function
Something went wrong: Service sensor.rest_command not found.
What I want to achieve is to get temperature from a particular sensor, in your docs I see that is possible to use REST .
Tried manually with Postman with url
{{base_url}}/states/sensor.temperatura_corridoio
and it gives me the correct json data
{"entity_id":"sensor.temperatura_corridoio","state":"16.57","attributes":{"source":"1.1.14","unit_of_measurement":"°C","device_class":"temperature","friendly_name":"Temperatura corridoio"},"last_changed":"2024-01-08T10:12:10.938297+00:00","last_updated":"2024-01-08T10:12:10.938297+00:00","context":{"id":"01HKM8XW1T3HAC2WHACT81M85H","parent_id":null,"user_id":null}}
How can I set bearer token? How can I map a particular prompt to use REST function?
Thanks for reporting an issue!
How can I map a particular prompt to use REST function?
You can register a function to gpt, and it automatically decides what data or function to use. There are a few function types (including REST) you can use in this integration.
What I want to achieve is to get temperature from a particular sensor, in your docs I see that is possible to use REST
Are you trying to get sensor from another remote HA or local HA?
-
If local, you can access the state of sensor easily just by exposing entities.
-
If it's remote, you will have to add a rest function like below.
- spec:
name: get_state
description: Get state of entity
parameters:
type: object
properties:
entity_id:
type: string
description: The entity id.
function:
type: rest
resource_template: "https://YOUR_BASE_URL/api/states/{{entity_id}}"
value_template: "{{ value_json.state }}"
headers:
Authorization: "Bearer YOUR_TOKEN"
You have to replace YOUR_BASE_URL
and YOUR_TOKEN
with your data.
Since only spec
part is passed to OpenAI, you don't have to let OpenAI know what url and token are.
Let OpenAI decide what entity_id from what you asked, which enables you to use entity_id
from rest function.
Hi Jekal, it worked!!! I use home assistant from internet (using ngrok) so I use rest. To get all the entities I tried with postman using {{base_url}}/states Is it correct to configure the call like this?
- spec: name: get_states description: Get entity list parameters: type: object function: type: rest resource_template: "https://myid.ngrok-free.app/api/states" value_template: "{{ value_json.states }}" headers: Authorization: ""
Since /api/states
returns a list of entities, you have to iterate over value_json
.
Also, since all entities are returned, maybe you have to limit the number of entities.
The more entities returned, the more costs.
- spec:
name: get_entities
description: Get entity list
parameters:
type: object
properties:
dummy:
type: string
description: Nothing
function:
type: rest
resource_template: "YOUR_BASE_URL/api/states"
headers:
Authorization: "Bearer YOUR_TOKEN"
value_template: >-
```csv
entity_id,name,state
{% for entity in value_json -%}
{% if loop.index < 3 -%}
{{ entity['entity_id'] }},"{{ entity['attributes']['friendly_name'] }}",{{ entity['state'] }}
{% endif -%}
{% endfor -%}
```
Closing the issue.