Domoticz 2024.1版本下Heatzy模块控制Lua脚本报错修复请求
问题
Domoticz升级至2024.1版本后,控制Heatzy加热模块的Lua脚本报错,错误信息:
Error: EventSystem: in HeatzyStatusSalon: [string "json = (loadfile "/home/pi/domoticz/scripts/l..."]:20: attempt to index a nil value (field 'attr')
原脚本内容:
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For Linux enlever les -- devant la ligne qui vous interesse et mettre -- devant la ligne a commenter -- json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")() -- For raspberry enlever les -- devant la ligne qui vous interesse et mettre -- devant la ligne a commenter -- json = (loadfile "D:\\Domoticz\\scripts\\lua\\json.lua")() -- For Windows enlever les -- devant la ligne qui vous interesse et mettre -- devant la ligne a commenter -- json = (loadfile "/volume1/@appstore/domoticz/var/scripts/lua/json.lua")() -- For Syno enlever les -- devant la ligne qui vous interesse et mettre -- devant la ligne a commenter --variable a configurer did = 'MYDID' --did que l'on a trouvé, on garde les '' selectorheatzy = 'Chauffage Salon' --nom de votre inter selecteur qui pilote le heatzy on garde les '' --fin des variable a configurere --ligne de commande qui interoge les serveur heatzy pour le retour d'etat local config=assert(io.popen('curl -X GET --header \'Accept: application/json\' --header \'X-Gizwits-Application-Id: c70a66ff039d41b4a220e198b0fcc8b3\' \'https://euapi.gizwits.com/app/devdata/'..did..'/latest\'')) --parsing du json local blocjson = config:read('*all') config:close() --print (blocjson) local jsonValeur = json:decode(blocjson) mode = jsonValeur.attr.mode --print('****************') --print(mode) commandArray = {} if mode == 'stop' and otherdevices[selectorheatzy]~='Off' then print("heatzy le radiateur est passé en mode Stop") commandArray[selectorheatzy]='Set Level: 0' elseif mode == 'cft' and otherdevices[selectorheatzy]~='Confort' then print("heatzy le radiateur est passé en mode Confort") commandArray[selectorheatzy]='Set Level: 10' elseif mode == 'eco' and otherdevices[selectorheatzy]~='Eco' then print("heatzy le radiateur est passé en mode Eco") commandArray[selectorheatzy]='Set Level: 20' elseif mode == 'fro' and otherdevices[selectorheatzy]~='Hors Gel' then print("heatzy le radiateur est passé en mode Hors-gel") commandArray[selectorheatzy]='Set Level: 30' end return commandArray
该脚本此前可正常运行,升级后出现上述错误,请求修复。
修复方案
报错核心原因是jsonValeur.attr为nil,大概率是Domoticz 2024.1对Lua环境的调整导致外部JSON库加载异常,或API返回数据结构/请求状态异常。以下是针对性修复:
1. 改用Domoticz内置JSON库
Domoticz自带JSON解析工具,无需依赖外部JSON.lua,避免版本兼容问题。
2. 增加数据校验逻辑
在访问嵌套字段前先检查各级数据是否存在,防止nil值索引报错。
3. 添加调试日志
打印API返回的原始内容,方便排查请求或数据结构问题。
修改后的完整脚本:
-- 变量配置 did = 'MYDID' -- 替换为你的Heatzy设备ID selectorheatzy = 'Chauffage Salon' -- 替换为Domoticz中选择器的名称 -- 请求Heatzy API获取状态 local config = assert(io.popen('curl -X GET --header \'Accept: application/json\' --header \'X-Gizwits-Application-Id: c70a66ff039d41b4a220e198b0fcc8b3\' \'https://euapi.gizwits.com/app/devdata/'..did..'/latest\'')) local blocjson = config:read('*all') config:close() -- 调试:打印API返回内容(可在Domoticz日志中查看) print('Heatzy API返回内容: '..blocjson) -- 使用Domoticz内置JSON解析 local jsonValeur = domoticz.utils.fromJSON(blocjson) local mode = nil -- 校验数据结构 if jsonValeur then if jsonValeur.attr and jsonValeur.attr.mode then mode = jsonValeur.attr.mode print('Heatzy当前模式: '..mode) else print('错误:API返回的JSON中缺少attr.mode字段') end else print('错误:无法解析API返回的JSON内容') end commandArray = {} -- 仅当mode有效时执行同步 if mode then if mode == 'stop' and otherdevices[selectorheatzy]~='Off' then print("Heatzy切换为Stop模式,同步Domoticz选择器") commandArray[selectorheatzy]='Set Level: 0' elseif mode == 'cft' and otherdevices[selectorheatzy]~='Confort' then print("Heatzy切换为Confort模式,同步Domoticz选择器") commandArray[selectorheatzy]='Set Level: 10' elseif mode == 'eco' and otherdevices[selectorheatzy]~='Eco' then print("Heatzy切换为Eco模式,同步Domoticz选择器") commandArray[selectorheatzy]='Set Level: 20' elseif mode == 'fro' and otherdevices[selectorheatzy]~='Hors Gel' then print("Heatzy切换为Hors-gel模式,同步Domoticz选择器") commandArray[selectorheatzy]='Set Level: 30' else print("Heatzy模式与Domoticz选择器状态一致,无需同步") end end return commandArray
额外排查建议
- 查看Domoticz日志中的API返回内容,如果是401/404等错误,确认设备ID
did是否有效,或Heatzy API是否有更新 - 在终端手动执行脚本中的curl命令,验证是否能返回预期的JSON数据
内容的提问来源于stack exchange,提问作者Frederic B.
相关产品推荐
相关产品推荐

