How to trigger an event on another device (possibly virtual?)

Hi - I am trying to trigger an event on another device from within my SmartApp using when a virtual (simulated) switch is triggered. The virtual switch gets triggered fine (as it happens by an IFTTT rule) and then the associated event fires with :-

sendEvent(name: "themotion", value: "active")

which is tied to

section("Which Motion Sensor to activate:") {
	input "themotion", "capability.motionSensor", required:true, title: "Where?"
}

I am hoping the sensor will act as if it has been triggered by physical motion but this does not happen (with nothing in the “Recently” tab).

Any thoughts why this is not working?
Thanks.
James.

1 Like

Try this:

sendEvent(themotion.deviceNetworkId, [name: "motion", value: "active"])

This version tells sendEvent first what device you’re triggering. Then the event map which includes name of the capability attribute that is changing and the value it’s changing to. If this event doesn’t change the state of “motion” on the device, it won’t show in Recently unless you force it an additional map parameter isStateChange: true

Be aware if the motion sensor is a real device you could get very odd behavior depending on how often it reports its status.

Seems to be throwing an error on the new line when using a simulated motion sensor

java.lang.NullPointerException: Cannot get property ‘device’ on null object

sendEvent(themotion.deviceNetworkId, [name: "motion", value: "active"])

I am assuming “deviceNetworkId” is a property on this object and not something hard coded?!

Yes, it’s a property of the device object. ST automatically assigns them when a device pairs, but for simulated devices, you might have needed to input one yourself. Check the My Devices tab of the IDE and you will see a column for Device Network Id to see if it’s populated for this simulated device. I’m betting it’s blank and you can manually update it by clicking the device, then Edit at the bottom of the device screen.

I had considered that and already checked, unfortunately it does have a network device id.

Just in case it was linked to the simulated motion sensor I published the code to my device and it doesn’t work as hoped either for a physical motion sensor or the simulated version.

http://docs.smartthings.com/en/latest/ref-docs/smartapp-ref.html#sendevent

I was trying to follow this part of the docs for using sendEvent from a SmartApp. When I took out the .deviceNetworkId I got a different error that seems to imply you can only do this for Child Devices of the SmartApp. (By the way, I am using this format of sendEvent using a child device with the .deviceNetworkId part in a SmartApp. And I believe you can see it in the Hue Connect SmartApp if you want an example.)

Since it’s a simulated motion sensor, you could use a custom device handler with a new command that just sends the ‘Active’ event for the device, then call that custom command from your SmartApp.

Thanks for your input again, still trying to work my way through the samples/docs as I have only had SmartThings for a few days.

I am happy to try and trigger the physical motion sensor instead of the simulated (virtual) one if that makes life easier - would you have any guidance to enable this approach instead?

That was silly of me! I thought you would need a custom device type, but it looks like the Simulated Motion Sensor already has extra commands. You should be able to just do:

themotion.active()

That should trigger the active() command in the simulated motion sensor. The physical motion sensor won’t have that custom command, so you’ll want to use the simulated one.

1 Like

Fantastic, that did it - thank you!

Where did you find the details for the methods the simulated motion sensor object supports? (it should have been something I would have found if I had known where to look).

Thanks again!

try this app…

2 Likes

Mike’s app is a really cool way to check devices you don’t have the code for. You can find the code for most stock ST devices including the simulated motion sensor on their public github. Sometimes the version on github isn’t the same as the one in production.

1 Like

Thanks for the help given here - it’s a great community!

With the help from here, I have completed the app to my requirements now and here is a link should anyone else be remotely interested in this functionality - it’s very simple (not a bad thing sometimes with code!) and does the job well.

Any chance on getting step by step instructions for this, or a video?