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

文档:
https://en.domoticz.cn/wiki/DzVents:_ne ... _scripting
回复
rayborng
帖子: 10
注册时间: 周一 12月 18, 2017 10:10

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

帖子 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
上次由 rayborng 在 周五 3月 09, 2018 11:40,总共编辑 1 次。
rayborng
帖子: 10
注册时间: 周一 12月 18, 2017 10:10

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

帖子 rayborng »

查看了domoticz wiki 的例子:

代码: 全选

 commandArray[textDeviceIdx] = { ['UpdateDevice'] = textDeviceIdx..'|0|'..'some text' }
难道只能更新为文本?
flying_dog
帖子: 2
注册时间: 周二 3月 27, 2018 00:24

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

帖子 flying_dog »

我修改好插件后本地能显示4个值,但传到domoticz就有变成3个值了,您有碰到类似问题吗
头像
Admin
网站管理员
帖子: 118
注册时间: 周六 2月 25, 2017 12:47
Gender:

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

帖子 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) 查看 25729 次
屏幕快照 2018-03-28 08.41.03.jpg
屏幕快照 2018-03-28 08.41.03.jpg (50.74 KiB) 查看 25729 次
屏幕快照 2018-03-28 08.39.41.jpg
屏幕快照 2018-03-28 08.39.41.jpg (56.6 KiB) 查看 25730 次
123hundun123
帖子: 1
注册时间: 周三 3月 28, 2018 23:52

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

帖子 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脚本吗,我测试这个脚本确实无法进行数值判断
头像
Admin
网站管理员
帖子: 118
注册时间: 周六 2月 25, 2017 12:47
Gender:

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

帖子 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
bingoscc
帖子: 2
注册时间: 周三 3月 14, 2018 12:07
Gender:

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

帖子 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)就是把对应设备的文字转数值
头像
Admin
网站管理员
帖子: 118
注册时间: 周六 2月 25, 2017 12:47
Gender:

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

帖子 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做判断。
lnight
帖子: 11
注册时间: 周六 7月 07, 2018 18:56

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

帖子 lnight »

楼主那确实有问题,要改也简单,举个例子。

代码: 全选

PM25 = tonumber(PM25)
commandArray[1] = {['UpdateDevice'] =idp .. '|0|' .. tostring(PM25)}
spender
帖子: 11
注册时间: 周二 7月 10, 2018 06:04

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

帖子 spender »

tonumber(string_var)
楼上正解
回复