# Trigger an attribute write in a custom device type

**URL:** <https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052>\
**Category:** Writing Device Types\
**Created:** [October 24, 2015, 4:00pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052 "2015-10-24T16:00:58Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnR](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/johnr/32/19702_2.png) [@JohnR](https://community.smartthings.com/u/JohnR)\
**Post date:** [October 24, 2015, 4:00pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/1 "2015-10-24T16:00:58Z")

</div>

I’m working on a custom device lets say it is a color LED. My custom device type uses the color control and sets the LED’s color everything works great. When I reboot my color LED its default color is white and it is out of sync with what the color control thinks its color is. I want to set my recently rebooted color LED to the current color in the color control. My color LED sends a custom command to my device type letting it know it has been rebooted. I’m able to detect the custom command in my parse method. I then use the command:

> cClr.hex = device.currentState(“color”)?.value

to get the current color and then I pass it to the following setColor method:

> def setColor(value) {  
> log.trace"setColor = ${value}"  
> def cx = value.hex  
> cx = cx.substring(1, cx.length()) // Remove # from front of hex value.hex string  
> def cmds =   
> cmds \<\< “st wattr 0x${device.deviceNetworkId} 0x38 0x0008 0x400 0x23 {${cx}}” // Send new RGB Color value write attribute 0x0400  
> cmds  
> }

The setColor method creates an array of cmds and I can’t figure out how to execute them?? What am I missing? I know I’m over looking something obvious.

Thanks

---

<div class="post-metadata">

**Author:** ![bravenel](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/bravenel/32/13223_2.png) [@bravenel](https://community.smartthings.com/u/bravenel)\
**Post date:** [October 24, 2015, 4:08pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/2 "2015-10-24T16:08:29Z")

</div>

Just a wild guess, but what about `sendEvent( ... )`?

---

<div class="post-metadata">

**Author:** ![JohnR](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/johnr/32/19702_2.png) [@JohnR](https://community.smartthings.com/u/JohnR)\
**Post date:** [October 24, 2015, 4:22pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/3 "2015-10-24T16:22:53Z")

</div>

Yep that was the first thing i tried. Here is the error:

 ![](https://global.discourse-cdn.com/smartthings/original/3X/8/b/8bc3cb1fe69adb9e84f8a68d6b6116b72b36664b.JPG)

Is it my syntax? The error talks about an onEvent and setParent option?

---

<div class="post-metadata">

**Author:** ![bravenel](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/bravenel/32/13223_2.png) [@bravenel](https://community.smartthings.com/u/bravenel)\
**Post date:** [October 24, 2015, 4:42pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/4 "2015-10-24T16:42:36Z")

</div>

What did your sendEvent look like? It should be the same as using the device type to set the color, I think.

---

<div class="post-metadata">

**Author:** ![JohnR](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/johnr/32/19702_2.png) [@JohnR](https://community.smartthings.com/u/JohnR)\
**Post date:** [October 24, 2015, 5:02pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/6 "2015-10-24T17:02:47Z")

</div>

Here is the custom Device type:

> <https://github.com/JohnRucker/Nuts-Volts/blob/master/devicetypes/nutsvolts/push-button-with-color.src/push-button-with-color.groovy>

---

<div class="post-metadata">

**Author:** ![JohnR](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/johnr/32/19702_2.png) [@JohnR](https://community.smartthings.com/u/JohnR)\
**Post date:** [October 24, 2015, 5:06pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/7 "2015-10-24T17:06:27Z")

</div>

In the parseOnOff method look for this command:

> sendEvent(setColor(cClr))

The value I send is based on the return of the setColor method.

See I wonder if this is just not going to work because I’m triggering the event from the ZigBee device not from my iPhone. Usually the setColor method is called as a result of me selecting a color from the color control on my iPhone. But in this case I’m calling it from the parse method??

---

<div class="post-metadata">

**Author:** ![bravenel](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/bravenel/32/13223_2.png) [@bravenel](https://community.smartthings.com/u/bravenel)\
**Post date:** [October 24, 2015, 5:20pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/8 "2015-10-24T17:20:20Z")

</div>

Have you tried just doing that sendEvent from within the device type? It shouldn’t matter where the event came from, the device type should process it the same way. You might want to put in `isStateChange: true` as well.

```
sendEvent(name: setColor, value: cClr, isStateChange: true)

```

I’m not at all sure about that, just guessing. I had a completely unrelated device type, and that’s how I set attribute values.

---

<div class="post-metadata">

**Author:** ![JohnR](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/johnr/32/19702_2.png) [@JohnR](https://community.smartthings.com/u/JohnR)\
**Post date:** [October 24, 2015, 6:06pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/9 "2015-10-24T18:06:30Z")

</div>

Well your getting me a lot closer and your example makes sense. When I call it this way i no longer get an error but the method doesn’t get called?? I tried it with the isStateChange: true and with out it.

---

<div class="post-metadata">

**Author:** ![bravenel](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/bravenel/32/13223_2.png) [@bravenel](https://community.smartthings.com/u/bravenel)\
**Post date:** [October 24, 2015, 6:26pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/10 "2015-10-24T18:26:22Z")

</div>

It’s something about the interaction with the tile definition. Perhaps experiment by sending switch events, to see if you can get those to work. I’m not familiar with the new multi-attribute tile stuff, and I suspect your problem is a nit about how to address it.

---

<div class="post-metadata">

**Author:** ![JohnR](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/johnr/32/19702_2.png) [@JohnR](https://community.smartthings.com/u/JohnR)\
**Post date:** [October 24, 2015, 6:33pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/11 "2015-10-24T18:33:57Z")

</div>

I have tried sending the on() event same thing happens. It executes the

> sendEvent(name: on, isStateChange: true)

No errors but just doesn’t call it.

To be clear all these commands work just fine if I call them from the iPhone by tapping on the color value, level value etc… What I’m trying to do now is call them from within the parse method.

I guess I could write a SmartApp that listens for the “justBooted” event and then calls all these commands.

---

<div class="post-metadata">

**Author:** ![bravenel](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/bravenel/32/13223_2.png) [@bravenel](https://community.smartthings.com/u/bravenel)\
**Post date:** [October 24, 2015, 7:09pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/12 "2015-10-24T19:09:47Z")

</div>

That’s not right. It would be

sendEvent(name: switch, value: on, …

---

<div class="post-metadata">

**Author:** ![JohnR](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/johnr/32/19702_2.png) [@JohnR](https://community.smartthings.com/u/JohnR)\
**Post date:** [October 24, 2015, 8:34pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/13 "2015-10-24T20:34:44Z")

</div>

I tried that syntax same thing fires event no error and doesn’t call my “on” method.

Doesn’t

> sendEvent(name: “switch”, value: “on”)

just set the switch attribute equal to the string “on”?

I want to call this method called on:

> // Commands  
> def on() {  
> log.info “on cmd sent”  
> “st cmd 0x${device.deviceNetworkId} 1 6 1 {}”  
> }

Shouldn’t the command be something like device.switch.on() something like that?

---

<div class="post-metadata">

**Author:** ![Sticks18](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Sticks18](https://community.smartthings.com/u/Sticks18)\
**Post date:** [October 24, 2015, 8:53pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/14 "2015-10-24T20:53:38Z")

</div>

Unfortunately, you can’t call a command from parse(). At least that was what I was told (see below quote). Parse is meant to create events, but events themselves don’t take action on a device, only change information displayed to the user in the devicetype. I think you’ll need a SmartApp to initiate the action.

> I am not sure why you are trying to trigger commands from the parse method. The parse method does not expect the return to be a command.

---

<div class="post-metadata">

**Author:** ![tgauchat](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tgauchat/32/49516_2.png) [@tgauchat](https://community.smartthings.com/u/tgauchat)\
**Post date:** [October 24, 2015, 9:04pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/15 "2015-10-24T21:04:07Z")

</div>

> [@Sticks18](#):
>
> Unfortunately, you can’t call a command from parse().

Ummm… Isn’t the Command in scope of the Device Handler?

Just:

```groovy
def parse... { 
... 
   on() 
...
}

```

?

---

<div class="post-metadata">

**Author:** ![Sticks18](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Sticks18](https://community.smartthings.com/u/Sticks18)\
**Post date:** [October 24, 2015, 9:11pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/16 "2015-10-24T21:11:44Z")

</div>

I guess my statement was misleading. By command, I mean sending out a command, such as a Zigbee command, to the device. You can call methods and return values, but it won’t trigger an action to the device.

If the on() command simply creates an ‘on’ event, you’re all good; but if you actually want it to turn on the device by sending a command out, it won’t do it. It will be ignored.

---

<div class="post-metadata">

**Author:** ![tgauchat](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tgauchat/32/49516_2.png) [@tgauchat](https://community.smartthings.com/u/tgauchat)\
**Post date:** [October 24, 2015, 9:16pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/17 "2015-10-24T21:16:15Z")

</div>

> [@Sticks18](#):
>
> If the on() command simply creates an ‘on’ event, you’re all good; but if you actually want it to turn on the device by sending a command out, it won’t do it. It will be ignored.

Sure… But why the heck would you have an `on()` method in your Device Handler that doesn’t actually have the code in it to physically affect the device approximately??

---

<div class="post-metadata">

**Author:** ![Sticks18](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Sticks18](https://community.smartthings.com/u/Sticks18)\
**Post date:** [October 24, 2015, 9:23pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/18 "2015-10-24T21:23:28Z")

</div>

I’m not saying you would. Only that calling on() from your parse() method won’t physically affect the device because ST will ignore any outgoing commands to the actual device.

---

<div class="post-metadata">

**Author:** ![tgauchat](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tgauchat/32/49516_2.png) [@tgauchat](https://community.smartthings.com/u/tgauchat)\
**Post date:** [October 24, 2015, 9:27pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/19 "2015-10-24T21:27:07Z")

</div>

> [@Sticks18](#):
>
> Only that calling on() from your parse() method won’t physically affect the device because ST will ignore any outgoing commands to the actual device.

Not correct (I think).  
If we have (pseudo code)

```auto
def on() {
   zigbeeSend( address, "on" );
}

```

Then `on()` should be callable and functioning anywhere in the scope of the Device Handler Instance, including inside its `parse()` method.

**NB** : I understand the _theoretical_ architecture of SmartThings a lot, but have much less practical coding experience than other folks here. But that knowledge of the theory has often led to eureka moments and further education in both directions.

---

<div class="post-metadata">

**Author:** ![Sticks18](https://avatars.discourse-cdn.com/v4/letter/s/b487fb/32.png) [@Sticks18](https://community.smartthings.com/u/Sticks18)\
**Post date:** [October 24, 2015, 9:34pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/20 "2015-10-24T21:34:37Z")

</div>

I think it should be too, but it doesn’t work and the quote I posted is from ST. Testing that I believe @rpress did showed that the same is true for the updated() method. I can only attest to Zigbee commands because that’s what I’ve worked with, but they simply don’t fire.

It might be a way to prevent loops within a devicetype.

(One exception seems to be for the Enroll Response for Zigbee sensors, which is triggered by a request from the device.)

---

<div class="post-metadata">

**Author:** ![tgauchat](https://yyz2.discourse-cdn.com/smartthings/user_avatar/community.smartthings.com/tgauchat/32/49516_2.png) [@tgauchat](https://community.smartthings.com/u/tgauchat)\
**Post date:** [October 24, 2015, 9:44pm UTC](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052/21 "2015-10-24T21:44:14Z")

</div>

> [@Sticks18](#):
>
> I think it should be too, but it doesn’t work and the quote I posted is from ST. Testing that I believe @rpress did showed that the same is true for the updated() method. I can only attest to Zigbee commands because that’s what I’ve worked with, but they simply don’t fire.

OK. Well… this is definitely worth me confirming with a test device and some log.debug, etc., commands to confirm.

Indeed, it is certainly understandable that this would be the behavior, even if frustrating … while inside parse(), the communication layer is in “receive” mode (or something), and thus “sending” is prohibited or impossible. Though that could vary by RF protocol.

If this is the case, then I, personally, would first want to understand the bigger “problem we’re trying to solve” before trying to hack around the restriction.

In other words, trying to hack the restriction by using `sendEvent()` or some other clever coding, could be a waste of time, _when we really ought to be putting some eyes on the actual end-result requirements first._

[Next page](https://community.smartthings.com/t/trigger-an-attribute-write-in-a-custom-device-type/27052.md?page=2)
