Send HTTP request using a button

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?

Can you post your plugin code, so that I can test it?

I’m not sure if this also works in the UI5, but with this command you can retrieve all available devices of the Vera
http://your_vera_ip:3480/data_request?id=invoke

If you put it in your browser, you will see all vera devices

VeraUI5.plugin (5.2 KB)

The ‘invoke’ request works perfectly on the Veralite. This will be very useful to me

Hi Max,
in this code I have removed the hard coding baseURL. At the first start it has my Vera IP - you can go in the plugin settings and change it to your IP. There are also settings for the port and polling. In my case the settings work. The code has know a function to get sure the settings are setup before the onConnect.
Please try it and let me know if it works.

// Plugin VeraUI5 pour HomeRemote V 1.0
// Exemple de base pour récupérer les données de température et d'humidité de la salle de bain

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": "5000"
};

var http = new HTTPClient();
var baseUrl;

function initializeSettings() {
    console.log("initializeSettings");
    
    // Vérifier si les paramètres ont été chargés
    if (!plugin.Settings["IpAddress"] || !plugin.Settings["Port"] || !plugin.Settings["PollingInterval"]) {
        console.error("Paramètres non chargés correctement");
        return false;
    }
    
    // Initialiser les paramètres
    plugin.PollingInterval = parseInt(plugin.Settings["PollingInterval"], 10) || 5000;
    var ipAddress = plugin.Settings["IpAddress"];
    var port = plugin.Settings["Port"];
    baseUrl = "http://" + ipAddress + ":" + port + "/data_request";
    
    console.log("Paramètres initialisés avec succès");
    return true;
}

function onChangeRequest(device, attribute, value) {
}

function onConnect() {
    console.log("onConnect");
    
    // Vérifier l'initialisation des paramètres
    if (!initializeSettings()) {
        console.error("Connexion impossible car les paramètres n'ont pas été initialisés correctement");
        return;
    }
    
    console.log("PollingInterval = " + plugin.PollingInterval);
    console.log(baseUrl);
}

function onDisconnect() {
}

function onPoll() {
    console.log("onPoll");
    
    // Mise à jour de tous les appareils Vera.
    // Limité aux capteurs d'humidité et de température de la salle de bain dans cet exemple.
    var devices = [227, 228]; // La sonde Oregon Scientific THX122NR-01 a deux capteurs considérés comme deux appareils distincts dans la VeraLite
    devices.forEach(function(deviceNum, index) {
        console.log("mise à jour de l'appareil " + deviceNum);
        console.log("Récupération de la température auprès de la Vera");
        var temperature = getTemperature(deviceNum);
        console.log("Température actuelle pour l'appareil " + deviceNum + ": " + temperature);
        console.log("Récupération de l'humidité auprès de la Vera");
        var humidity = getHumidity(deviceNum);
        console.log("Humidité actuelle pour l'appareil " + deviceNum + ": " + humidity);
        console.log("Récupération du niveau de la batterie auprès de la Vera");
        var batLevel = getBatteryLevel(deviceNum);
        console.log("Niveau de la batterie actuel pour l'appareil " + deviceNum + ": " + batLevel);
        var device = plugin.Devices[index + 1]; // En supposant que les ID des appareils commencent à 1
        if (device != null) {
            device.Temperature = temperature;
            console.log("device.Temperature = " + device.Temperature);
            device.Humidity = humidity;
            console.log("device.Humidity = " + device.Humidity);
            device.BatteryLevel = batLevel;
            console.log("device.BatteryLevel = " + device.BatteryLevel);
        } else {
            console.log("device is null");
        }
    });
}

function onSynchronizeDevices() {
    console.log("onSynchronizeDevices");
    
    var VeraUI5 = new Device();
    VeraUI5.Id = "1";
    VeraUI5.DisplayName = "Capteur de Température Sdb";
    VeraUI5.Capabilities = [];
    VeraUI5.Attributes = ["Temperature","Humidity","BatteryLevel"]; 
    plugin.Devices[VeraUI5.Id] = VeraUI5;
}

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

