Can anyone help with the tuya smart device node?

I've used the tuya-smart-device node, and following the instructions and the example flow managed to get everything linked up and can turn the bulb on and off, but I'm a bit lost as to how I can change the colour and brightness etc.
I've tried looking at the tuyapi documentation but really don't understand any of it! I think I need to do something with the dps values, but don't know what they are or where I might find them.

This is a good place for Tuya device info tuya-mqtt/DEVICES.md at master · TheAgentK/tuya-mqtt · GitHub

I found it useful to connect the bulb to Smart Life because then, with a debug node connected to the Tuya node, I could determine what dps number controlled what aspect of the bulb.

Mine looked like this;

DPS Number     Command              Values                   Equates To
20             State                true or false            On or Off
21             Mode                 white, colour, scene, music
22             Brightness			10 - 1000	             1 - 100%*10
23             Colour Temperature	0 - 1000	             0 - 100% *10 [~2200K - 6500K] 
24             Colour Data HSV		Hue 0 - 360, 
                                    Saturation 1 - 1000 
                                    Value 0 - 1000           Saturation & Value / 10
26             Time out Value       0 - 86400                Seconds

My bulbs require the Colour Data to be in a hex format. For example a redish colour has the following HSV values

hue = 357
saturation = 100
value = 100

which gives a Tuya value of '016503e803e8'

To change colour you also have to change to Mode, like so
msg.payload = { "multiple": true, "data": { 21: 'colour', 24: '016503e803e8'}}

To change brightness
msg.payload = = {dps: 22, set: 800} Note that a selected Brightness of 1 - 100% has to be multiplied by 10 to give required Tuya value of 10 - 1000

I have a function to convert to and from HSV & Tuya values if you cannot find anything else.

Hope this helps point you in the right direction

That helps a lot, thank you! I can now set it to two different settings depending on another input, which is the important part. Now for the not so important part of seeing if I can make a dashboard control for it.

That would be very much appreciated if you can share it. It does seem like a rather odd way of defining the colour!

Here you go. There are probably better ways but this works for me

Notes:

  • The saturation & value adjustments are included.
  • The functions work with the newer style Tuya 12 character colour values)
  • Some explanations of Tuya colour mention HSB instead of HSV. As far as I can tell they are the same
	/******************************************************************************
	* Description	Converts an HSV colour value to Tuya hexadecimal colour.
	* 
	* 				Assumes Hue in set [0, 360], Saturation and Value in the set [0, 100] and
	* 				returns Tuya colour as 12 character hexadecimal string
	* 
	*
	* @param   {number | Object}     hue            - The hue or an Object. Example {357, 100, 100}
	* @param   {number}              saturation     - The saturation. Not required if Hue as Object
	* @param   {number}              value          - The value. Not required if Hue as Object
	* 
	* @returns {string}             - A 12 character hexadecimal string as used by Tuya devices for colour
	* 
	* Note: To convert RGB colour to Tuya colour use RGBtoHSV() as input
	*/

	function HSVtoTuyaColour(hue, saturation, value) {
		// Note: hue = hue.hue MUST go last
		if (arguments.length === 1) {
			saturation = hue.saturation;
			value = hue.value;
			hue = hue.hue;

		}

		// Increase saturation & value to correct set [0, 1000]
		saturation = saturation * 10;
		value = value * 10;

		return HSVtoHexStr(hue, saturation, value);

	} // End Function HSVtoTuyaColour()


	/******************************************************************************
	* Description	Converts a Tuya hexadecimal colour to an HSV colour .
	* 
	* 				Assumes Tuya colour hexadecimal representation: Hue in set [0, 360], Saturation and Value in the set [0, 1000]
	* 				returns an Object containing HSV hue, saturation and value
	* 
	*
	* @param   {string}	tuyaString	- Tuya colour as 12 character hexadecimal string
	* 
	* @returns {object}           	- The HSV representation of the tuya colour string
	* 
	* Note: To convert Tuya colour to RGB use HSVtoRGB()
	*/

	function TuyaColourToHSV(tuyaString) {
		let hue = hexToDecimal(tuyaString.slice(0, 4), 4);
		let saturation = hexToDecimal(tuyaString.slice(4, 8), 4) / 10;
		let value = hexToDecimal(tuyaString.slice(8, 12), 4) / 10;

		return { hue: hue, saturation: saturation, value: value };

	} // End Function TuyaColourToHSV()


	/******************************************************************************
	* Helper functions for HSVtoTuyaColour. Internal function only
	*/
	/******************************************************************************
	* Description	Converts an HSV colour value to hexadecimal string.
	* 
	* 				Assumes Hue in set [0, 360], Saturation and Value in the set [0, 100]
	* 				returns a 12 digit containing HSV hue, saturation and value
	* 
	*
	* @param   {number}	hue		       - HSV hue 
	* @param   {number}      saturation       - HSV saturation
	* @param   {number}      value	       - HSV value (brightness)
	* 
	* @returns {object}           	- The HSV representation
	* 
	* Note: To convert Tuya colour to RGB use HSVtoRGB()
	*/

	function HSVtoHexStr(hue, saturation, value) {
		let a = decimalToHex(hue, 4);
		let b = decimalToHex(saturation, 4);
		let c = decimalToHex(value, 4);

		return a + b + c;

	} // End Function HSVtoHexStr()

