Page 1 of 1

Make Lua script only runs when device has changed

Posted: Saturday 18 March 2017 9:33
by curious
I created a new hardware device named Buienradar and a dummy device with six virtual sensors named "Buienradar dummies"
When I run next Lua script all virtual sensors are updated correct (One of the virtual sensors is named "Buienradar Wind".

Code: Select all




local idxt = 365 --idx of the virtual temperature sensor you need to change this to your own Device IDx
local idxh = 366 --idx of the virtual humidity sensor you need to change this to your own Device IDx
local idxi = 367 --idx of the virtual pressure sensor you need to change this to your own Device IDx
local idxj = 368
local idxk = 369
local idxl = 370




commandArray = {}



sWindDirectionDegrees, sWindDirection, sWindSpeed, sWindGust, sWindTemperature, sWindFeel = otherdevices_svalues['Buienradar - Wind']:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
 
sWindDirectionDegrees = tonumber(sWindDirectionDegrees);
sWindDirection = (sWindDirection);
sWindSpeed = tonumber(sWindSpeed);
sWindGust = tonumber(sWindGust);
sWindTemperature = tonumber(sWindTemperature);
sWindFeel = tonumber(sWindFeel);

print("Windmeter: Winddirection (in degrees) is: " .. sWindDirectionDegrees .. " ");
print("Windmeter: Winddirection is: " .. sWindDirection .. " ");
print("Windmeter: Windspeed is: " .. sWindSpeed .. " ");
print("Windmeter: Windgust is: " .. sWindGust .. " ");
print("Windmeter: Windtemperature is: " .. sWindTemperature .. " ");
print("Windmeter: Windfeel is: " .. sWindFeel .. " ");




commandArray[1] = {['UpdateDevice'] = idxt .. '|0|' .. sWindDirectionDegrees}
commandArray[2] = {['UpdateDevice'] = idxh .. '|0|' .. sWindDirection}
commandArray[3] = {['UpdateDevice'] = idxi .. '|0|' .. sWindSpeed}
commandArray[4] = {['UpdateDevice'] = idxj .. '|0|' .. sWindGust}
commandArray[5] = {['UpdateDevice'] = idxk .. '|0|' .. sWindTemperature}
commandArray[6] = {['UpdateDevice'] = idxl .. '|0|' .. sWindFeel}



return commandArray 

Now I would like to run the script only when the "Buienradar" has changed

For that I changed the code to this, but then get an error :
2017-03-18 09:14:00.359 Error: EventSystem: in Buienradar_wind: [string "--Windmeter data: ..."]:17: attempt to index global 'devicechanged' (a nil value)


Code: Select all

--Windmeter data:


local sensoren= 'Buienradar'
local idxt = 365 --idx of the virtual temperature sensor you need to change this to your own Device IDx
local idxh = 366 --idx of the virtual humidity sensor you need to change this to your own Device IDx
local idxi = 367 --idx of the virtual pressure sensor you need to change this to your own Device IDx
local idxj = 368
local idxk = 369
local idxl = 370




commandArray = {}

if devicechanged[sensoren] then

sWindDirectionDegrees, sWindDirection, sWindSpeed, sWindGust, sWindTemperature, sWindFeel = otherdevices_svalues['Buienradar - Wind']:match("([^;]+);([^;]+);([^;]+);([^;]+);([^;]+);([^;]+)")
 
sWindDirectionDegrees = tonumber(sWindDirectionDegrees);
sWindDirection = (sWindDirection);
sWindSpeed = tonumber(sWindSpeed);
sWindGust = tonumber(sWindGust);
sWindTemperature = tonumber(sWindTemperature);
sWindFeel = tonumber(sWindFeel);

print("Windmeter: Winddirection (in degrees) is: " .. sWindDirectionDegrees .. " ");
print("Windmeter: Winddirection is: " .. sWindDirection .. " ");
print("Windmeter: Windspeed is: " .. sWindSpeed .. " ");
print("Windmeter: Windgust is: " .. sWindGust .. " ");
print("Windmeter: Windtemperature is: " .. sWindTemperature .. " ");
print("Windmeter: Windfeel is: " .. sWindFeel .. " ");




commandArray[1] = {['UpdateDevice'] = idxt .. '|0|' .. sWindDirectionDegrees}
commandArray[2] = {['UpdateDevice'] = idxh .. '|0|' .. sWindDirection}
commandArray[3] = {['UpdateDevice'] = idxi .. '|0|' .. sWindSpeed}
commandArray[4] = {['UpdateDevice'] = idxj .. '|0|' .. sWindGust}
commandArray[5] = {['UpdateDevice'] = idxk .. '|0|' .. sWindTemperature}
commandArray[6] = {['UpdateDevice'] = idxl .. '|0|' .. sWindFeel}

end

return commandArray 
I tried several optins for the device name (with and without brackets and or apostrophes, but get everytime this error.
Does someone know what is wrong ?

Re: Make Lua script only runs when device has changed

Posted: Saturday 18 March 2017 10:06
by jvdz
Is this an LUA script made in the internal editor? if so change "All" to "Devices" in the dropdownbox under LUA.
This error occurs when an Time event is triggered, which seem to be the case looking at the time of the error.

Jos

Re: Make Lua script only runs when device has changed

Posted: Saturday 18 March 2017 20:36
by curious
I changed it to Device. Now the error doesn't show up, but changes are not noticed either.
So the virtual devices are not updated.

Re: Make Lua script only runs when device has changed

Posted: Saturday 18 March 2017 21:26
by jvdz
So what is the exact name of the device that changes when the update needs to happen?

Jos

Re: Make Lua script only runs when device has changed

Posted: Sunday 19 March 2017 11:16
by curious
The name of device = Buienradar
Buienradar.JPG
Buienradar.JPG (25.01 KiB) Viewed 2735 times

Re: Make Lua script only runs when device has changed

Posted: Sunday 19 March 2017 12:08
by jvdz
This is the Hardware page ...right?
How does it look under the Devices page?
You can see the actual devicenames and their current content.

Jos

Re: Make Lua script only runs when device has changed

Posted: Sunday 19 March 2017 14:31
by curious
The device that was created by installing the plugin is called "Buienradar - Wind"

[img]
Buienradar2.JPG
Buienradar2.JPG (43.49 KiB) Viewed 2719 times
[/img]

Re: Make Lua script only runs when device has changed

Posted: Sunday 19 March 2017 14:43
by jvdz
So guess you need to change this line to the appropriate device name like:

Code: Select all

local sensoren= 'Buienradar - Wind'
EDIT: When you want to be sure about the device names that change you could add this code (from the event wiki) temporary to your script to log them to the domoticz.log file:

Code: Select all

commandArray = {}
 
-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
    print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
end
 
return commandArray
Jos