DZVents Xiaomi Magic Cube

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

Moderator: leecollings

Post Reply
User avatar
lonebaggie
Posts: 86
Joined: Tuesday 31 January 2017 13:21
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: England
Contact:

DZVents Xiaomi Magic Cube

Post by lonebaggie »

Been playing with the Xiaomi magic Cube to control the TV very cool. However soon discovered after the Cat knocked the cube off the table and turn the TV off in the middle of the football needed something more sophisticated.

This script will turn on the cube for 5 mins when the tap twice action is performed . The hub speaker will play the knock on wood ring tome to confirm activation. after 5 mins of non use the cube becomes inactive until the tap twice action is performed again .

The script uses a string user variable XiaomiCube to hold the date and time of the last action. This is used to time the activation window.

The script assumes all the device names for Xiaomi hub and Cube are unchanged.

Replace the device names to be triggered between the IF statements for the cube actions except the first IF (tap twice) which updates the the user variable . You must add

Code: Select all

domoticz.variables("XiaomiCube").set(currentTime.rawDate .. " " .. currentTime.rawTime)
within the If statments for the other cube actions to keep the time windows open.

I toggle the actions so if the TV is On the Flip 180 action will turn the TV Off. If its Off the Flip 180 action will turn the TV on.

I use the clock wise and anti clockwise to change volume and the shake air to turn on netflix


Code: Select all

return {
	active = true, 
	on = {
		devices = {
			'Xiaomi Cube' 
		},

	},
	execute = function(domoticz, cube)
		local Time = require('Time')
        	local currentTime = Time()
        	local cubeset = Time(domoticz.variables("XiaomiCube").value)
        	if (cube.levelName == "tap_twice") then
            		domoticz.variables("XiaomiCube").set(currentTime.rawDate .. " " .. currentTime.rawTime)
            		cubeset = Time(currentTime.rawDate .. " " .. currentTime.rawTime)
            		domoticz.devices("Xiaomi Gateway Doorbell").switchSelector(20)
            		domoticz.log("Cube is active for 5 Mins time now is " .. cubeset.rawTime)
        	end
	    	if (cube.levelName == 'shake_air') and cubeset.minutesAgo < 5 then
	        	domoticz.variables("XiaomiCube").set(currentTime.rawDate .. " " .. currentTime.rawTime)
		    	if domoticz.devices("Movies").state == "On" then
		        	domoticz.devices("Movies").switchOff()
		        	domoticz.log('Movies Off')
		    	elseif domoticz.devices("Movies").state == "Off" then
		        	domoticz.devices("Movies").switchOn()
		        	domoticz.log('Movies On')
		    	end
		end
		if (cube.levelName == 'flip180') and cubeset.minutesAgo < 5 then
			domoticz.variables("XiaomiCube").set(currentTime.rawDate .. " " .. currentTime.rawTime)
		     	if domoticz.devices("TV").state == "On" then
		        	domoticz.devices("TV").switchOff()
		        	domoticz.log('TV Off')
		    	elseif domoticz.devices("TV").state == "Off" then
		        	domoticz.devices("TV").switchOn()
		        	domoticz.log('TV on')
		    	end
        	end
       	 	if (cube.levelName == 'clock_wise') and cubeset.minutesAgo < 5 then
		     	domoticz.variables("XiaomiCube").set(currentTime.rawDate .. " " .. currentTime.rawTime)
		     	domoticz.devices("Sound").switchOn()    
        	end
        	if (cube.levelName == 'anti_clock_wise') and cubeset.minutesAgo < 5 then
		     	domoticz.variables("XiaomiCube").set(currentTime.rawDate .. " " .. currentTime.rawTime)
		     	domoticz.devices("Sound").switchOff()     
        	end
	end
}
}
This is my first DzVents script, and I struggled with timer functions , so if there is a better way to achieve this let me know
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest