If statement inside metadata

Is it possible to use if statements in the metadata { } ?

Nope (at least not for capabilities...seems some have had luck with at least attributes below).

Damn... figured that.

It didn't through a error when saving it so I was hopeful for a solution.

@bravenel Is this a possibility at some point ?

I've done it in a driver and it worked? Or at least I think I have it's just a basic IF

It did ?? Show me a code snipet ?

I tried this and no go....

    if (rgbwplus)
    command "setEffect", [[name:"Effect to use (0=OFF)", type: "NUMBER", description: "Effect to use (0=OFF)", constraints: ["0","1","2","3","4","5","6",]]]
    command "setEffect", [[name:"Effect to use (0=OFF)", type: "NUMBER", description: "Effect to use (0=OFF)", constraints: ["0","1","2","3"]]]
 if (settingEnable) input name: "param1", type: "number", range: "0..65535", required: true, defaultValue: "0",
            title: "Parameter No. 1 - Input 1 Alarm Cancellation Delay. \n" +
                   "Additional delay after an alarm from input 1 has ceased.\n" +
                   "Time in seconds to delay the ceasing event.\n" +
                   "Default value: 0."

Well that's interesting... Your code allows the use of hiding the preferences correct ?

But that isn't inside the metadata { } that's preferences { }


Here is one I used in the past. Dave

	def deviceType() { return "Plug-Switch" }
//	def deviceType() { return "Dimming Switch" }	
//	==========================================================

    metadata {
    	definition (name: "TP-Link/Kasa ${deviceType()}", 
        			namespace: "davegut", 
                    author: "Dave Gutheinz") {
    		capability "Switch"
            capability "Actuator"
    		capability "Refresh"
    		if (deviceType() == "Dimming Switch") {
    			capability "Switch Level"

    	if (getDataValue("installType") != "Kasa Account")  {
    	    preferences {
    			input ("device_IP", "text", title: "Device IP (Hub Only, NNN.NNN.N.NNN)")
    			input ("gateway_IP", "text", title: "Gateway IP (Hub Only, NNN.NNN.N.NNN)")
1 Like

which is within metadata{ Preferences{} } not a developer just tinkerer so assumed it counted as it's within?

1 Like

Where are you defining that at ?

I place it before the metadata section, for convenience. I used this to use the same code for either a normal switch or a dimming switch. Just mark out the line I did not want.

Technically yes -- I was looking for a a command solution though

Ahh so it's not based on a preference setting.... that's what I'm looking for.

Be sneaky. Set a data value to a default on install. Have an option in preferences (i.e., noMenu) that in the updated method does an updateDataValue.

I define it...

input name: "rgbwplus", type: "bool", title: "What RGBW device ?", description:"RGBW2 or RGBW+", defaultValue: false

Then tried and didn't work

    if (rgbwplus == true)
    command "setEffect", [[name:"Effect to use (0=OFF)", type: "NUMBER", description: "Effect to use (0=OFF)", constraints: ["0","1","2","3","4","5","6",]]]
    command "setEffect", [[name:"Effect to use (0=OFF)", type: "NUMBER", description: "Effect to use (0=OFF)", constraints: ["0","1","2","3"]]]

Ahh ok I'll try that

code snippet from wx-ApiXU-Driver...

metadata    {
 	definition (name: "wx-ApiXU-Driver", namespace: "csteele", author: "bangali, csteele", importUrl: "https://raw.githubusercontent.com/HubitatCommunity/wx-ApiXU/master/wx-ApiXU-Driver.groovy")  {
 		capability "Actuator"
 		capability "Sensor"
 		capability "Polling"
 		capability "Illuminance Measurement"
 		capability "Temperature Measurement"
 		capability "Relative Humidity Measurement"
 		capability "Pressure Measurement"
 		capability "Ultraviolet Index"
			k, v -> if (v.typeof) attribute "${k}", "${v.typeof}"

That happens to populate "attribute" -- but "command should work too.

1 Like

Use a getDataValue with a default rebwplus = false (your default).

Add a clause in method updated:

if (rgbwplus == true) { updateDataValue("rgbwplus", true) }
else {updateDataValue("rgbwplus", false) }

On initial install, the command will be the false value above. If you update preferences, when redisplayed, it will be the if true value.

1 Like

Note that the if statement in the metadata would be
if (getDataValue("rgbwplus") == true) ........

1 Like