How to update device attribute batterylevel
Posted: Friday 09 March 2018 14:52
Is there a way to update or change the device attribute batterylevel on a virtual device? Just want to create custom virtual battery if possible
Open source Home Automation System
https://forum.domoticz.com/
Code: Select all
local batteryDevice = domoticz.devices("Your battery device name") -- Type: General, subType: Custom Sensor
local batteryLevel = yourBatteryLevel -- int between 0 - 100
batteryDevice.update(0,batteryLevel)
Thanks for your reaction.waaren wrote: ↑Sunday 11 March 2018 12:39tested on devices created by Z-Wave battery monitor pluginCode: Select all
local batteryDevice = domoticz.devices("Your battery device name") -- Type: General, subType: Custom Sensor local batteryLevel = yourBatteryLevel -- int between 0 - 100 batteryDevice.update(0,batteryLevel)
Code: Select all
http://192.168.xxx.yy:8080/json.htm?type=devices&rid=94
If you are still interested...
Code: Select all
--[[
updateBatterylevel
]] --
return {
on = {
devices = { 'Your triggerdevice name'} ,
timer = { 'every 30 minutes' }, -- timer or device to trigger the script. Can be both or one of them
httpResponses = { 'setBatterylevel' } -- When json call is answered it will trigger this
},
execute = function(domoticz, trigger)
local debug = true
function debug_print(myLine)
if debug then print("***** updateBatterylevel ****** " .. myLine) end
end
if trigger.isHTTPResponse then
debug_print("Triggered by HTTPResponse")
if (trigger.ok) then -- statusCode == 2xx
local setting = trigger.json.status
debug_print("result: " .. setting )
end
else
debug_print("Triggered by device or timer")
local myIP ="nnn.nnn.nnn.nnn" -- your domoticz system IP
local myPort ="nnnn" -- your domoticz port
local batteryDevice = domoticz.devices("BatteryTest")
local deviceIDX = batteryDevice.idx
local newBatterylevel = batteryDevice.batteryLevel or math.random(100)
local myNvalue = batteryDevice.nValue
local mySvalue = batteryDevice.state
local mySignal = batteryDevice.signalLevel
local myUrl = "http://" .. myIP .. ":" .. myPort .. "/json.htm?type=command¶m=udevice&idx=" .. deviceIDX
myUrl = myUrl .. "&nvalue=" .. myNvalue .. "&svalue=" .. mySvalue .. "&rssi=" .. mySignal .. "&battery=" .. newBatterylevel
debug_print("myUrl: " .. myUrl)
domoticz.openURL({
url = myUrl,
method = "GET",
callback = "setBatterylevel"
})
end
end
}
Thanks a lot I will have a look at it.waaren wrote: ↑Tuesday 20 March 2018 21:22If you are still interested...Code: Select all
--[[ updateBatterylevel ]] -- return { on = { devices = { 'Your triggerdevice name'} , timer = { 'every 30 minutes' }, -- timer or device to trigger the script. Can be both or one of them httpResponses = { 'setBatterylevel' } -- When json call is answered it will trigger this }, execute = function(domoticz, trigger) local debug = true function debug_print(myLine) if debug then print("***** updateBatterylevel ****** " .. myLine) end end if trigger.isHTTPResponse then debug_print("Triggered by HTTPResponse") if (trigger.ok) then -- statusCode == 2xx local setting = trigger.json.status debug_print("result: " .. setting ) end else debug_print("Triggered by device or timer") local myIP ="nnn.nnn.nnn.nnn" -- your domoticz system IP local myPort ="nnnn" -- your domoticz port local batteryDevice = domoticz.devices("BatteryTest") local deviceIDX = batteryDevice.idx local newBatterylevel = batteryDevice.batteryLevel or math.random(100) local myNvalue = batteryDevice.nValue local mySvalue = batteryDevice.state local mySignal = batteryDevice.signalLevel local myUrl = "http://" .. myIP .. ":" .. myPort .. "/json.htm?type=command¶m=udevice&idx=" .. deviceIDX myUrl = myUrl .. "&nvalue=" .. myNvalue .. "&svalue=" .. mySvalue .. "&rssi=" .. mySignal .. "&battery=" .. newBatterylevel debug_print("myUrl: " .. myUrl) domoticz.openURL({ url = myUrl, method = "GET", callback = "setBatterylevel" }) end end }
@hoeby, this script was created to test if it is possible to set the batterylevel on a virtual device. It will look for the batterylevel attribute of a testdevice but if that attribute is not set for that specific device it will generate a random number between 0 and 100 and update the batterylevel of a virtual device with it.
Code: Select all
or math.random(100)
Code: Select all
--[[
updateBatterylevel
]] --
return {
on = {
devices = { 'Xiaomi Door Sensor'} ,
timer = { 'every 1 minutes' }, -- timer or device to trigger the script. Can be both or one of them
httpResponses = { 'setBatterylevel' } -- When json call is answered it will trigger this
},
execute = function(domoticz, trigger)
local debug = true
function debug_print(myLine)
if debug then print("***** updateBatterylevel ****** " .. myLine) end
end
if trigger.isHTTPResponse then
debug_print("Triggered by HTTPResponse")
if (trigger.ok) then -- statusCode == 2xx
local setting = trigger.json.status
debug_print("result: " .. setting )
end
else
debug_print("Triggered by device or timer")
local myIP ="192.168.178.29" -- your domoticz system IP
local myPort ="8080" -- your domoticz port
local batteryDevice = domoticz.devices("Xiaomi Door Sensor")
local deviceIDX = batteryDevice.idx
local newBatterylevel = batteryDevice.batteryLevel
local myNvalue = batteryDevice.nValue
local mySvalue = batteryDevice.batteryLevel
local mySignal = batteryDevice.signalLevel
local myUrl = "http://192.168.178.29:8080/json.htm?type=command¶m=udevice&idx=100"
myUrl = myUrl .. "&nvalue=" .. myNvalue .. "&svalue=" .. mySvalue .. "&rssi=" .. mySignal .. "&battery=" .. newBatterylevel
debug_print("myUrl: " .. myUrl)
domoticz.openURL({
url = myUrl,
callback = "setBatterylevel"
})
end
end
}
Code: Select all
--
-- Lua dzVents Script
-- Check Dead Device and set BatteryLevel
--
return {
on = {
devices = {
'Doppellampe'
},
timer = {
'at 12:00'
}
},
logging = {
level = domoticz.LOG_DEBUG,
marker = 'ManageBattery'
},
execute = function(domoticz, item)
-- Every Day at 12:00 do this:
if(item.isTimer) then
-- Check all devices for potential deadness based on the latest update:
local deadDevices = domoticz.devices().filter(function(device)
return (device.lastUpdate.daysAgo > 4)
end)
deadDevices.forEach(function(deadDevice)
domoticz.openURL('http://192.168.178.10:8080/json.htm?type=command¶m=udevice&idx=' ..deadDevice.idx.. '&nvalue=' ..deadDevice.nValue.. '&svalue=""&rssi=7&battery=5')
domoticz.log('Set potential dead device ' ..deadDevice.name.. ' Battery & RSSI Level!', domoticz.LOG_DEBUG)
end)
-- If any device is changed, do this:
elseif(item.isDevice) then
local deviceIDX = item.idx
local newBatterylevel = item.batteryLevel or math.random(50, 70)
local myNvalue = item.nValue
local mySvalue = item.state
local mySignal = item.signalLevel or math.random(5, 8)
local myUrl = "http://192.168.178.10:8080/json.htm?type=command¶m=udevice&idx=" ..deviceIDX.. "&nvalue=" ..myNvalue.. "&svalue=" ..mySvalue.. "&rssi=" ..mySignal.. "&battery=" ..newBatterylevel
domoticz.log('myUrl: ' ..myUrl, domoticz.LOG_DEBUG)
domoticz.openURL({
url = myUrl,
method = "GET",
callback = "setBatterylevel"
})
end
end
}