Rule Engine Query SmartThings API JSON

@josh would it be possible to add a way for the Rule Engine to query the full status of a SmartThings device using SharpTools existing OAuth authorization?

Essentially, I’d like to execute the equivalent of:

GET https://api.smartthings.com/v1/devices/{deviceId}/status

And receive the complete JSON response, including all components, capabilities and attributes, rather than just the individual attributes SharpTools currently exposes.

Ideally, the response could be stored in a SharpTools global variable for further processing or forwarding through an HTTP action.

The goal is to query a device on demand without needing to manage a separate SmartThings API token and the application required to keep it refreshed.

I’ve cut down almost all of my SmartThings usage (projected to be under 1%) by moving most things to Home Assistant. However, there are still some status fields returned by the SmartThings API that aren’t available through local integrations. It appears in the latest blog communication that even using a personal token to access the SmartThings API may be subject to the new developer ‘tax’.

This is really just a nice-to-have, not a necessity. If you see a lot of complications with implementing something like this, I completely understand.

2 Likes

I second this request. :folded_hands:

What’s your use case?

The complexity of the Samsung appliances. There are a lot of ‘supportXX’ arrays that have configuration information that is not available in SharpTool attributes.

Thanks! Can you walk us through one specific end-to-end example? Which device is it, which exact values do you need from the status response, and what would you do with them once you have them (show them somewhere, use them in a rule, send them to Home Assistant)? A concrete example will make it a lot easier to see what you’re trying to do and whether there’s already a way to get there.

Thanks Sam. In this case the only way to get the input settings on Samsung TVs is through the SmartThings API. There are also arrays that hold the configuration of those inputs.

I can get the individual variables with the current capabilities (it’s a little awkward of course) but SharpTools today doesn’t give you access to array attributes or a way to store them. Might be able to stringify them into text.

Here is the YAML I’m using today to do this:

living_room_tv.yaml


rest_command:
living_room_tv_on:
url: !secret living_room_tv_on_url
method: GET

rest:

resource: “https://api.smartthings.com/v1/devices/{deviceId}/status”
method: GET
headers:
Authorization: !secret smartthings_read_only_bearer_token
Accept: “application/json”

scan_interval: 86400

sensor:

name: “Living Room TV SmartThings”
unique_id: living_room_tv_smartthings

value_template: >
{{ value_json.components.main.switch.switch.value | default(‘unknown’) }}

json_attributes_path: “$.components.main”

json_attributes:

samsungvd.mediaInputSource

samsungvd.pictureMode

custom.picturemode

samsungvd.soundMode

custom.soundmode

tvChannel

samsungvd.firmwareVersion

ocf

template:

sensor:

name: “Living Room TV Source”
unique_id: living_room_tv_source

state: >
{% set data = state_attr(
‘sensor.living_room_tv_smartthings’,
‘samsungvd.mediaInputSource’
) %}

{% if data and data.inputSource is defined %}
{% set current = data.inputSource.value %}
{% set sources = data.supportedInputSourcesMap.value | default(
) %}
{% set match = sources
| selectattr(‘id’, ‘equalto’, current)
| map(attribute=‘name’)
| list %}

{{ match[0] if match else current }}

{% else %}
unknown
{% endif %}

attributes:
switch: >
{% if is_state(‘media_player.living_room_tv’, ‘off’) %}
off
{% elif is_state(‘media_player.living_room_tv’, ‘unavailable’) %}
off
{% elif is_state(‘media_player.living_room_tv’, ‘unknown’) %}
off
{% else %}
on
{% endif %}

volume: >
{% set volume = states(
‘sensor.living_room_soundbar_volume’
) | float(0) %}
{{ (volume * 100) | round(0) | int }}

volume_unit: “%”

mute: >
{% set muted = state_attr(
‘sensor.living_room_soundbar_volume’,
‘is_volume_muted’
) %}
{% if muted == true %}
muted
{% else %}
unmuted
{% endif %}

input_source: >
{% set data = state_attr(
‘sensor.living_room_tv_smartthings’,
‘samsungvd.mediaInputSource’
) %}

{{ data.inputSource.value
   if data and data.inputSource is defined
   else 'unknown' }}

input_source_name: >
{% set data = state_attr(
‘sensor.living_room_tv_smartthings’,
‘samsungvd.mediaInputSource’
) %}

{% if data and data.inputSource is defined %}
  {% set current = data.inputSource.value %}
  {% set sources = data.supportedInputSourcesMap.value | default([]) %}
  {% set match = sources
       | selectattr('id', 'equalto', current)
       | map(attribute='name')
       | list %}

  {{ match[0] if match else current }}
{% else %}
  unknown
{% endif %}

picture_mode: >
{% set samsung = state_attr(
‘sensor.living_room_tv_smartthings’,
‘samsungvd.pictureMode’
) %}
{% set custom = state_attr(
‘sensor.living_room_tv_smartthings’,
‘custom.picturemode’
) %}

{% if samsung and samsung.pictureMode is defined
      and samsung.pictureMode.value is not none %}
  {{ samsung.pictureMode.value }}
{% elif custom and custom.pictureMode is defined
      and custom.pictureMode.value is not none %}
  {{ custom.pictureMode.value }}
{% else %}
  unknown
{% endif %}

sound_mode: >
{% set samsung = state_attr(
‘sensor.living_room_tv_smartthings’,
‘samsungvd.soundMode’
) %}
{% set custom = state_attr(
‘sensor.living_room_tv_smartthings’,
‘custom.soundmode’
) %}

