Make Lua script only runs when device has changed

Moderator: leecollings

Post Reply
curious
Posts: 132
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Make Lua script only runs when device has changed

Post 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 ?
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Make Lua script only runs when device has changed

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
curious
Posts: 132
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Re: Make Lua script only runs when device has changed

Post 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.
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Make Lua script only runs when device has changed

Post by jvdz »

So what is the exact name of the device that changes when the update needs to happen?

Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
curious
Posts: 132
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Re: Make Lua script only runs when device has changed

Post by curious »

The name of device = Buienradar
Buienradar.JPG
Buienradar.JPG (25.01 KiB) Viewed 2741 times
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Make Lua script only runs when device has changed

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
curious
Posts: 132
Joined: Saturday 02 April 2016 19:38
Target OS: -
Domoticz version:
Contact:

Re: Make Lua script only runs when device has changed

Post by curious »

The device that was created by installing the plugin is called "Buienradar - Wind"

[img]
Buienradar2.JPG
Buienradar2.JPG (43.49 KiB) Viewed 2725 times
[/img]
User avatar
jvdz
Posts: 2334
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: Make Lua script only runs when device has changed

Post 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
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest