Page 1 of 1

How to update device attribute batterylevel

Posted: Friday 09 March 2018 14:52
by McMelloW
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

Re: How to update device attribute batterylevel

Posted: Sunday 11 March 2018 12:39
by waaren
McMelloW wrote: 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

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)
tested on devices created by Z-Wave battery monitor plugin

Re: How to update device attribute batterylevel

Posted: Sunday 11 March 2018 14:09
by McMelloW
waaren wrote: Sunday 11 March 2018 12:39

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)
tested on devices created by Z-Wave battery monitor plugin
Thanks for your reaction.
This does not update the attribute "BatteryLevel"". It remains 255. It does update the attribute "Data"

Code: Select all

http://192.168.xxx.yy:8080/json.htm?type=devices&rid=94

Re: How to update device attribute batterylevel

Posted: Sunday 11 March 2018 14:34
by waaren
I know but if I update the devices created by Z-Wave battery monitor plugin with this, the new value is displayed for the device on the Utility tab.
I have not find anything that is closer to the desired result yet.

Re: How to update device attribute batterylevel

Posted: Tuesday 13 March 2018 12:01
by McMelloW
Any device / sensor expert out there willing help me.
How do I update the device attribute BatteryLevel with a number 1-100 on a Virtual device

Re: How to update device attribute batterylevel

Posted: Tuesday 20 March 2018 21:22
by waaren
McMelloW wrote: 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
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&param=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
}

Re: How to update device attribute batterylevel

Posted: Tuesday 20 March 2018 21:46
by McMelloW
waaren wrote: Tuesday 20 March 2018 21:22
McMelloW wrote: 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
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&param=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.

Re: How to update device attribute batterylevel

Posted: Monday 23 July 2018 21:38
by hoeby
I am using a copy of this script.
But it is not very stable.

The battery which i am reading out is 84%.
But whit running the script, the answer of the script is going up and down and never got the right read out.

Some idea's?

Re: How to update device attribute batterylevel

Posted: Monday 23 July 2018 23:48
by waaren
hoeby wrote: Monday 23 July 2018 21:38 I am using a copy of this script.
But it is not very stable.

The battery which i am reading out is 84%.
But whit running the script, the answer of the script is going up and down and never got the right read out.

Some idea's?
@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.
It was never meant to be used other then for test/development purposes. Sorry if that was not clear

Re: How to update device attribute batterylevel

Posted: Tuesday 24 July 2018 7:02
by hoeby
thanks for the reply.

i will try to remove the math.random and look what will hapen. maybe nothing, but i can try

EDIT: tried it, but get an error. disabled the script, tonight i will look further if something can be changed to get it working.
Domoticz can read the battery status of the device. Just looking for it how it works and how it can be used for making virtual switches with it.

Re: How to update device attribute batterylevel

Posted: Tuesday 24 July 2018 19:16
by hoeby
Your code is working whit a little changing

What is changed?
- Removed the

Code: Select all

or math.random(100)
- Changed the URL to the dummy IDX device. I have done this because i want to use it in a dashboard an need the read the battery value of my device. When connecting it to a dummy i can program it different than the IDX which produces the batterylevel. This is not necessary when your are not using dummy devices.

I don't work whit the Local xx = xx function.
Because when debugging i find it easier when to code is writting whitout variables.
Therefor my code is not cleaned up, sorry for that

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&param=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
}

Re: How to update device attribute batterylevel

Posted: Tuesday 18 September 2018 14:13
by jannnfe
I do not understand why the script should go to you. For me it results in an infinite loop, because every time the device is turned on, it is automatically turned on a second time and that triggers the script again. Do not you have this problem? Please check your log.

Re: How to update device attribute batterylevel

Posted: Tuesday 18 September 2018 14:48
by jannnfe
This is my modified script:

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&param=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&param=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
}
And the setting of the signal level does not work. it will always set the default value of 12.