How to update device attribute batterylevel

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

How to update device attribute batterylevel

Post 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
Greetings McMelloW
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to update device attribute batterylevel

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

Re: How to update device attribute batterylevel

Post 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
Greetings McMelloW
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to update device attribute batterylevel

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

Re: How to update device attribute batterylevel

Post 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
Greetings McMelloW
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to update device attribute batterylevel

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
McMelloW
Posts: 434
Joined: Monday 20 November 2017 17:01
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.1
Location: Harderwijk, NL
Contact:

Re: How to update device attribute batterylevel

Post 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.
Greetings McMelloW
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: How to update device attribute batterylevel

Post 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?
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: How to update device attribute batterylevel

Post 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
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: How to update device attribute batterylevel

Post 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.
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
hoeby
Posts: 531
Joined: Saturday 02 June 2018 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V2022.1
Location: Echt, Netherlands
Contact:

Re: How to update device attribute batterylevel

Post 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
}
Thin-client --> Docker Domoticz main environment
Pi3A+ --> Google home (GAssistPi)
Pi3B+ --> Docker (P1monitor, Domoticz test environment, Ubiquity controller)
jannnfe
Posts: 30
Joined: Tuesday 30 January 2018 0:27
Target OS: Linux
Domoticz version: Beta
Location: Germany
Contact:

Re: How to update device attribute batterylevel

Post 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.
jannnfe
Posts: 30
Joined: Tuesday 30 January 2018 0:27
Target OS: Linux
Domoticz version: Beta
Location: Germany
Contact:

Re: How to update device attribute batterylevel

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest