hi, good evening, im a noob i m not good enough writing code and im trying to acomplish something i have a sqlite db on HA, i create a service using pyscript so it can query info from it and update it too, i have i test it and works flawless, i have use it with the notification system etc,
import sqlite3
import requests
from homeassistant.const import EVENT_CALL_SERVICE
@service
def get_sensor_web(search_term):
sensorweb_db_path = '/config/testwebdb.db'
connection = sqlite3.connect(sensorweb_db_path)
cursor = connection.cursor()
cursor.execute("SELECT (location || ' y ' || sensor_value ) as searchTerm FROM websensor WHERE location LIKE ? LIMIT 1", ('%' + search_term + '%',))
results = cursor.fetchall()
cursor.close()
connection.close()
hass.bus.fire("get_data_address_event", {"wow": results})
print(results)
response = requests.post(url, headers=headers, json=data)
print(response.json())
return results if results else None
so now im trying to make a web form that call that service using rest api, it almos work… i can make the web form execute the script via api but it doent showme any responce/ result , but in the event lisen i can see the result so i dont know what im missing
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Search Form</title>
</head>
<body>
<h1>Search Data</h1>
<form id="dataForm">
<label for="search_term">Search Term:</label>
<input type="text" id="search_term" name="search_term" required>
<button type="submit">Submit</button>
</form>
<div id="result"></div>
<script>
document.getElementById('dataForm').addEventListener('submit', function(e) {
e.preventDefault(); // Prevent the default form submission
const searchTerm = document.getElementById('search_term').value;
fetch('/api/services/pyscript/get_sensor_web', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer MYTOKEN'
},
body: JSON.stringify({ search_term: searchTerm })
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.then(data => {
console.log(data); // Log the response for debugging
document.getElementById('result').innerText = JSON.stringify(data, null, 2); // Display the result
})
.catch(error => {
console.error('Error:', error);
document.getElementById('result').innerText = 'Error: ' + error.message;
});
});
</script>
</body>
</html>
i dont know whats wrong when i query it only return to me but in the event listener i see the query result/response/value, please helpme