Logic Machine Forum
apple tv standby - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: apple tv standby (/showthread.php?tid=4409)



apple tv standby - gjniewenhuijse - 29.11.2022

Is there a way to standby an Apple tv from a LM script?


RE: apple tv standby - admin - 29.11.2022

There are some solutions in Python and NodeJS but that's not something that can be run on LM as is.


RE: apple tv standby - gjniewenhuijse - 04.12.2022

Home Assistant support Apple TV standby, so i used that product also.

So make a script in Home Assistant and run that script with the LM

Code:
https = require('ssl.https')
json = require('json')
ltn12 = require('ltn12')
token = "xxxxxxxxxxxxxx" --generate long use token in HA (10 years lifetime)

function runScript(iDevice, iScript, iScriptId)
  data = json.encode({
    query = '{"entity_id": "'..iScriptId..'"}'
  })

  tbl = {}
  res, code = https.request({
    url = 'http://'..iDevice..'/api/services/script/'..iScript,
    method = 'POST',
    headers = {
      ['authorization'] = 'Bearer ' .. token,
      ['content-type']  = 'application/json',
      ['content-length'] = #data,
    },
    source = ltn12.source.string(data),
    sink = ltn12.sink.table(tbl),
  })

  --[[
  if res and code == 200 then
    resp = table.concat(tbl)
    resp = json.pdecode(resp)

    log(resp)
  else
    log(res, code)
  end
  --]]
end

-- standby apple tv woonkamer (ip, scriptname, scriptid)
--runScript("192.168.X.XX:8123", "AppleTVWoonkamer_Standby", "script.appletvwoonkamer_standby")