{% if samsung and samsung.soundMode is defined
      and samsung.soundMode.value is not none %}
  {{ samsung.soundMode.value }}
{% elif custom and custom.soundMode is defined
      and custom.soundMode.value is not none %}
  {{ custom.soundMode.value }}
{% else %}
  unknown
{% endif %}

tv_channel: >
{% set data = state_attr(
‘sensor.living_room_tv_smartthings’,
‘tvChannel’
) %}

{{ data.tvChannel.value
   if data and data.tvChannel is defined
   else '' }}

tv_channel_name: >
{% set data = state_attr(
‘sensor.living_room_tv_smartthings’,
‘tvChannel’
) %}

{{ data.tvChannelName.value
   if data and data.tvChannelName is defined
   else '' }}

firmware_version: >
{% set data = state_attr(
‘sensor.living_room_tv_smartthings’,
‘samsungvd.firmwareVersion’
) %}

{{ data.firmwareVersion.value
   if data and data.firmwareVersion is defined
   else 'unknown' }}

model: >
{% set data = state_attr(
‘sensor.living_room_tv_smartthings’,
‘ocf’
) %}

{{ data.mnmo.value
   if data and data.mnmo is defined
   else 'unknown' }}

input_boolean:
living_room_tv_source_refresh:
name: “Living Room TV Source Refresh”
icon: mdi:refresh

automation:

alias: “Living Room TV - Turn On”
id: living_room_tv_turn_on
mode: restart

triggers:

trigger: samsungtv.turn_on
entity_id: media_player.living_room_tv

actions:

action: rest_command.living_room_tv_on

action: wake_on_lan.send_magic_packet
data:
mac: “28:07:08:9E:AF:42”

delay:
milliseconds: 500

action: wake_on_lan.send_magic_packet
data:
mac: “28:07:08:9E:AF:42”

alias: “Living Room TV - Refresh Source”
id: living_room_tv_refresh_source

triggers:

trigger: state
entity_id: input_boolean.living_room_tv_source_refresh
to: “on”

actions:

action: homeassistant.update_entity
target:
entity_id: sensor.living_room_tv_smartthings

action: input_boolean.turn_off
target:
entity_id: input_boolean.living_room_tv_source_refresh

alias: “Living Room TV - Refresh Source on Power or HA Restart”
id: living_room_tv_refresh_source_on_power

triggers:

trigger: state
entity_id: media_player.living_room_tv
from: “off”
id: tv_on

trigger: homeassistant
event: start
id: ha_start

actions:

choose:

conditions:

condition: trigger
id: tv_on
sequence:

delay: “00:00:05”

conditions:

condition: trigger
id: ha_start
sequence:

delay: “00:00:15”

action: homeassistant.update_entity
target:
entity_id: sensor.living_room_tv_smartthings

media_player:

platform: universal
name: “Living Room Media”
unique_id: living_room_media
device_class: tv

children:

media_player.living_room_apple_tv

media_player.living_room_tv

commands:
turn_on:
action: media_player.turn_on
target:
entity_id: media_player.living_room_apple_tv

turn_off:
action: media_player.turn_off
target:
entity_id: media_player.living_room_apple_tv

volume_set:
action: media_player.volume_set
target:
entity_id: media_player.living_room_soundbar
data:
volume_level: “{{ volume_level | float }}”

volume_up:
action: media_player.volume_up
target:
entity_id: media_player.living_room_soundbar

volume_down:
action: media_player.volume_down
target:
entity_id: media_player.living_room_soundbar

volume_mute:
action: media_player.volume_mute
target:
entity_id: media_player.living_room_soundbar
data:
is_volume_muted: “{{ is_volume_muted | bool }}”

media_play:
action: media_player.media_play
target:
entity_id: media_player.living_room_apple_tv

media_pause:
action: media_player.media_pause
target:
entity_id: media_player.living_room_apple_tv

media_stop:
action: media_player.media_stop
target:
entity_id: media_player.living_room_apple_tv

media_next_track:
action: media_player.media_next_track
target:
entity_id: media_player.living_room_apple_tv

media_previous_track:
action: media_player.media_previous_track
target:
entity_id: media_player.living_room_apple_tv

select_source:
action: media_player.select_source
target:
entity_id: media_player.living_room_apple_tv
data:
source: “{{ source }}”

play_media:
action: media_player.play_media
target:
entity_id: media_player.living_room_apple_tv
data:
media_content_id: “{{ media_content_id }}”
media_content_type: “{{ media_content_type }}”

attributes:
state: media_player.living_room_tv

volume_level: media_player.living_room_soundbar|volume_level
is_volume_muted: media_player.living_room_soundbar|is_volume_muted

source: sensor.living_room_tv_source

source_list: media_player.living_room_apple_tv|source_list

media_title: media_player.living_room_apple_tv|media_title
media_artist: media_player.living_room_apple_tv|media_artist
media_album_name: media_player.living_room_apple_tv|media_album_name
media_duration: media_player.living_room_apple_tv|media_duration
media_position: media_player.living_room_apple_tv|media_position
media_position_updated_at: media_player.living_room_apple_tv|media_position_updated_at

app_id: media_player.living_room_apple_tv|app_id
app_name: media_player.living_room_apple_tv|app_name

browse_media_entity: media_player.living_room_apple_tv