Tända slumpmässigt

Post Reply
User avatar
sonnyboy
Proffsmedlem
Posts: 669
Joined: 26 Sep 2013, 08:05
10
Location: Västerås

Jag kör denna luakod för att tända min belysning slumpmässigt.
Har en scen som uppdateras varannan minut och kör denna kod.
jag kan ju tycka att det är helt onödigt att den ska skicka tändkommandon när det redan är tänt
Då såg jag dantibergs lilla lua kod som jag ville få in men det går inte, hur jag en försöker så får jag något tokigt felmeddelande.

Någon som kan hjälpa mig med detta?
Min kod

Code: Select all

--[[
%% properties

%% globals
--]]

-- REFERENCE
-- zwaveforum.se, forum.fibaro.com
-- Thanks to Bamsefar, Daniel.Knight for good LUA functions code.

if (fibaro:countScenes() > 1) then fibaro:abort() end

local deviceIDs = { 248, 250, 252, 254, 45, 189 }
 
math.randomseed(os.time())

function randomOrder(table)
  local new = {}
   for j,x in pairs(table) do new[j] = x end
    local res = {}
     while (#new > #res) do
      local index = math.random(1,#new)
      if (new[index] > -1) then
      res[#res+1] = new[index]
     new[index] = -1
    end
  end
 return res
end


 for Device,device in pairs(randomOrder(deviceIDs)) do
  
   fibaro:call(device, "turnOn")
    fibaro:debug( Device .. " DeviceID: "..device)
     fibaro:sleep(math.random(5000,12000))
      fibaro:call(119, "pressButton", "1")--Endast för VD
end
Dantibergs kod som jag vill ha in någonstans.

Code: Select all

local lightstatus=tonumber(fibaro:getValue(Device, "value"));
if(lightstatus>0
Fibaro HomeCenter 2
Fw 4.600
BeyondMeasure 1.10
EventRunner
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
11
Location: Hofors

Prova detta

Code: Select all

--[[
%% properties

%% globals
--]]

-- REFERENCE
-- zwaveforum.se, forum.fibaro.com
-- Thanks to Bamsefar, Daniel.Knight for good LUA functions code.

if (fibaro:countScenes() > 1) then fibaro:abort() end

local deviceIDs = { 248, 250, 252, 254, 45, 189 }
 
math.randomseed(os.time())

function randomOrder(table)
  local new = {}
   for j,x in pairs(table) do new[j] = x end
    local res = {}
     while (#new > #res) do
      local index = math.random(1,#new)
      if (new[index] > -1) then
      res[#res+1] = new[index]
     new[index] = -1
    end
  end
 return res
end


 for Device,device in pairs(randomOrder(deviceIDs)) do
	local lightstatus=tonumber(fibaro:getValue(deviceIDs, "value"));
	if(lightstatus>0 then
		fibaro:call(device, "turnOn")
	end
    fibaro:debug( Device .. " DeviceID: "..device)
     fibaro:sleep(math.random(5000,12000))
      fibaro:call(119, "pressButton", "1")--Endast för VD
end
User avatar
sonnyboy
Proffsmedlem
Posts: 669
Joined: 26 Sep 2013, 08:05
10
Location: Västerås

Det funkade inte, fick detta i loggen. :o
[ERROR] 19:37:17: line 34: ')' expected near 'then'
och när jag satte en ) efter 0 så kom detta
[ERROR] 19:39:37: Runtime error: /opt/fibaro/FibaroSceneAPI.lua:91: attempt to concatenate local 'deviceId' (a table value)
Fibaro HomeCenter 2
Fw 4.600
BeyondMeasure 1.10
EventRunner
User avatar
Bamsefar
Z-Wave Kung
Posts: 1230
Joined: 25 Nov 2013, 15:06
10
Location: Stockholm

Fattas en parentes i if villkoret:

Code: Select all

    --[[
    %% properties

    %% globals
    --]]

    -- REFERENCE
    -- zwaveforum.se, forum.fibaro.com
    -- Thanks to Bamsefar, Daniel.Knight for good LUA functions code.

    if (fibaro:countScenes() > 1) then fibaro:abort() end

    local deviceIDs = { 248, 250, 252, 254, 45, 189 }
     
    math.randomseed(os.time())

    function randomOrder(table)
      local new = {}
       for j,x in pairs(table) do new[j] = x end
        local res = {}
         while (#new > #res) do
          local index = math.random(1,#new)
          if (new[index] > -1) then
          res[#res+1] = new[index]
         new[index] = -1
        end
      end
     return res
    end


     for Device,device in pairs(randomOrder(deviceIDs)) do
       local lightstatus=tonumber(fibaro:getValue(deviceIDs, "value"));
       if(lightstatus>0) then
          fibaro:call(device, "turnOn")
       end
        fibaro:debug( Device .. " DeviceID: "..device)
         fibaro:sleep(math.random(5000,12000))
          fibaro:call(119, "pressButton", "1")--Endast för VD
    end
Testar Home Assistant på Raspberry Pi4B - nice :mrgreen:
jompa68
Proffsmedlem
Posts: 735
Joined: 12 Aug 2012, 08:53
11
Location: Hofors

Tillrättad kod, denna är verifierad :)

Code: Select all

--[[
%% properties

%% globals
--]]

-- REFERENCE
-- zwaveforum.se, forum.fibaro.com
-- Thanks to Bamsefar, Daniel.Knight for good LUA functions code.

if (fibaro:countScenes() > 1) then fibaro:abort() end

local deviceIDs = { 25, 50, 52 }
 
math.randomseed(os.time())

function randomOrder(table)
  local new = {}
   for j,x in pairs(table) do new[j] = x end
    local res = {}
     while (#new > #res) do
      local index = math.random(1,#new)
      if (new[index] > -1) then
      res[#res+1] = new[index]
     new[index] = -1
    end
  end
 return res
end


 for Device,device in pairs(randomOrder(deviceIDs)) do
   local lightstatus=tonumber(fibaro:getValue(device, "value"));
  fibaro:debug(lightstatus)
   if (lightstatus==0) then
      fibaro:call(device, "turnOn")
   end
    fibaro:debug( Device .. " DeviceID: "..device)
     fibaro:sleep(math.random(5000,12000))
      --fibaro:call(119, "pressButton", "1")--Endast för VD
end
User avatar
sonnyboy
Proffsmedlem
Posts: 669
Joined: 26 Sep 2013, 08:05
10
Location: Västerås

Ser bra ut, tackar för hjälpen [WHITE SMILING FACE]
Bara att vänta ett tag till så det börjar skymma lite.
Fibaro HomeCenter 2
Fw 4.600
BeyondMeasure 1.10
EventRunner
Post Reply