How can i integrate (at least partially) the actions, triggers and variables from BTT to Homeassistant in real time?
ideally it should allow a two way communication -
-
mac to HA: consider the following script
(its arbitrary but accomplishing that would pretty much account for 99% of any of my needs):
make the X position of the cursor on the screen map into a parameter between 0-100 and control the intensity of my smart lights in HA.
-
HA to mac:
allow any of my HA triggers to also kick a trigger in BTT at the same time
is mqtt even the right way to go about this task? maybe webhook or something like that?
please let me know what you think
TIA
Unfortunately I don't have experience with HA (I should probably switch from home bridge to home assistant, but I have been too lazy ;-))
I think easiest would be to use HA's REST API.
This is an example that you could run via BTT's "Run Real Java Script" action. You'd need to adapt the URL and access token.
async function setLightBrightness() {
const url = "http://HOME_ASSISTANT_URL/api/services/light/turn_on";
const token = "YOUR_LONG_LIVED_ACCESS_TOKEN";
const cursorPositionInPercent = await get_number_variable("mouse_pos_percent_x"); // this would return something like 44.5
const payload = {
entity_id: "light.your_light_entity",
brightness: (255/100)*cursorPositionInPercent // Value between 0 and 255
};
try {
const response = await fetch(url, {
method: "POST",
headers: {
"Authorization": `Bearer ${token}`,
"Content-Type": "application/json"
},
body: JSON.stringify(payload)
});
if (!response.ok) {
throw new Error(`Error ${response.status}: ${response.statusText}`);
}
const data = await response.json(); // Optional: read response
console.log("Light updated successfully:", data);
} catch (error) {
console.error("Failed to update light brightness:", error);
}
}