Get with Home Assistant API from Google apps script causes error

71 views
Skip to first unread message

emanuele mattiolo

unread,
Sep 4, 2023, 5:50:00 AM9/4/23
to Google Apps Script Community
Hi everyone,
sorry for my english.
I'm trying to query my Home Assistant with API (https://developers.home-assistant.io/docs/api/rest/), pull the value of a sensor and write it to a google sheet. With Postman no problem, so url and token are correct. It doesn't work with google script. I write only the part of code that performs this action with the Home Assistant API.

function fetchHomeAssistantData(
var token = "myapikey"
var options = { "method": "GET", "headers": { "Authorization": "Bearer " + token }, "muteHttpExceptions": true }; 
var response = UrlFetchApp.fetch(url, options); 
var data = response.getContentText(); 
var jsonData = JSON.parse(data); 
Logger.log(options); 
Logger.log(response.getContentText()); 

var state = jsonData.state
var lastChanged = jsonData.last_changed; }

Response: "Error SyntaxError: Unexpected non-whitespace character after JSON at position 3 fetchHomeAssistantData @ HARestApi.gs:15

can anyone help me? thank you
Reply all
Reply to author
Forward
0 new messages