Thank you! I'll carry on playing with that tomorrow.

Sorry for probably being dumb, but I'm struggling to get this to work!

This was my original function that works with a switch and sliders for power, brightness, colour temp etc:

var topic = msg.topic
var payload = msg.payload

msg.payload = { 
    dps : topic,
    set : payload
}

return msg;

I've tried copying in your functions to make this:

var topic = msg.topic
var payload = msg.payload


msg.payload = { 
    dps : topic,
    set : payload
}

if (topic == "24") {
    payload = HSVtoTuyaColour();
}

return msg;

function HSVtoTuyaColour(hue, saturation, value) {
		// Note: hue = hue.hue MUST go last
		if (arguments.length === 1) {
			saturation = hue.saturation;
			value = hue.value;
			hue = hue.hue;

		}

		// Increase saturation & value to correct set [0, 1000]
		saturation = saturation * 10;
		value = value * 10;

		return HSVtoHexStr(hue, saturation, value);
}

function HSVtoHexStr(hue, saturation, value) {
		let a = decimalToHex(hue, 4);
		let b = decimalToHex(saturation, 4);
		let c = decimalToHex(value, 4);

		return a + b + c;

	}

But I'm getting the following error:
ReferenceError: decimalToHex is not defined

Am I missing another helper function for the decimalToHex, or am I just going about it all wrong?

The dashboard colour picker outputs either "hsv(112, 93%, 95%)" as a string, or "{"h":113,"s":0.9299999999999999,"v":0.95,"a":1}" as an object.
Do I also need to try and split that down to just the numerical values?

I also am trying to work out a spreadsheet to figure out how to get a slow fade ~30 seconds full brightness to off, makes my head hurt lol. Frustrating b/c the devices don't support a transition natively in home assistant so i'm trying to go the node-red route as the "scene" example here gives me a slow fade through all the colors

Many apologies I missed a couple of functions;

	/******************************************************************************
	* General Helper Functions
	* Also exposed as general conversion functions
	* 
	* 
	*/

	/** 	
  	*
  	* Description	Converts a decimal number to a Hex string
  	*				Removes any leading #. Checks that there are either 3 or 6 characters and tries to convert to a number
  	*               which results in NaN if false. Returns true or false
  	*
  	* @param 	{number}	decimal		- Decimal number       		Example 16777215
  	* @param 	{number}  	padCount    - Number of 0 used to pad Hex string with (defaults to each hex number having 2 characters. 
  	*
  	* @returns	{string} 		    	- Return a Hex string       Example 'FFF' becomes '0F0F0F'
  	*
  	*/

	function decimalToHex(decimal, padCount = 2) {
		return (decimal + Math.pow(16, padCount)).toString(16).slice(-padCount).toUpperCase();

	} // End Function decimalToHex()


	/**	
  	*
  	* Description	Converts a Hex string to a numeric
  	*				Tries to convert hex value to a number which results in NaN if false.
  	*
  	* @param	{String}	hexString   - Hex string					Example "#FFFFFF"
  	* @param	{Number}  	padCount    - Number of 0 used to pad Hex string with Example 'FFF' becomes '0F0F0F'
  	*
  	* @returns	{Number} 				- Return decimal number       Example 16777215
  	*
  	*/

	function hexToDecimal(hexString, padCount = 2) {
		if (isHexString(hexString)) {
			return parseInt(hexString.slice(-padCount), 16);

		} else {
			return undefined;

		}

	} // End Function hexToDecimal()

From the dashboard picker use the Object {"h":113,"s":0.9299999999999999,"v":0.95,"a":1}. (current value as an Object) The function call is then something like;

const colourValue = msg.payload

HSVtoTuyaColour(colourValue.h, colourValue.s * 100, colourValue.v * 100)

The "a" property can be ignored as not relevant to colour lights. s & v have to be multiplied by 100 to get them to % in range 0 - 100

To activate the example 'scene' you need to send;

const sceneValue = '010b0a02000003e803e8000000000b0a02007603e803e8000000000b0a0200e703e803e800000000'

msg.payload = { "multiple": true, "data": { 21: 'scene', 25: sceneValue } }

and to get back to 'white' just send

msg.payload = { 21: 'white' }

And thank you for pointing me at the 'scene' web page as previously I was struggling to determine how a scene string was constructed.

That's done it. Thank you!