Logic Machine Forum
Gardena smart - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Gateway (https://forum.logicmachine.net/forumdisplay.php?fid=10)
+--- Thread: Gardena smart (/showthread.php?tid=1349)



Gardena smart - gjniewenhuijse - 23.04.2018

Anyone already linked a Gardena Smart gateway?

Here my first code to read a Sensor and write the moisturelevel and batterylevel.

Code:
function getData()
  https = require 'ssl.https'
  require('json')
  socket.http.TIMEOUT = 5
 
 local cUsername = 'email@abc.com'
 local cPassword = 'password'
 
  -- get session info
  local cBody = '{"sessions":{"email":"' .. cUsername .. '","password":"' .. cPassword ..'"}}'
  local cReq = {}
  local cUrl = 'https://sg-api.dss.husqvarnagroup.net/sg-1/sessions'
  result1 = https.request({
      url = cUrl,
      method = 'POST',
      headers = {
        ['content-length'] = #cBody,
        ['content-type'] = 'application/json'
      },
      source = ltn12.source.string(cBody),
      sink = ltn12.sink.table(cReq)
  })
  if result1 then
    --log(cReq)
    cReq = table.concat(cReq)
    cReq = json.pdecode(cReq)
    if cReq then
      --log(cReq)
      token = cReq.sessions.token
      user_id = cReq.sessions.user_id
    end
  end

  -- get location info
  local cUrl = 'https://sg-api.dss.husqvarnagroup.net/sg-1/locations/?user_id=' .. user_id
  result2 = https.request({
      url = cUrl,
      method = 'GET',
      headers = {
        ['content-type'] = 'application/json',
        ['X-Session'] = token
      },
      sink = ltn12.sink.table(cReq)
  })
  if result2 then
    --log(cReq)
    cReq = table.concat(cReq)
    cReq = json.pdecode(cReq)
    if cReq then
      --log(cReq)
      local loc_name = cReq.locations[1].name
      loc_id = cReq.locations[1].id
    end
  end

  -- get device info
  local cUrl = 'https://sg-api.dss.husqvarnagroup.net/sg-1/devices?locationId=' .. loc_id
  result3 = https.request({
      url = cUrl,
      method = 'GET',
      headers = {
        ['content-type'] = 'application/json',
        ['X-Session'] = token
      },
      sink = ltn12.sink.table(cReq)
  })
  if result3 then
    --log(cReq)
    cReq = table.concat(cReq)
    cReq = json.pdecode(cReq)
    if cReq then
      --log(cReq)
      local dName = cReq.devices[1].name
      for k,v in pairs(cReq.devices) do
        --log(v)
        --log("Found the following GARDENA "..v.category)
        --log("Name: "..v.name) 
        --log("Identifier: "..v.id)
        -- gateway
        if (v.category == "gateway") then 
        -- sensor
        elseif (v.category == "sensor") then
          for a, b in pairs(v.abilities) do
            for c, d in pairs(b.properties) do
              --log(d.name)
              --log(d.value)
              if (d.name == 'last_time_online') then
                --check = tostring(d.value)
              elseif (d.name == 'humidity') then
                grp.write('1/1/1', tonumber(d.value))
              elseif (d.name == 'level') then 
                grp.write('1/1/2', tonumber(d.value))
              end
            end
          end
        -- onbekend apparaat
        else
          log('onbekend gardena apparaat: '..v.category)
        end
      end
    end
  end
end



RE: Gardena smart - admin - 25.04.2018

You need to check the result of https.request call instead of cReq variable. Since it's defined as a table first if cReq then... will evaluate as true.


RE: Gardena smart - gjniewenhuijse - 26.04.2018

(25.04.2018, 06:45)admin Wrote: You need to check the result of https.request call instead of cReq variable. Since it's defined as a table first if cReq then... will evaluate as true.

thanks for the comment.


RE: Gardena smart - Firechief - 24.05.2020

gjniewenhuijse,

I'm interested in this project as i'm installing a Smart Sileno Life 750 robotic mower this week and planning to extend this with Smart Water Control + Sensor and Smart Pressure Pump and maybe some Smart Power Plugs to control lights.
I'm a novice on scripting and things and would like to learn and understand more of this. It's not always clear where things go and steps to follow to get things working.
Can you, or anyone, explain me what to do and where to do it? I have a HL, wich is similar to the LM.

Thx in advance

I found this page with API for all Gardena Smart products:

https://developer.husqvarnagroup.cloud/apis/GARDENA+smart+system+API#/general

Kind regards,


RE: Gardena smart - Joep - 31.07.2023

"This API has moved to the official developer portal. Please follow the instructions at https://developer.husqvarnagroup.cloud"