Is it possible to create an endpoint to directly control level of light?


(Tony - SmartThings Unpublished Contributor ) #1

I know I can create a virtual switch that in turn controls a hello home action. But I’m wondering if it’s possible to have an endpoint for a light that accepts a variable X and then sets the light level to X.

Bonus points if you can also direct me to / provide me with example code.

Thanks!


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #2

Ummm… I think SmartTiles must do this, since it offers dimmer control on tiles. And it’s open source.

We can dig through the code (or ask Alex).


(Tony - SmartThings Unpublished Contributor ) #3

Thanks Terry. That’s a good recommendation. I’ll take a look.


(ActionTiles.com co-founder Terry @ActionTiles; GitHub: @cosmicpuppy) #4

So the endpoint calls are buried in the script.js; but it isn’t hard to search for the word dimmer…
I should use line numbers, but just do a search on the raw files. Some relevant clips…


https://raw.githubusercontent.com/625alex/SmartTiles/master/resources/script.js

    $(".dimmer, .dimmerLight").on('slidestop', function(e) {
    	var level = $(this).find("input").val()
		if ($(this).hasClass("active")) {
			animateClick($(this));
			sendCommand($(this).attr("data-type"), $(this).attr("data-device"), "level", level);
		};
		$(this).attr("data-level", level);
    });

Which calls the sendCommand function…

function sendCommand(type, device, command, value) {
	//alert("&type=" + type + "&device=" + device + "&command=" + command + "&value=" + value);
	
	var access_token = getUrlParameter("access_token");
	var request = { type: type, device: device, command: command, value: value};
	if (access_token) request["access_token"] = access_token;
	
	$.get("command", request).done(function( data ) {
		//alert( "Data Loaded: " + data);
		if (data.status == "ok") {
			nextPoll(5);
		}
	}).fail(function() {
		setWTFCloud();
		nextPoll(10);
	});
}

And in the SmartTiles.groovy SmartApp itself, the def command endpoint method:
https://raw.githubusercontent.com/625alex/SmartTiles/master/SmartTiles.groovy

def command() {
	log.debug "command received with params $params"
	if (disableDashboard || readOnlyMode) return [status: "disabled"]

    def id = params.device
    def type = params.type
    def command = params.command
    def value = params.value

   def device

...

	} else if (type == "dimmer" || type == "dimmerLight") {
		def deviceSet = (type == "dimmer" ? dimmers : dimmerLights)
		device = deviceSet?.find{it.id == id}
		if (device) {
			if (command == "toggle") {
				if(device.currentValue('switch') == "on") {
					device.off()
				} else {
					device.setLevel(Math.min((value as Integer) * 10, 99))
				}
			} else if (command == "level") {
				device.setLevel(Math.min((value as Integer) * 10, 99))
			}
		}

...
    
	[status:"ok"]
}

In other words … it’s definitely possible; and, indeed, of course, definitely possible to make a “simple” example that doesn’t involve the layers of Javascript required for the rich functionality of SmartTiles. :wink:

Got some chores tomorrow… But maybe I’ll try a very simple skeleton example. I’m sure dozens of Community folks could do it while they are sleeping; but parsing apart some complex code into a simple example is not a bad way to learn. The positive side effect is a better understanding of how SmartTiles is put together!