Calculate Tangents, Sines and Cosines

Anybody know how to calculate the tangent of an angle in an action? I am trying to take the sun’s elevation from the SunCalc driver and calculate how far I should lower my shades to cast a certain shadow.

Thanks!

1 Like

I see where @augoisms uses trigonometric functions in his SunCalc driver. I don’t have the skills to write something like a simple driver that could return a tangent based on an angle. Any help appreciated!

Looking in browser for "Groovy Tangent", I found the following reference:

https://www.tutorialspoint.com/groovy/groovy_tan.htm

Other Groovy number functions can be found at:

https://www.tutorialspoint.com/groovy/groovy_numbers.htm

You may or may not have to do some sort of import function for the functions to work. First step is to try without.

Dave

PS: I would recommend using bands of sun angles that relate to a desired shade angle. You can do this by converting the angle to an integer and then use the switch function. An example of switch from the color naming driver is below. You input the angle and the set the shade angle for each band. Note that the below method was authored by @mikemaxwell for naming color on a bulb. it is adaptable for your function.

> def setRgbData(hue, saturation){
> 	logDebug("setRgbData: hue = ${hue} // highRes = ${highRes}")
> 	state.lastHue = hue
> 	state.lastSaturation = saturation
> 	if (highRes != true) { hue = (hue * 3.6).toInteger() }
>     def colorName
> 	switch (hue){
> 		case 0..15: colorName = "Red"
>             break
> 		case 16..45: colorName = "Orange"
>             break
> 		case 46..75: colorName = "Yellow"
>             break
> 		case 76..105: colorName = "Chartreuse"
>             break
> 		case 106..135: colorName = "Green"
>             break
> 		case 136..165: colorName = "Spring"
>             break
> 		case 166..195: colorName = "Cyan"
>             break
> 		case 196..225: colorName = "Azure"
>             break
> 		case 226..255: colorName = "Blue"
>             break
> 		case 256..285: colorName = "Violet"
>             break
> 		case 286..315: colorName = "Magenta"
>             break
> 		case 316..345: colorName = "Rose"
>             break
> 		case 346..360: colorName = "Red"
>             break
> 		default:
> 			logWarn("setRgbData: Unknown.")
> 			colorName = "Unknown"
>     }
> 	logInfo "${device.getDisplayName()} Color Mode is RGB.  Color is ${colorName}."
>  	sendEvent(name: "colorMode", value: "RGB")
>     sendEvent(name: "colorName", value: colorName)
> }
1 Like

Thanks, Dave. I really need a simple driver for calculating the tangent within a decimal point or so, so the table suggestion would be too coarse. I don’t know enough about the Rule environment and programming language to extrapolate from the references you gave to a working product. Can you take it that last step for me?

Much appreciated,

Don

Maybe I'm misunderstanding... but Groovy's tangent doesn't use a "table" (it's a switch). And neither would using Java java.lang.Math?

See below copied from turorial on the web. I think this is correct.

Following is an example of the usage of this method −
Live Demo

class Example { 
   static void main(String[] args) { 
      double degrees = 45.0; 
      double radians = Math.toRadians(degrees);  
		
      System.out.format("The value of pi is %.4f%n", Math.PI); 
      System.out.format("The tangent of %.1f degrees is %.4f%n", degrees, Math.tan(radians));  
   } 
}

When we run the above program, we will get the following result −

The value of pi is 3.1416 
The tangent of 45.0 degrees is 1.0000

Just tested in Hubitat and it works direct. For test, I did a
log.error Math.tan(2.2) receiving an approximate expected value.

2 Likes

Yes, this is what I meant @djgutheinz. Thanks for taking the time to create an example for @Donald.

I really appreciate the help, @codahq

Unfortunately, my ability to code in this environment is extremely limited. The example is instructive, but not enough. What I need are the exact lines of code that I could install as a Rule Machine driver where I can take the elevation angle variable returned from SunCalc, calculate its tangent, then return it as a new variable, say ElTan.

Again, I appreciate the help.

Don