Trigger SmartThings scene from MacroDroid [Please scroll to see Sept 2023 updates]

RSF

Well-known member
Steps to set up MacroDroid to trigger a scene in SmartThings:
  1. Go to the Samsung SmartThings API "Personal Access Tokens" page, log in using your SmartThings user ID and password, and click "Generate New Token". Give your token a name, and the permissions you need (I just gave it everything listed here*, but you could be more selective if you want). and make note of the token value.
  2. Create a global variable in MacroDroid called smartthings_auth_token and set its value to "Bearer " plus the token value above (noting that the word Bearer has to have a space after it, before the token value)
  3. Import the attached action blocks -- renaming each from *.txt to *.ablock (the forum doesn't allow for .ablock files to be uploaded)
  4. Edit the "Get Locations" action block. Tap the HTTP Request (Get) action (the only action in the block), then tap "Test action". Check the value of the http_response_code and http_response variables. The response code should be 200, and the response should be a JSON structure showing your account's location(s). Make note of the location ID you want to control.
    (If your response code was 401 it means there's something wrong with your personal access token; double-check that you copied it into the global variable prefixed with "Bearer " correctly.)
  5. Edit the first action in the "Trigger Scene" action block to set the value of the location_id variable to the location ID you got in the step above. Save the action block.
  6. Call that "Trigger Scene" action block (from a macro, or another action block), passing in the name of a SmartThings scene you want to trigger. SmartThings should then run that scene... The action block returns the http_response_code as well, which should be 200 if all went well.
If you have multiple houses/locations, of course you could expand the "Get Locations" action block to take the name of a house as an argument and return its ID, vs. hard-coding the ID in the "Trigger Scene" action block.

The full SmartThings API documentation is here. You could define new scenes, control devices directly, etc.

* - i.e.: r:locations:*, x:rules:*, r:rules:*, r:scenes:*, x:devices:*, l:devices, w:rules:*, w:locations:*, r:devices:*, w:devices:*, x:locations:*, x:scenes:*
 

Attachments

  • SmartThings-_Get_Locations.txt
    1.9 KB · Views: 227
  • SmartThings-_Trigger_scene.txt
    15.7 KB · Views: 207

MySmartHome

New member
Thanks a lot it's works very well!! Thank you for the great how to! step by step easy step to follow :) also thanks for action block you made and share with us.

Question is there a way to just call device switch turn off/on instead of call scenes? Also it's possible to trigger like battery status of door sensor or status of open/close door state like on Xiaomi mijia in SmartThings.. I'm already using a lot SharpTools integration but since the will shutdown groovy IDE soon it's will no longer working anymore... (I know SharpTools team work on it to works with new transition as Edge driver).
 

RSF

Well-known member
Question is there a way to just call device switch turn off/on instead of call scenes? Also it's possible to trigger like battery status of door sensor or status of open/close door state like on Xiaomi mijia in SmartThings.. I'm already using a lot SharpTools integration but since the will shutdown groovy IDE soon it's will no longer working anymore... (I know SharpTools team work on it to works with new transition as Edge driver).
Yes, the SmartThings API definitely includes that. There are many, many options for controlling devices. The API device-control documentation can be found here (with details on the commands here). You'll need to get a device's device ID via the List Devices call (similar to getting the location ID in the example Action Blocks above), and then use that in subsequent device-control calls. I haven't created any Action Blocks or Macros with those schemes, since I have a very simple set of scenes that suffice for me, but the same general scheme of HTTP POST's or GET's should work fine for controlling devices. Similarly, there are a lot of calls to get devices' status.
 

eshroom

New member
@RSF - Do you think it's possible to do things the other way round? When a certain thing in Smartthings is triggered (e.g. motion sensor or multisensor) macrodroid makes an alarm sound?
 

RSF

Well-known member
@eshroom - Couple possible approaches:
  1. The official way is to subscribe to events via this API, involving creating a SmartApp. It sounds powerful, but somewhat complicated. I've never tried that.
  2. Quick semi-hack: Create a routine in SmartThings to send your phone a notification ...
    Screenshot 2022-11-27 11.22.17 AM.png

    ... and then create a macro in MacroDroid triggered by that notification from the SmartThings app:
    Screenshot 2022-11-27 11.30.42 AM.png

    You could also send your phone a text vs. a notification, the SmartThings routine, and trigger off that, if you prefer.
 

Mcrazy

Member
Hi everyone,
Thanks for the great work!
Does anyone know how to query the status of a Smart Plug about whether it is On or Off?
 

RSF

Well-known member
See attached action blocks for possible methods to get the status of switches (or lamps). I can't say this scheme will work with all devices -- SmartThings supports a wide array of devices and their status may be returned in different ways. (For example, a refrigerator or clothes dryer may report being on or off differently than a plug)

You'll want to call the "SmartThings: Get Device Status" action block, passing in the name of your device, and (optionally) a location ID if you have the same device name in multiple locations. That Action Block calls the "SmartThings: Get Device ID" action block to get the internal ID of the device from the name.

These files' extensions need to be renamed from .txt to .ablock after downloading and before importing; the forum still doesn't allow action blocks to be posted directly.

Also: this scheme needs the same smartthings_auth_token global variable to be created and set as described in the original post above.

Lastly, for performance reasons, note that you could edit the "SmartThings: Get Device ID" action block to re-use the location name/ID info after its first use, if this is something you'll call often.
 

Attachments

  • SmartThings-_Get_Device_ID.txt
    14.5 KB · Views: 36
  • SmartThings-_Get_Device_Status.txt
    31.5 KB · Views: 30
  • Test_SmartThings_Get_Status.txt
    35.1 KB · Views: 29

Mcrazy

Member
See attached action blocks for possible methods to get the status of switches (or lamps). I can't say this scheme will work with all devices -- SmartThings supports a wide array of devices and their status may be returned in different ways. (For example, a refrigerator or clothes dryer may report being on or off differently than a plug)

You'll want to call the "SmartThings: Get Device Status" action block, passing in the name of your device, and (optionally) a location ID if you have the same device name in multiple locations. That Action Block calls the "SmartThings: Get Device ID" action block to get the internal ID of the device from the name.

These files' extensions need to be renamed from .txt to .ablock after downloading and before importing; the forum still doesn't allow action blocks to be posted directly.

Also: this scheme needs the same smartthings_auth_token global variable to be created and set as described in the original post above.

Lastly, for performance reasons, note that you could edit the "SmartThings: Get Device ID" action block to re-use the location name/ID info after its first use, if this is something you'll call often.
Thank you very much for your help!
I'll try to set it up tonight.
 

Mcrazy

Member
See attached action blocks for possible methods to get the status of switches (or lamps). I can't say this scheme will work with all devices -- SmartThings supports a wide array of devices and their status may be returned in different ways. (For example, a refrigerator or clothes dryer may report being on or off differently than a plug)

You'll want to call the "SmartThings: Get Device Status" action block, passing in the name of your device, and (optionally) a location ID if you have the same device name in multiple locations. That Action Block calls the "SmartThings: Get Device ID" action block to get the internal ID of the device from the name.

These files' extensions need to be renamed from .txt to .ablock after downloading and before importing; the forum still doesn't allow action blocks to be posted directly.

Also: this scheme needs the same smartthings_auth_token global variable to be created and set as described in the original post above.

Lastly, for performance reasons, note that you could edit the "SmartThings: Get Device ID" action block to re-use the location name/ID info after its first use, if this is something you'll call often.
Hi all,
I tested the Action Blocks. Works perfectly, great job!
Unfortunately, it cannot return the status of my devices (Tuya Zigbee). This is not a shortcoming of Action Blocks. SmartThings also only returns the Scene status, whether it is active or inactive. The Tuya app includes a Widget that would actually show that the Smart Plug is on/off, but it is unstable.
Thanks again for the great work!
 

RSF

Well-known member
Sorry to hear that. You might want to step through the "SmartThings: Get Device Status" action block action-by-action, and inspect the status_dict dictionary in detail. That's what comes back from SmartThings for a device's status. I wonder if the Tuya device has its status buried in some other dictionary entry. I don't have any of those devices, so cannot test it myself.
 

Mcrazy

Member
Sorry to hear that. You might want to step through the "SmartThings: Get Device Status" action block action-by-action, and inspect the status_dict dictionary in detail. That's what comes back from SmartThings for a device's status. I wonder if the Tuya device has its status buried in some other dictionary entry. I don't have any of those devices, so cannot test it myself.
Thanks for the advice, but it makes no sense for the following reasons:
Tuya devices cannot be added directly to SmartThings.
Only Scenes created in the Tuya app are seen by SmartThings when I link the two accounts.
Therefore, SmartThings can only send commands to the Scenes that are switched On/Off, but cannot retrieve the status.
 

tstancat

New member
In the last day or so, my scenes stopped being triggered and I'm getting the popup message "no such scene". I haven't made any changes to the scene names. Any suggestions?
 

RSF

Well-known member
Out of town for about a week, so can't access devices to check, but will check on it when I return. Must be a change in SmartThings' API...
 

tstancat

New member
I was able to resolve the issue. For some reason, there was an if statement in the Trigger Scene action block that stopped working. It's the if statement just before the execute scene command that checks if entry_scene_name equals scene_name. I replaced the if statement with another if statement with the exact same conditions and it started to work normally again. See attachment for a screen shot of the configure screen for the new if statement. Be sure to change the type to "string".
 

Attachments

  • Screenshot_20230920_160129_MacroDroid.jpg
    Screenshot_20230920_160129_MacroDroid.jpg
    329.6 KB · Views: 24
Top