Hello,
I’m using OpenHAB along with Grafana + InfluxDB, all on the same Raspberry Pi. I’d like to be able to push one of the buttons in my openHAB interface, but from the Grafana dashboard instead. I’m treading in unknown waters, so here’s a lot of newbie question.
Grafana allows html and javascript in what it calls text panels, so I was hoping to be able to embed some html and javascript in the Grafana panel to for the pushbutton functionality. When I inspect the OpenHAB webpage for that button, I see javascript like this. It’s the button that turns off all my lights, and it only sends the OFF command.
<a href="javascript:void(0)" onclick="ChangeState('CMD?itm_all_off_fnt=OFF')" class="iButton iBAction" style="">Off</a>
I know I can access all items via REST API, so for that item:
http://192.168.2.24:8080/rest/items/itm_all_off_fnt
{
“link”: “http://192.168.2.24:8080/rest/items/itm_all_off_fnt”,
“state”: “OFF”,
“type”: “Switch”,
“name”: “itm_all_off_fnt”,
“label”: “All Off”,
“tags”: [],
“groupNames”: []
}
I think my javascript needs to look something like this:
<script type="text/javascript" language="javascript">
function UserAction() {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://192.168.2.24:8080/rest/items/itm_all_off_fnt", true);
xhttp.setRequestHeader("Content-type", "application/json");
xhttp.send();
var response = JSON.parse(xhttp.responseText);
}
</script>
<button type="submit" onclick="UserAction()">Clickme</button>
This makes the “Clickme” button show up in Grafana, but clicking it doesn’t seem to do anything. I don’t see any events happening in the Openhab log when button is pressed. I don’t think I’m composing the POST call correctly.
Any hints would be appreciated.