Send HTTP request using a button

Hi Bill,
your code above works fine with the simulator (when I press the button in the simulator, the light switches on and off), but it doesen’t work in my project on the iPad.
I have no idea what is wrong. The simulator has a log in which I can see that the commands are executed.

Did you have any idea?

Thanks,
Kalle

Sorry, it was my fault - I have solved it by my self.
The button was not in front and in this way not clickable.

Kalle

Hello Bill,

I am new to migrating from Imperihome to HR. I adapted this example to communicate with my Vera but it doesn’t work for me. I always receive an error message “No public methods with the specified arguments were found.”
This is the HTTP request I am making :

http.get(“http://MY_VERA_IP:3480/data_request?id=variableget&DeviceNum=227&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature”);

Device 227 is a TemperatureSensor1 device.
If I try this request in a web browser it’s OK but not in HR.

Could you tell me if my code is not correct or if I forgot something ?

Thanks.

I don’t think Bill is really active in this forum anymore.
You can use the Vera plugin from HR, which is much easier than http request via this plugin.

1 Like

I agree that you should investigate using the Vera plugin, like KalleVC said.

However, if you want help with your code, you are going to have to tell us how it doesn’t work, and show us the relevant code.

Thank you for your responses.
In fact I have problems with the Vera plugin and not having found a solution on this side I tried using an HTTP request.
I explain this in the thread I just created.

https://community.thehomeremote.com/t/how-to-get-temperature-from-a-vera-temperaturesensor-device/4573

However, I would need to understand why the HTTP request does not succeed because my home automation installation includes several connected objects which I access via HTTP requests to the integrated web servers.

About the code I took the HTTPExample example and I just modified the requests in the plugin. Here is the modified plugin

HTTPExample.plugin (1.6 KB)

I forgot to tell you the status of the device on the Vera.

Sorry my previous message does not correspond to this thread

Here is a customized plugin code that you can try out - the descriptions in the code are in French.
This code was created with the help of artificial intelligence - maybe that will help you. But as already mentioned, it’s best to use the Vera plugin.
No guarantee that it will work.

plugin.Name = "VeraRequests";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 60; // Interrogation toutes les 60 secondes
plugin.DefaultSettings = { "VeraHost": "Your_Vera_IP", "VeraPort": "3480" };

var http = new HTTPClient();

var baseUrl;

function onChangeRequest(device, attribute, value) {
    if (attribute != "TemperatureRequest")
        throw "Attribut invalide";

    var temperature = getTemperature(value);
    console.log("Température actuelle pour l'appareil " + value + ": " + temperature);

    var mediaCenter = plugin.Devices["1"];
    if (mediaCenter != null) {
        mediaCenter.Attributes["CurrentTemperature"] = temperature;
    }
}

function onConnect() {
    baseUrl = "http://" + plugin.Settings["VeraHost"] + ":" + plugin.Settings["VeraPort"] + "/data_request";

    var mediaCenter = plugin.Devices["1"];
    if (mediaCenter != null) {
        mediaCenter.SupportedMediaCommands = ["TemperatureRequest"];
    }
}

function onDisconnect() {
}

function onPoll() {
    // Exemple d'appel pour plusieurs appareils lors de l'interrogation
    var devices = [227, 228, 229]; // Ajoutez ici vos numéros d'appareils
    devices.forEach(function(deviceNum) {
        var temperature = getTemperature(deviceNum);
        console.log("Température actuelle pour l'appareil " + deviceNum + ": " + temperature);

        var mediaCenter = plugin.Devices["1"];
        if (mediaCenter != null) {
            mediaCenter.Attributes["CurrentTemperature_" + deviceNum] = temperature;
        }
    });
}

function onSynchronizeDevices() {
    var mediaCenter = new Device();
    mediaCenter.Id = "1";
    mediaCenter.DisplayName = "VeraRequests";
    mediaCenter.Capabilities = ["TemperatureControl"];
    mediaCenter.Attributes = {};
    plugin.Devices[mediaCenter.Id] = mediaCenter;
}

function getTemperature(deviceNum) {
    var url = baseUrl + "?id=variableget&DeviceNum=" + deviceNum + "&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature";
    try {
        var response = http.get(url);
        console.log("Réponse de la température: " + response);
        return response;
    } catch (error) {
        console.error("Échec de la requête HTTP: " + error);
    }
}

Hello Kalle,

I had to make some minor adaptations in your code to make it work but it works!

I ended up understanding this morning that my modified ‘HTTPExample.plugin’ example was working well and that the error message I was getting was not related to the request but to the display of the response in the log.
It was necessary to write ‘console.log(response.data);’ in place of ‘console.log(response);’. Beginner’s mistake…

Here is the result obtained with your code. It’s perfect.

Now that I have the Vera’s temperature, what do you think would be the easiest way to display it on my label?
I’m not sure I know how to do it at my beginner level.

Thank you very much for your help.

You can try this PluginCode, The plugin code store the temp in the “mediaCenter.Attributes” Température.

plugin.Name = "VeraRequests";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 60; // Interrogation toutes les 60 secondes
plugin.DefaultSettings = { "VeraHost": "Your_Vera_IP", "VeraPort": "3480" };

var http = new HTTPClient();

var baseUrl;

function onChangeRequest(device, attribute, value) {
    if (attribute != "TemperatureRequest")
        throw "Attribut invalide";

    var temperature = getTemperature(value);
    console.log("Température actuelle pour l'appareil " + value + ": " + temperature);

    var mediaCenter = plugin.Devices["1"];
    if (mediaCenter != null) {
        mediaCenter.Attributes["CurrentTemperature"] = temperature;
    }
}

function onConnect() {
    baseUrl = "http://" + plugin.Settings["VeraHost"] + ":" + plugin.Settings["VeraPort"] + "/data_request";

    var mediaCenter = plugin.Devices["1"];
    if (mediaCenter != null) {
        mediaCenter.SupportedMediaCommands = ["TemperatureRequest"];
    }
}

function onDisconnect() {
}

function onPoll() {
    // Exemple d'appel pour plusieurs appareils lors de l'interrogation
    var devices = [227, 228, 229]; // Ajoutez ici vos numéros d'appareils
    devices.forEach(function(deviceNum) {
        var temperature = getTemperature(deviceNum);
        console.log("Température actuelle pour l'appareil " + deviceNum + ": " + temperature);

        var mediaCenter = plugin.Devices["1"];
        if (mediaCenter != null) {
            mediaCenter.Attributes["CurrentTemperature_" + deviceNum] = temperature;
        }
    });
}

function onSynchronizeDevices() {
    var mediaCenter = new Device();
    mediaCenter.Id = "1";
    mediaCenter.DisplayName = "VeraRequests";
    mediaCenter.Capabilities = ["TemperatureControl"];
    mediaCenter.Attributes = { "Température": {} }; // Ajouter l'attribut "Température"
    plugin.Devices[mediaCenter.Id] = mediaCenter;
}

function getTemperature(deviceNum) {
    var url = baseUrl + "?id=variableget&DeviceNum=" + deviceNum + "&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature";
    try {
        var response = http.get(url);
        console.log("Réponse de la température: " + response);
        return response;
    } catch (error) {
        console.error("Échec de la requête HTTP: " + error);
    }
}

Hello Kalle,

Sorry for my incompetence but I can’t get the plugin to work. Above all, I would like to understand the code and the definition of the attribute raises questions for me.
In the 'OnChangeRequest function you set the attribute “CurrentTemperature”
mediaCenter.Attributes[“CurrentTemperature”] = temperature;
but in the ‘OnPoll’ function you set
mediaCenter.Attributes[“CurrentTemperature_” + deviceNum] = temperature;
and finally in the ‘onSynchronizeDevices’ function you set
mediaCenter.Attributes = { “Température”: {} };

I admit to being a little lost.

Another thing about the ‘OnPoll’ function. In the device loop you write
var mediaCenter = plugin.Devices[“1”];

Shouldn’t we rather write something like

var id = 0;
devices.forEach(function(deviceNum) {
    id += 1;
    var mediaCenter = plugin.Devices[id];
    ...
    }

As an example I defined two devices this way. Is this correct?

And here is the binding definition for my text label.

Something else with ‘baseUrl’ definition in the ‘OnConnect’ function.
I clearly defined
plugin.DefaultSettings = { “VeraHost”: “192.168.xx.xx”, “VeraPort”: “3480” };

in the beginning of the plugin but
baseUrl = “http://” + plugin.Settings[“VeraHost”] + “:” + plugin.Settings[“VeraPort”] + “/data_request”;
returns
http://null:null/data_request’

Hi Max,
the code is AI generated by Bing CoPilot. The most code which I generate by AI does the job, but I had no chance to test this code.

OK, let us start from the scratch - this is now a working Plugin Code for your device 227 where the value can be displayed in a label. Can you try this code?

// Plugin VeraUI5 pour HomeRemote V 1.0
// Récupérer les données de température

plugin.Name = "VeraUI5";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.DefaultSettings = { 
    "IpAddress": "192.168.68.16",
    "Port": "3480",
    "PollingInterval": "15000"
};

var http = new HTTPClient();

function onChangeRequest(device, attribute, value) {
    // Vous pouvez ajouter du code ici pour répondre aux demandes de changement
}

function onConnect() {
    plugin.PollingInterval = parseInt(plugin.Settings["PollingInterval"], 10);
}

function onDisconnect() {
    // Vous pouvez ajouter du code ici pour répondre aux événements de déconnexion
}

function onPoll() {
    var ipAddress = plugin.Settings["IpAddress"];
    var port = plugin.Settings["Port"];
    var deviceId = 227; // ID de l'appareil

    var response = http.get("http://" + ipAddress + ":" + port + "/data_request?id=variableget&DeviceNum=" + deviceId + "&serviceId=urn:upnp-org:serviceId:TemperatureSensor1&Variable=CurrentTemperature");
    if(response.status == 200) {
        var data;
        if(typeof response.data === 'object') {
            data = response.data;
        } else {
            data = JSON.parse(response.data);
        }
        var device = plugin.Devices[deviceId];
        device.Temperature = data;
    }
}

function onSynchronizeDevices() {
    var deviceId = 227; // ID de l'appareil
    var VeraUI5 = new Device();
    VeraUI5.Id = deviceId;
    VeraUI5.DisplayName = "Capteur de Température";
    VeraUI5.Capabilities = [];
    VeraUI5.Attributes = ["Température"];
    plugin.Devices[VeraUI5.Id] = VeraUI5;
}

Hi Kalle,

I tried your code but without success.
I think the problem is linked to the problem I talked to you about ‘baseUrl’ at the end of my last message.
In the ‘onPoll’ function, if I log ‘ipAddress’ and ‘port’ the result is
ipAddress = null
port = null
I assigned the ip address and port directly in the url and the HTTP request works fine.

But the same thing happens when we assign the temperature value in the plugin attribute.

    device.Temperature = data;
    console.log("Data = " + data);
    console.log("device.Temperature = " + device.Temperature);

returns

Data = 22.4
device.Temperature = null

So the temperature is not stored correctly in the device and this probably explains why the binding of the label text does not work.
I have tried almost everything for two days and I have the impression that this is the problem that is blocking everything.

Another weird thing for me is that the ‘onSynchronizeDevices’ function is not called when I run the simulator but that might be the normal behavior.

I haven’t understood everything yet but I have just resolved the problem of memorizing the temperature in the device. I just noticed that I had added a call to the ‘onSynchronizeDevices’ function in the ‘onConnect’ function following my previous remark. By deleting this call I recover the temperature placed in the device

Data = 22.3
device.Temperature = 22.3

On the other hand, the problem persists regarding the plugin settings (PollingInterval, port, …).

I forgot to tell you that the ‘device’ variable initialization doesn’t seem right.
I replaced

var device = plugin.Devices[deviceId];

with

var device = plugin.Devices[“1”];

because ‘deviceId’ = 227 and there is only one VeraUI5 device in the project.

With all this I finally managed to display the temperature in my label :+1:

I can finally go get some sleep…

Good to hear that you were able to solve the problem.

Hi Kalle,

It’s a lot thanks to you. Thank you very much for your assistance and advice.
I will now be able to create my own dashboard to manage the Vera’s devices.
Before closing the subject, however, I would like to clarify the problem of the plugin settings and also understand the principle of synchronization.

The documentation states

I understand but:

  • Is it really necessary to implement this function?
  • What would be the use of synchronizing the plugin?
  • Where is the control menu to start synchronization in the application and in the simulator?

If you still have a little time to give me, your experience would be valuable to me.

I think this function is the same as when you click on “Synconize Device” with the right mouse button in Devices.
But I could be wrong and it is meant to load all current data into the plugin when you start the remote control.
To summarize: The onSynchronizeDevices function ensures that a new device with certain capabilities and attributes is created and added to the plugin’s list of devices.

image

Thank you for these details.
In my case for this to work correctly the ‘onSynchronizeDevices’ function of my VeraUI5 plugin would have to ask the VeraLite in order to retrieve the new devices and to integrate them into the plugin. This is probably what should be done during the Vera plugin synchronization.
It would also be necessary to take into account any deleted devices on the VeraLite to remove them in the plugin.

Otherwise an idea about the problem with the functioning of the plugin settings?