LUA - can't get Nightmode script to run
Posted: Wednesday 25 November 2015 8:59
I have this script for turning off all lights when we have gone to bed. But I'm struggeling to get it to work.
It's supposed to check if one of three lamps are ON and motion, TV and an override-switch is off. Then if time is between 10PM and 6AM, it will activate a scene that turns off all lights. But this never happens.
I have checked that the scene works, and I've added a notification to the scene so that I get a message via Telegram if the scene is activated. That way I know that the scene is never activated by the script.
It would be great if someone could spare some precious time and check for errors or faults in my script:
Thanks!
It's supposed to check if one of three lamps are ON and motion, TV and an override-switch is off. Then if time is between 10PM and 6AM, it will activate a scene that turns off all lights. But this never happens.
I have checked that the scene works, and I've added a notification to the scene so that I get a message via Telegram if the scene is activated. That way I know that the scene is never activated by the script.
It would be great if someone could spare some precious time and check for errors or faults in my script:
Code: Select all
-- ~/domoticz/scripts/lua/script_time_nightmode.lua
-- Function for calculating time
function timedifference (s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
time = os.date("*t")
commandArray = {}
-- SETTINGS --
local motion_switch = 'Bevegelse Stue' -- name of the motion switch
local lamp_switch = 'Dimmer stue sør' -- name of a lamp that this script should depend on
local lamp_switch2 = 'Oslo Wood' -- name of a lamp that this script should depend on
local lamp_switch3 = 'AJ Lampe' -- name of a lamp that this script should depend on
local media_device = 'Chromecast' -- name of a media device like Chromecast or TV.
local override_switch = 'Overstyr' -- Switch for activating system override
-- END SETTINGS --
-- Define hours for day and evening lights
h = tonumber((os.date('%H')))
if (h >= 22 or h < 6)
then
x = 'Scene:Night'
g = '(<b>Natt</b>)<font color="red">No movement in the livingroom and the TV is off. switching off lights...</font>)'
i = '<font color="green">GOOD NIGHT!</font>'
else
x = 'Not night'
end
-- Issue command "x" if motion_switch, media_device and override_switch is off after 10PM.
if (otherdevices[lampswitch] == 'On' and otherdevices[motion_switch] == 'Off' and otherdevices[media_device] == 'Off' and otherdevices[override_switch] == 'Off' or otherdevices[lampswitch2] == 'On' and otherdevices[motion_switch] == 'Off' and otherdevices[media_device] == 'Off' and otherdevices[override_switch] == 'Off' or otherdevices[lampswitch3] == 'On' and otherdevices[motion_switch] == 'Off' and otherdevices[media_device] == 'Off' and otherdevices[override_switch] == 'Off') then
print(g)
commandArray[x]='On'
print(i)
end
return commandArray