Re: Samsung Washmachine read status from json "SOLVED"
Posted: Thursday 05 March 2020 13:56
Updated code V1.1
*Typo row:93 weightSensing
*Row:182 Prevent error log if wm_percent.percentage = nil om some unknown reaseon
*Typo row:93 weightSensing
*Row:182 Prevent error log if wm_percent.percentage = nil om some unknown reaseon
Code: Select all
-- assumpitions for Samsung Wasmachine Script V1.1
--[[
Dummy textdevices WM Programma, WM Klaar om, WM aan het, WM Toerental, WM Resterend, WM Temperatuus.
Dummy on/of switch WM Status
Dummy Percentaeg WM Percentage
API key from https://developer.samsung.com/smartthings
Deivce ID:
With this curl reqeust you can get all the device info from the command line (somewhere in the response look for "deviceId" ) (There is an space betwean Bearer and < YOUR API KEY> ! )
curl --location --request GET 'https://api.smartthings.com/v1/devices/' \ --header 'Authorization: Bearer <Your API KEY>'
]]--
local API = '<YOUR API>'
local Device = '<YOUR DEVICE ID>'
local scriptVar = 'wasmachine_JSON'
--Define dz Switches
local WM_STATUS = 'WM Status' --Domoticz virtual switch ON/Off state Washer
local WM_PERCENT = 'WM Percentage'
local LOGGING = true
return
{
on =
{
timer =
{
'every minute', -- just an example to trigger the request
},
devices =
{
200, -- Just an Switch to activated the script manualy for testing.
},
httpResponses =
{
scriptVar, -- must match with the callback passed to the openURL command
},
},
logging =
{
level = domoticz.LOG_DEBUG,
marker = scriptVar,
},
execute = function(dz, item)
local wm_status = dz.devices(WM_STATUS)
local wm_percent = dz.devices(WM_PERCENT)
if item.isTimer or item.isDevice then
dz.openURL({
url = 'https://api.smartthings.com/v1/devices/'.. Device .. '/states',
headers = { ['Authorization'] = 'Bearer '.. API },
method = 'GET',
callback = scriptVar, -- httpResponses above.
})
return
elseif item.ok then
if (item.isJSON) then -- when recognized as json then dzVents will convert it to a table for you
rt = item.json.main
if _G.logLevel == dz.LOG_DEBUG then -- Only when script loglevel is set to debug
-- dump the string represenation of the complete table to log
dz.utils.dumpTable(item.json)
dz.log('\n-\nAnd now some subtables \n-\n ', dz.LOG_DEBUG)
dz.log('\n-\n ------------ main.data.value\n-\n', dz.LOG_DEBUG)
--dump the string representation of the DATA table to log, The data attributes changes depending on the state of the washer.
dz.utils.dumpTable(dz.utils.fromJSON(item.json.main.data.value))
end
local function codeToDutch(code) --Convert Table_00_Course_* and Washingstate to Dutch
local translationcode =
{
D0 = 'Katoen',
D1 = 'Eco Katoen',
D2 = 'Syntethisch',
D3 = 'Fijne Was',
D4 = 'Spelen+Centrifugeren',
D5 = 'Eco Trommelreiniging',
D6 = 'Beddengoed',
D7 = 'Outdoor',
D8 = 'Wol',
D9 = 'Donkere Kleding',
DA = 'Super Ecowas',
DB = 'Super Speed',
DC = '15 min Kort',
BA = 'Centrifugeren',
none = 'Niksen',
weightSensing = 'Wegen',
wash = 'Wassen',
rinse = 'Spoelen',
spin = 'Centrifugeren',
finish = 'Klaar',
}
return translationcode[code:gsub('Table_00_Course_','')] or translation[code]
end
local endprogram = 'Programma gereed'
--Update textdevices only when status is changed to prevent too many irrelevant log rows
local function updateTextOnlyWhenChanged( textSensorName, newText)
local textDevice = dz.devices(textSensorName)
if textDevice.text == newText then
return
else
textDevice.updateText(newText)
end
end
--Update percentagedevices only when status is changed to prevent too many irrelevant log rows
local function updatePercentageOnlyWhenChanged( percentageSensorName, newPercentage)
local percentageDevice = dz.devices(percentageSensorName)
if percentageDevice.percentage == newPercentage then
return
else
-- percentage.updatePercentage(newPercentage)
percentageDevice.updatePercentage(newPercentage)
end
end
local function convertTime(dateString, offset ) --convert the time to Hour,Minutem,second.
local function makeTimeStamp(dateString)
local pattern = '(%d+)%-(%d+)%-(%d+)%T(%d+):(%d+):(%d+)(%.*)' -- %d+ = 1 or more digits , %.* = 0 or more any character
local xyear, xmonth, xday, xhour, xminute, xseconds = dateString:match(pattern) -- split the string using the pattern
local convertedTimestamp = os.time({year = xyear, month = xmonth, day = xday, hour = xhour, min = xminute, sec = xseconds})
return convertedTimestamp
end
return os.date('%H:%M:%S', makeTimeStamp(dateString) + offset )
end
local values = dz.utils.fromJSON(item.json.main.data.value)
local remainingTime = values.payload.remainingTime or values.payload["x.com.samsung.da.remainingTime"] or 'not found'
local progressPercentage = values.payload.progressPercentage or values.payload["x.com.samsung.da.progressPercentage"] or 'not found'
local completionTime = convertTime(rt.completionTime.value, 3600 ) --Making 1 hour ofset to get local time
local switch = rt.switch.value
local washerWaterTemperature = rt.washerWaterTemperature.value
local washerJobState = rt.washerJobState.value
local washerMode = rt.washerMode.value
local washerSpinLevel = rt.washerSpinLevel.value
if switch == ('on') then -- Turn on Domoitcz switches updates only when Washer is Active.
dz.log('Debuggg if swithc on:' .. switch, dz.LOG_DEBUG)
wm_status.switchOn().checkFirst() --turn on dz WM status switch
updateTextOnlyWhenChanged('WM Temperatuur', washerWaterTemperature )
updateTextOnlyWhenChanged('WM Aan het', codeToDutch(washerJobState))
updateTextOnlyWhenChanged('WM Toerental', washerSpinLevel)
updateTextOnlyWhenChanged('WM Resterend', remainingTime)
updateTextOnlyWhenChanged('WM Klaar om', completionTime)
updateTextOnlyWhenChanged('WM Programma', codeToDutch(washerMode))
updatePercentageOnlyWhenChanged('WM Percentage', progressPercentage)
else --Upate text deices with endprogram wen swithc is off
dz.log('Debuggg if switch off:' .. switch, dz.LOG_DEBUG)
wm_status.switchOff().checkFirst()
dz.log('washerMode if switch off:' .. endprogram, dz.LOG_DEBUG)
updateTextOnlyWhenChanged('WM Temperatuur', endprogram)
updateTextOnlyWhenChanged('WM Aan het', endprogram)
updateTextOnlyWhenChanged('WM Toerental', endprogram)
updateTextOnlyWhenChanged('WM Resterend', endprogram)
updateTextOnlyWhenChanged('WM Klaar om', endprogram)
updateTextOnlyWhenChanged('WM Programma', endprogram)
updatePercentageOnlyWhenChanged('WM Percentage', 100)
end
if LOGGING then
dz.log('\n-\nWhats happening: \n-\n ', dz.LOG_INFO)
dz.log('Klaar om:' .. completionTime, dz.LOG_INFO)
dz.log('Status:' .. switch, dz.LOG_INFO)
dz.log('Temperatuur:' .. washerWaterTemperature, dz.LOG_INFO)
dz.log('Aan het:' .. washerJobState, dz.LOG_INFO)
dz.log('Programma:' .. washerMode, dz.LOG_INFO)
dz.log('Toerental:' .. washerSpinLevel, dz.LOG_INFO)
dz.log('remaining time = ' .. remainingTime, dz.LOG_INFO)
dz.log('progressPercentage = ' .. progressPercentage, dz.LOG_INFO)
if wm_percent.percentage == nil then --Prevent error log if wm_percent.percentage = nil om some unknown reaseon
dz.log('Percentage WM Percentage NIL', dz.LOG_INFO)
else dz.log('Percentage WM Percentage = ' .. wm_percent.percentage .. '%' , dz.LOG_INFO)
end
dz.log('\n-\n============================== \n-\n ', dz.LOG_INFO)
end
return
else
dz.log('result was not recognized as a JSON', dz.LOG_ERROR)
end
else
dz.log('result was not OK', dz.LOG_ERROR)
end
dz.log(item,dz.LOG_ERROR) -- dump all to log as one long string
end
}