extended_openai_conversation icon indicating copy to clipboard operation
extended_openai_conversation copied to clipboard

How to use rest api to get sensor data

Open FoliniC opened this issue 1 year ago • 3 comments

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?

FoliniC avatar Jan 08 '24 10:01 FoliniC

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?

  1. If local, you can access the state of sensor easily just by exposing entities.

  2. 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.

jekalmin avatar Jan 10 '24 08:01 jekalmin

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: ""

FoliniC avatar Jan 10 '24 14:01 FoliniC

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 -%}
      ```

jekalmin avatar Jan 10 '24 15:01 jekalmin

Closing the issue.

jekalmin avatar Nov 12 '24 13:11 jekalmin