LUA - can't get Nightmode script to run

Moderator: leecollings

Post Reply
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

LUA - can't get Nightmode script to run

Post by thorbj »

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:

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
Thanks!
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
JuanUil
Posts: 500
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: LUA - can't get Nightmode script to run

Post by JuanUil »

You should place the or functions between brackets ()
F. e.

if (device and device or (device and device))

succes
Jan
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
thorbj
Posts: 113
Joined: Thursday 20 November 2014 22:11
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Norway
Contact:

Re: LUA - can't get Nightmode script to run

Post by thorbj »

So I should change line 45 to this?

Code: Select all

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
Thanks!
2xRaspberry Pi Model 2 w/RaZberry z-wave chip (master/slave)|Fibaro Wall Plug|Fibaro Universal Dimmer 500W|Aeon Labs Multisensor|RFXtrx433E|Nexa WMR-1000|Nexa Pe-3|Nexa Remote|Nexa LGDR3500|Lightify Gateway|Lightify RGBW bulb
JuanUil
Posts: 500
Joined: Friday 22 May 2015 12:21
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11083
Location: Asten NB Nederland
Contact:

Re: LUA - can't get Nightmode script to run

Post by JuanUil »

yes you should
success!
Your mind is like a parachute,
It only works when it is opened!

RPI4 several Fibaro, KaKu, Neocoolcam switches, Z-Wave, Zigbee2Mqtt, Ikea bulbs and remote, Zigbee temp nodes
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest