Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Motion control for some lights and heating switches


Question

Posted

Hi, 

I have been trying to write some LUA to have my motion sensors turn on some lights and a hot water circulating pump when motion is detected. To keep them running while motion is detected then turn them off 5 mins after the last motion is detected.

I seem to be only able to get the lights to come on when I hit "Run Action" and create motion at the same time. Also the light off section seems not to be working. I have been trying to get ChatGPT to help me sort it out, but I think it's making things worse.

Welcome any support.

BEst,

Phil

 

--[[
%% autostart
%% properties
237 value -- Declare the motion sensor's "value" property
239 value -- Declare the lux sensor's "value" property
%% globals
--]]
 
local motionSensorID = 237
local lightIDs = {73, 77} -- Updated light IDs
local lightSensorID = 239 -- Lux sensor
local luxThreshold = 30
local timeout = 10000 -- Timeout after 10 seconds for testing
local hotWaterSwitchID = 229
 
local motionFlagVar = "Kitchen_MotionActive"
local lightsFlagVar = "Kitchen_LightsScripted"
local stopFlagVar = "Kitchen_StopFlag"
 
-- Function to check if the room lights are on
local function areRoomLightsOn()
for _, id in ipairs(lightIDs) do
local status = fibaro.getValue(id, "value")
fibaro.debug("Light " .. id .. " status: " .. tostring(status))
if status == "1" then return true end
end
return false
end
 
-- Check if the scene is stopped
local stopFlag = fibaro.getGlobalVariable(stopFlagVar)
if stopFlag == "1" then
fibaro.debug("Scene has been stopped manually.")
return -- Exit the scene if stop flag is set
end
 
-- Debugging the scene start
fibaro.debug("Kitchen scene triggered")
 
-- Check the raw motion sensor value
local motionVal = fibaro.getValue(motionSensorID, "value")
fibaro.debug("Raw Motion sensor value: " .. tostring(motionVal))
 
-- Convert value to string and compare it
if type(motionVal) == "boolean" then
motionVal = tostring(motionVal) -- Convert boolean to string
elseif type(motionVal) == "number" then
motionVal = tostring(motionVal) -- Convert number to string
elseif motionVal == nil then
motionVal = "false" -- Default to "false" if nil
end
 
-- Debugging if motion is detected
fibaro.debug("Motion value after conversion: " .. motionVal)
 
-- Only proceed if motion is detected (check both "true" and "1")
if motionVal == "true" or motionVal == "1" then
fibaro.debug("Motion detected!")
 
-- Turn on the hot water
fibaro.call(hotWaterSwitchID, "turnOn")
 
local lux = tonumber(fibaro.getValue(lightSensorID, "value")) or 100
fibaro.debug("Lux: " .. tostring(lux))
 
local lightsOn = areRoomLightsOn()
 
-- If lux is below threshold and lights are off, turn them on
if lux < luxThreshold and not lightsOn then
for _, id in ipairs(lightIDs) do
fibaro.call(id, "turnOn")
fibaro.debug("Turning on light: " .. id)
end
fibaro.setGlobalVariable(lightsFlagVar, "1")
else
fibaro.setGlobalVariable(lightsFlagVar, "0")
end
 
-- Set the motion flag to active
fibaro.setGlobalVariable(motionFlagVar, "1")
 
-- Set a timeout to reset flags and check again after no motion (10 seconds for testing)
fibaro.setTimeout(timeout, function()
local stillMotion = fibaro.getValue(motionSensorID, "value")
fibaro.debug("Checking motion after timeout: " .. tostring(stillMotion))
 
if stillMotion == "false" or stillMotion == "0" then
-- Turn off the lights after the timeout if they were turned on via the script
if fibaro.getGlobalVariable(lightsFlagVar) == "1" then
fibaro.debug("Turning off lights due to timeout.")
for _, id in ipairs(lightIDs) do
fibaro.call(id, "turnOff")
fibaro.debug("Turning off light: " .. id)
end
end
fibaro.setGlobalVariable(motionFlagVar, "0")
fibaro.setGlobalVariable(lightsFlagVar, "0")
fibaro.debug("Timeout reached, scene reset.")
else
fibaro.debug("Motion still detected, lights will remain on.")
end
end)
end

 

0 answers to this question

Recommended Posts

There have been no answers to this question yet

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...