function getHumidity(deviceNum) {
    var url = baseUrl + "?id=variableget&DeviceNum=" + 228 + "&serviceId=urn:micasaverde-com:serviceId:HumiditySensor1&Variable=CurrentLevel";
    try {
        var response = http.get(url);
        if (response.status == 200) {
           var data;
           if (typeof response.data === 'object') {
              data = response.data;
           } else {
              data = JSON.parse(response.data);
           }
        console.log("Réponse de la requête: " + data);
        return data;
        }
    } catch (error) {
        console.error("Échec de la requête HTTP: " + error);
    }
}

function getBatteryLevel(deviceNum) {
    var url = baseUrl + "?id=variableget&DeviceNum=" + deviceNum + "&serviceId=urn:micasaverde-com:serviceId:HaDevice1&Variable=BatteryLevel";
    try {
        var response = http.get(url);
        if (response.status == 200) {
           var data;
           if (typeof response.data === 'object') {
              data = response.data;
           } else {
              data = JSON.parse(response.data);
           }
        console.log("Réponse de la requête: " + data);
        return data;
        }
    } catch (error) {
        console.error("Échec de la requête HTTP: " + error);
    }
}

image

But the ‘invoke’ request only returns devices and not scenes.

I tried your modification but the settings still don’t work.

image

Weird thing. The plugin definition panel does not contain settings unlike yours.

Are there any settings to set in Home Remote Designer? I didn’t find anything like that.

One thing I have noticed in these code samples is that there is no call to “encodeURI()”. URL data strings that contain certain characters need to have those certain chaaracters replaced with the appropriate escape characters. You may have gotten by with not doing it for now, but eventually not encoding the URL will cause issues.

It is simple syntax:

url = encodeURI(url);

The decoding of the URL is normally automatically done be the receiving system.

I finally figured out how to declare the plugin settings.
It works fine now.

Now that I get the temperature, humidity and battery level, I will try to configure the structure of a ‘TemperatureSensor’ to display them.

I also tried to use ‘encoderURI’ but I get the error

image

Sorry David, I didn’t realize that it was you who advised me to encode the URLs.
Thanks. You are absolutely right. However, I don’t understand why the function is not defined in my project.

Hi Max,
I’m not sure if it is a typo - your error show “encoreURl”
Shouldn’t it be encodeURI?

KalleVC is right, you do have to spell it correctly :grinning: Also remember that Javascript is case sensitive.

If you want to see an example of encodeURI in use in a THR plugin, my “Simple Tasker Plugin” uses that call.

Yes of course. I didn’t even see this error. I must have been very tired yesterday :face_with_peeking_eye:. Thank you.

I’m making progress. I can now display the data retrieved from Vera in a standard tile.

As I progress I discover that HR is really powerful and very well thought out. I just need to take my time and go step by step to master it.
I still have a lot of time to spend designing my remote control but it’s very interesting and informative.

Regarding synchronization with Vera, there is all the information necessary for initializing devices and scenes in the JSon response of the ‘id=sdata’ request, which is a good news.

Thanks again Kalle and David for your help. :+1:
I certainly still have questions and problems to solve but I hope I won’t have to bother you too much. :smirk:

1 Like

Good job Max :+1:
We are here if you need help :wink:

I have made good progress in my migration project from ImperiHome to HR. Synchronization with VeraLite works well now and I can display all the values ​​in groups of tiles.

I’m currently stuck on optimizing the updating of devices in the plugin onPoll() function. In fact, I loop through all the plugin devices to update them, but it is not efficient. I would like to update only the devices visible on the screen. The devices are grouped into groups (lighting, temperatures, etc.) and there is always only one group visible on the screen.
This would drastically reduce the latency time during the update.
Does anyone know a way to achieve this?

You could checkout the HasSubscribers function: Plugin "HasSubscribers" Function

Hello RedGrant,
This is exactly what I was looking for. I’ll try this as soon as possible.
Thanks