分页: 1 / 1

请教lua中如何将值由文本改为数值。

发表于 : 周五 3月 09, 2018 10:45
rayborng
diy了攀藤的传感器,并按照网上教程将值传入domoticz,读取攀藤的4个值,采用文本分割,并分别更新到各个虚拟设备上。
有个问题:添加blocky判断的时候总是失效,因数据为文本,无法做大于小于的判断,所以失效。
代码小白,都是照搬。研究了半月还是不会,请求帮助大神帮忙解决下。能否改写成数值格式。
贴出代码

代码: 全选

commandArray = {}

if devicechanged['pms'] then  

    local device = otherdevices_svalues["pms"] 
    print(device) 
    
    local pm1, pm25, pm10, hcho
    
    _,_,pm1, pm25, pm10, hcho  = string.find(device, "(.+);(.+);(.+);(.+)")
    
    print(pm1)  --show temp in LOG 
    print(pm25)  --show humidity in LOG 
    print(pm10)  --show humstat in LOG,
    print(hcho)  --show humstat in LOG,

commandArray[1] = {['UpdateDevice'] = 4 .. '|0|' .. pm25}
commandArray[2] = {['UpdateDevice'] = 3 .. '|0|' .. pm10}
commandArray[4] = {['UpdateDevice'] = 14 .. '|0|' .. hcho}

end
return commandArray

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周五 3月 09, 2018 11:00
rayborng
查看了domoticz wiki 的例子:

代码: 全选

 commandArray[textDeviceIdx] = { ['UpdateDevice'] = textDeviceIdx..'|0|'..'some text' }
难道只能更新为文本?

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周二 3月 27, 2018 17:55
flying_dog
我修改好插件后本地能显示4个值,但传到domoticz就有变成3个值了,您有碰到类似问题吗

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周三 3月 28, 2018 08:40
Admin
rayborng 写了: 周五 3月 09, 2018 10:45 diy了攀藤的传感器,并按照网上教程将值传入domoticz,读取攀藤的4个值,采用文本分割,并分别更新到各个虚拟设备上。
有个问题:添加blocky判断的时候总是失效,因数据为文本,无法做大于小于的判断,所以失效。
代码小白,都是照搬。研究了半月还是不会,请求帮助大神帮忙解决下。能否改写成数值格式。
贴出代码

代码: 全选

commandArray = {}

if devicechanged['pms'] then  

    local device = otherdevices_svalues["pms"] 
    print(device) 
    
    local pm1, pm25, pm10, hcho
    
    _,_,pm1, pm25, pm10, hcho  = string.find(device, "(.+);(.+);(.+);(.+)")
    
    print(pm1)  --show temp in LOG 
    print(pm25)  --show humidity in LOG 
    print(pm10)  --show humstat in LOG,
    print(hcho)  --show humstat in LOG,

commandArray[1] = {['UpdateDevice'] = 4 .. '|0|' .. pm25}
commandArray[2] = {['UpdateDevice'] = 3 .. '|0|' .. pm10}
commandArray[4] = {['UpdateDevice'] = 14 .. '|0|' .. hcho}

end
return commandArray
我测试了一下判断没问题啊,你的设备是Custom Sensor吗?
屏幕快照 2018-03-28 08.41.36.jpg
屏幕快照 2018-03-28 08.41.36.jpg (16.88 KiB) 查看 25822 次
屏幕快照 2018-03-28 08.41.03.jpg
屏幕快照 2018-03-28 08.41.03.jpg (50.74 KiB) 查看 25822 次
屏幕快照 2018-03-28 08.39.41.jpg
屏幕快照 2018-03-28 08.39.41.jpg (56.6 KiB) 查看 25823 次

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周三 3月 28, 2018 23:55
123hundun123
Admin 写了: 周三 3月 28, 2018 08:40
rayborng 写了: 周五 3月 09, 2018 10:45 diy了攀藤的传感器,并按照网上教程将值传入domoticz,读取攀藤的4个值,采用文本分割,并分别更新到各个虚拟设备上。
有个问题:添加blocky判断的时候总是失效,因数据为文本,无法做大于小于的判断,所以失效。
代码小白,都是照搬。研究了半月还是不会,请求帮助大神帮忙解决下。能否改写成数值格式。
贴出代码

代码: 全选

commandArray = {}

