Exposing OpenHAB push button for use in another front end

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.

Hey mate,

you just want to switch the button ON or OFF?

you have to specify the request body within the xhttp.send(); method.
So for switching OFF you do xhttp.send(“OFF”); and the other way round.

If you want to check if your javascript gets fired at all you can put an alert(“test js”); within your UserAction() function.

@Simsal, thanks! My function isn’t running, your tip about putting alert() in the body of the function helped me troubleshoot.

@Eric
You changed the thread title as solve but did provide a solution so I changed it back.
Could you share what you did, please? And tick the solution mark.