Octoprint et Domoticz

Un petit script lua pour récupérer les infos d’Octoprint dans Domoticz

Octoprint et Domoticz

Il vous faut 7 Dummy devices :

  • OctoStatusIDX=’387′ — type: Text pour afficher le status de l’imprimante
  • OctoBebIDX=’388′ — type: Temperature pour afficher la température du plateau
  • OctoHeadIDX=’389′ — type: Temperature pour afficher la température de la tête d’impression
  • OctoTotalTimeIDX=’390′ — type: Text pour le temps total d’impression
  • OctoCompleteIDX=’391′ — type: Percentage pour le pourcentage d’avancement
  • OctoPrintTimeIDX=’392′ — type: Text pour la durée d’impression passé
  • OctoTimeLeftIDX=’393′ — type: Text pour le temps restant.

Dans les paramètres du script, il faut modifier les IDX de ces devices lignes 14 à 20.

Renseigner l’IP d’Octoprint ligne 5, la clé API d’Octoprint ligne 7.

Et enfin, si différent, l’emplacement du script JSON.lua et de curl, lignes 9 et 11.

Le script est disponible ici : https://github.com/chatainsim/scripts_domoticz/blob/master/octoprint.lua

-- Use Lua -> Time
-- For displaying debug messages
debug=false
-- IP address of octoprint 
OctoIP='OctoprintIPAddress'
-- Port of Octoprint - Default 80
OctoPort='80'
-- Octoprint API Key (found in Settings -> API)
OctoAPI='OctoprintAPIKey'
-- JSON.lua path
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")()
-- Curl path
curl = '/usr/bin/curl'

-- IDX of your Dummy devices
OctoStatusIDX='387' -- type: Text
OctoBebIDX='388' -- type: Temperature
OctoHeadIDX='389' -- type: Temperature
OctoTotalTimeIDX='390' -- type: Text
OctoCompleteIDX='391' -- type: Percentage
OctoPrintTimeIDX='392' -- type: Text
OctoTimeLeftIDX='393' -- type: Text

-- If you don't have a heated bed, put this to false
HeatedBed=true

OctoPrinter='http://'..OctoIP..':'..OctoPort..'/api/printer'
OctoJob='http://'..OctoIP..':'..OctoPort..'/api/job'

local function update(idx, value1)
    local cmd = idx..'|0|'..value1
    table.insert (commandArray, { ['UpdateDevice'] = cmd } )
end
local function ping(OctoIP)
    ping_success=os.execute('ping -W2 -c1 '..OctoIP)
    return ping_success
end
local function online()
    DataOctoTemp = assert(io.popen(curl..' -s --max-time 8 -H "X-Api-Key: '..OctoAPI..'" "'..OctoPrinter..'"'))
    BlocOctoTemp = DataOctoTemp:read('*all')
    DataOctoTemp:close()
    JsonOctoTemp = json:decode(BlocOctoTemp)
end
local function round(num, n)
  local mult = 10^(n or 0)
  return math.floor(num * mult + 0.5) / mult
end
function SecondsToClock(seconds)
  local seconds = tonumber(seconds)

  if seconds <= 0 then
    return "00:00:00";
  else
    hours = string.format("%02.f", math.floor(seconds/3600));
    mins = string.format("%02.f", math.floor(seconds/60 - (hours*60)));
    secs = string.format("%02.f", math.floor(seconds - hours*3600 - mins *60));
    return hours..":"..mins..":"..secs
  end
end
local function isempty(s)
  return s == nil or s == ''
end
commandArray = {}
    local status, retval = pcall(online,10);
if(ping(OctoIP)) then
    if (status) then
      DataOctoTemp = assert(io.popen(curl..' -s --max-time 8 -H "X-Api-Key: '..OctoAPI..'" "'..OctoPrinter..'"'))
      BlocOctoTemp = DataOctoTemp:read('*all')
      DataOctoTemp:close()
      JsonOctoTemp = json:decode(BlocOctoTemp)
      OctoBeb = JsonOctoTemp.temperature.bed.actual
      OctoHead = JsonOctoTemp.temperature.tool0.actual
      OctoStatus = JsonOctoTemp.state.text
      OctoState = JsonOctoTemp.state.flags.printing
      if (HeatedBed) then update(OctoBebIDX, OctoBeb) end
      update(OctoHeadIDX, OctoHead)
      update(OctoStatusIDX, OctoStatus)
      
      if (OctoState) then
          DataOctoTime = assert(io.popen(curl..' -s --max-time 8 -H "X-Api-Key: '..OctoAPI..'" "'..OctoJob..'"'))
          BlocOctoTime = DataOctoTime:read('*all')
          DataOctoTime:close()
          JsonOctoTime = json:decode(BlocOctoTime)
          OctoTotalTime = JsonOctoTime.job.estimatedPrintTime
          OctoComplete = JsonOctoTime.progress.completion
          OctoPrintTime = JsonOctoTime.progress.printTime
          OctoTimeLeft = JsonOctoTime.progress.printTimeLeft
          if isempty(OctoTotalTime) then update(OctoTotalTimeIDX, SecondsToClock(0)) else update(OctoTotalTimeIDX, SecondsToClock(OctoTotalTime)) end
          if isempty(OctoComplete) then update(OctoCompleteIDX, round(0)) else update(OctoCompleteIDX, round(OctoComplete)) end
          if isempty(OctoPrintTime) then update(OctoPrintTimeIDX, SecondsToClock(0)) else update(OctoPrintTimeIDX, SecondsToClock(OctoPrintTime)) end
          if isempty(OctoTimeLeft) then update(OctoTimeLeftIDX, SecondsToClock(0)) else update(OctoTimeLeftIDX, SecondsToClock(OctoTimeLeft)) end
      end
    else
      if (debug) then print("Printer not connected") end
      update(OctoStatusIDX, "Printer not connected")
      if (HeatedBed) then update(OctoBebIDX, 0) end
      update(OctoHeadIDX, 0)
      update(OctoTotalTimeIDX, SecondsToClock(0))
      update(OctoCompleteIDX, 0)
      update(OctoPrintTimeIDX, SecondsToClock(0))
      update(OctoTimeLeftIDX, SecondsToClock(0))
    end
else
    update(OctoStatusIDX, "Octoprint offline.")
end
return commandArray
octoprint.lua