if devicechanged['pms'] then  

    local device = otherdevices_svalues["pms"] 
    print(device) 
    
    local pm1, pm25, pm10, hcho
    
    _,_,pm1, pm25, pm10, hcho  = string.find(device, "(.+);(.+);(.+);(.+)")
    
    print(pm1)  --show temp in LOG 
    print(pm25)  --show humidity in LOG 
    print(pm10)  --show humstat in LOG,
    print(hcho)  --show humstat in LOG,

commandArray[1] = {['UpdateDevice'] = 4 .. '|0|' .. pm25}
commandArray[2] = {['UpdateDevice'] = 3 .. '|0|' .. pm10}
commandArray[4] = {['UpdateDevice'] = 14 .. '|0|' .. hcho}

end
return commandArray
我测试了一下判断没问题啊,你的设备是Custom Sensor吗?
屏幕快照 2018-03-28 08.41.36.jpg
屏幕快照 2018-03-28 08.41.03.jpg
屏幕快照 2018-03-28 08.39.41.jpg
请问您是用楼主提供的lua脚本吗,我测试这个脚本确实无法进行数值判断

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周四 3月 29, 2018 11:54
Admin
123hundun123 写了: 周三 3月 28, 2018 23:55
Admin 写了: 周三 3月 28, 2018 08:40
rayborng 写了: 周五 3月 09, 2018 10:45 diy了攀藤的传感器,并按照网上教程将值传入domoticz,读取攀藤的4个值,采用文本分割,并分别更新到各个虚拟设备上。
有个问题:添加blocky判断的时候总是失效,因数据为文本,无法做大于小于的判断,所以失效。
代码小白,都是照搬。研究了半月还是不会,请求帮助大神帮忙解决下。能否改写成数值格式。
贴出代码

代码: 全选

commandArray = {}

if devicechanged['pms'] then  

    local device = otherdevices_svalues["pms"] 
    print(device) 
    
    local pm1, pm25, pm10, hcho
    
    _,_,pm1, pm25, pm10, hcho  = string.find(device, "(.+);(.+);(.+);(.+)")
    
    print(pm1)  --show temp in LOG 
    print(pm25)  --show humidity in LOG 
    print(pm10)  --show humstat in LOG,
    print(hcho)  --show humstat in LOG,

commandArray[1] = {['UpdateDevice'] = 4 .. '|0|' .. pm25}
commandArray[2] = {['UpdateDevice'] = 3 .. '|0|' .. pm10}
commandArray[4] = {['UpdateDevice'] = 14 .. '|0|' .. hcho}

end
return commandArray
我测试了一下判断没问题啊,你的设备是Custom Sensor吗?
屏幕快照 2018-03-28 08.41.36.jpg
屏幕快照 2018-03-28 08.41.03.jpg
屏幕快照 2018-03-28 08.39.41.jpg
请问您是用楼主提供的lua脚本吗,我测试这个脚本确实无法进行数值判断
用脚本试了,没问题。
整数、小数都测试过。
PM2.5等设备类型都是 Custom Sensor

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周五 3月 30, 2018 22:55
bingoscc
给你个参考
commandArray = {}

local PM25 = otherdevices_svalues["卧室PM2.5"]
local PM10 = otherdevices_svalues["卧室PM10"]

if(tonumber(PM25) > 100 or tonumber(PM10) > 100) then commandArray['卧室空气净化器'] = 'Set Level: 10'

elseif(tonumber(PM25) < 30 and tonumber(PM10) < 30) then commandArray['卧室空气净化器'] = 'Set Level: 0'

end

return commandArray
tonumber(PM25)就是把对应设备的文字转数值

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周六 3月 31, 2018 08:53
Admin
bingoscc 写了: 周五 3月 30, 2018 22:55 给你个参考
commandArray = {}

local PM25 = otherdevices_svalues["卧室PM2.5"]
local PM10 = otherdevices_svalues["卧室PM10"]

if(tonumber(PM25) > 100 or tonumber(PM10) > 100) then commandArray['卧室空气净化器'] = 'Set Level: 10'

elseif(tonumber(PM25) < 30 and tonumber(PM10) < 30) then commandArray['卧室空气净化器'] = 'Set Level: 0'

end

return commandArray
tonumber(PM25)就是把对应设备的文字转数值
楼主是blockly做判断。

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周一 7月 09, 2018 00:44
lnight
楼主那确实有问题,要改也简单,举个例子。

代码: 全选

PM25 = tonumber(PM25)
commandArray[1] = {['UpdateDevice'] =idp .. '|0|' .. tostring(PM25)}

Re: 请教lua中如何将值由文本改为数值。

发表于 : 周四 7月 12, 2018 20:19
spender
tonumber(string_var)
楼上正解