Page 1 of 1
if statment not working lua
Posted: Wednesday 22 May 2019 22:11
by gertvanwijk1
hi can anyone explaine why the if statement if waarde == 280 then wind = "N" is not working
we get the value of the device but the update(wind) in idx 8 is always "W"
commandArray = {}
wind="W"
-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
if (deviceName=='windmeter') then
waardes = tostring(deviceValue)
end
if (deviceName=='wind') then
waarde = tonumber(deviceValue)
if waarde == 280 then wind = "N"
else wind = "NW"
end
end
print(waardes)
print(waarde)
commandArray['UpdateDevice']='8|sValue|0;'..wind..';'..waardes..';0;0;0'
end
return commandArray
Re: if statment not working lua
Posted: Wednesday 22 May 2019 22:39
by jvdz
I see you test for 2 different devicenames and for both of those different devices set the same domotics device. I would think that the result will be that you update it 2 times and thus only contain the information of the last device. right?
Jos
Re: if statment not working lua
Posted: Wednesday 22 May 2019 23:39
by waaren
gertvanwijk1 wrote: ↑Wednesday 22 May 2019 22:11
hi can anyone explaine why the if statement if waarde == 280 then wind = "N" is not working
we get the value of the device but the update(wind) in idx 8 is always "W"
This is because you set waarde to nil in your script. Have a look at this one.
Code: Select all
commandArray = {}
wind="W"
-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
if deviceName == 'DarkSky wind' then
print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
if deviceName == 'DarkSky wind' then
waardes = tostring(deviceValue)
end
if deviceName == 'DarkSky wind' then
waarde = tonumber(deviceValue) -- this cannot work as deviceValue is something like '157.00;SSE;6;7;12.3;12.3'
print("waarde: " .. tostring(waarde) ) -- nil
waarde = tonumber(deviceValue:match("([^;]+)")) -- get the value before the first ;
print("waarde: " .. tostring(waarde) )
if waarde == 280 then
wind = "N"
else
wind = "NW"
end
end
print("waardes: " .. waardes )
print("waarde: " .. tostring(waarde) )
print("wind: " .. wind )
end
end
return commandArray
relevant loglines
- Spoiler: show
- 2019-05-22 23:35:35.044 Status: LUA: Device based event fired on 'DarkSky wind', value '181.00;S;13;13;12.0;12.0'
2019-05-22 23:35:35.045 Status: LUA: waarde: nil
2019-05-22 23:35:35.045 Status: LUA: waarde: 181
2019-05-22 23:35:35.045 Status: LUA: waardes: 181.00;S;13;13;12.0;12.0
2019-05-22 23:35:35.045 Status: LUA: waarde: 181
2019-05-22 23:35:35.045 Status: LUA: wind: NW
Re: if statment not working lua
Posted: Thursday 23 May 2019 19:48
by gertvanwijk1
hi Waaren
Thanks for the reply
The value we get from our selfmade windvaan is for example 300
It is always a 3 digit number
We have 2 sensors one for the wind direction and one for windspeed
we want to convert the digit to for example to NE
then we want the 2 values send to idx 8
Re: if statment not working lua
Posted: Friday 24 May 2019 9:57
by waaren
gertvanwijk1 wrote: ↑Thursday 23 May 2019 19:48
The value we get from our selfmade windvaan is for example 300
We have 2 sensors one for the wind direction and one for windspeed
Can you you add this small Lua script and let it execute for a couple of minutes. It will give some information on what values are in your wind devices. It might help in creating the solution you look for.
Code: Select all
commandArray = {}
for deviceName,deviceValue in pairs(devicechanged) do
if deviceName:match('Wind') or deviceName:match('wind') then
print("waarde: " .. deviceName .. " " .. tostring(deviceValue) )
for key, value in pairs (devicechanged) do
print(key .. ": ==>> " .. value )
end
end
end
return commandArray
Re: if statment not working lua
Posted: Wednesday 05 June 2019 19:56
by gertvanwijk1
Hi Waaren,
this is the result from the script you gave us
Code: Select all
2019-06-05 19:46:09.846 Status: LUA: windmeter: ==>> -0
2019-06-05 19:46:10.801 Status: Warning: Expecting svalue with at least 5 elements separated by semicolon, 1 elements received ("280"), notification not sent (Hardware: 2 - Dummy, ID: 82001, Unit: 1, Type: 56 - Wind, SubType: 1 - WTGR800)
2019-06-05 19:46:10.809 Status: LUA: waarde: wind 280
2019-06-05 19:46:10.809 Status: LUA: wind: ==>> 280
2019-06-05 19:46:10.948 Status: Warning: Expecting svalue with at least 5 elements separated by semicolon, 1 elements received ("0"), notification not
now we want to convert 280
Code: Select all
if (waarde and waarde > 304 and waarde and waarde < 308) then wind ="N"
elseif (waarde and waarde > 277 and waarde and waarde < 283) then wind ="NE"
elseif (waarde and waarde > 255 and waarde and waarde < 261) then wind ="E"
elseif (waarde and waarde > 236 and waarde and waarde < 242) then wind ="SE"
elseif (waarde and waarde > 220 and waarde and waarde < 226) then wind ="S"
elseif (waarde and waarde > 206 and waarde and waarde < 212) then wind ="SW"
elseif (waarde and waarde > 193 and waarde and waarde < 199) then wind ="W"
elseif (waarde and waarde > 182 and waarde and waarde < 188) then wind ="NW"
so far so good
the next thing we want is the value form windmeter
Code: Select all
if (deviceName=='windmeter') then
waardes = tostring(deviceValue)
end
this is now 0 there is no wind
stil so far so good
the next thing is to get it into domoticz idx 8
Code: Select all
commandArray['UpdateDevice']='8|sValue|0;'..wind..';'..waardes..';0;0;0'
en then it fails
this is the complete script
Code: Select all
commandArray = {}
waardes = 100
-- loop through all the changed devices
for deviceName,deviceValue in pairs(devicechanged) do
print ("Device based event fired on '"..deviceName.."', value '"..tostring(deviceValue).."'");
if (deviceName=='wind') then
waarde = tonumber(deviceValue)
if (waarde and waarde > 304 and waarde and waarde < 308) then wind ="N"
elseif (waarde and waarde > 277 and waarde and waarde < 283) then wind ="NE"
elseif (waarde and waarde > 255 and waarde and waarde < 261) then wind ="E"
elseif (waarde and waarde > 236 and waarde and waarde < 242) then wind ="SE"
elseif (waarde and waarde > 220 and waarde and waarde < 226) then wind ="S"
elseif (waarde and waarde > 206 and waarde and waarde < 212) then wind ="SW"
elseif (waarde and waarde > 193 and waarde and waarde < 199) then wind ="W"
elseif (waarde and waarde > 182 and waarde and waarde < 188) then wind ="NW"
end
if (deviceName=='windmeter') then
waardes = tostring(deviceValue)
end
end
print(wind)
print(waarde)
print(waardes)
commandArray['UpdateDevice']='8|sValue|0;'..wind..';'..waardes..';0;0;0'
end
return commandArray
so what is wrong?
Re: if statment not working lua
Posted: Wednesday 05 June 2019 22:52
by waaren
gertvanwijk1 wrote: ↑Wednesday 05 June 2019 19:56
.... and then it fails. So what is wrong?
Sorry but I pass for the standard Lua stuff here. The script below is a possible solution using dzvents.
When not yet familiar with dzVents please start with reading
Get started before implementing. It will only take a couple of minutes.
Special attention please for "In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."
Code: Select all
-- Transfer wind
return {
on = { devices = { 'wind' }},
logging = { level = domoticz.LOG_DEBUG,
marker = 'wind' },
execute = function(dz, item)
local waardes = 10
local windDirection = item.rawData[1] or -1 -- don't know what the devicetype of wind is so assume direction is rawData[1]
local targetDevice = dz.devices(8) -- Type wind
local function val2Wind(degrees)
if degrees > 304 then return 'N' end
if degrees > 277 then return 'NE' end
if degrees > 255 then return 'SE' end
if degrees > 236 then return 'S' end
if degrees > 220 then return 'SW' end
if degrees > 206 then return 'W' end
if degrees > 193 then return 'NW' end
return '-'
end
targetDevice.updateWind( windDirection, val2Wind(tonumber(windDirection)), waardes, waardes, 0 , 0 )
end
}