Page 1 of 1
Script for light to switchOff when TV is on [Solved]
Posted: Friday 19 October 2018 10:59
by drwurn
I'm trying to make a script to turn off a light when tv is switched on.
When the tv is switched off the light has to go on again.
I'm this far but there is (atleast) one fault at [15:8] '}' expected near 'if'.
Not sure what is going wrong
Code: Select all
local timerString = "every 1 minutes between sunset and 23:00"
return {
on = { timer = { timerString }},
execute = function(dz)
local SamsungTV = dz.devices(48)
local LampWoonkamer = dz.devices(3)
local OnConditionsMet = LampWoonkamer.state == "On" and
SamsungTV.state =="On" and
dz.time.matchesRule("between sunset and 23:00")
end
if OnConditionsMet then
LampWoonkamer.switchOff()
else
if LampWoonkamer.state == "Off" then
LampWoonkamer.switchOn()
if SamsungTV.state =="Off"
elseif dz.time.matchesRule("between sunset and 23:00")
end
end
end
}
Re: Script for light to switchOff when TV is on
Posted: Friday 19 October 2018 12:51
by waaren
drwurn wrote: Friday 19 October 2018 10:59
I'm trying to make a script to turn off a light when tv is switched on.
When the tv is switched off the light has to go on again.
I'm this far but there is (atleast) one fault at [15:8] '}' expected near 'if'.
Not sure what is going wrong
Not sure what you want with the light outside the time the script executes but the end at line 13 does not belong there.
What is the purpose of the logic at line 20/21 ? They niow both miss a "then"
Re: Script for light to switchOff when TV is on
Posted: Friday 19 October 2018 13:54
by drwurn
Hi Waaren,
What I want to accomplish is:
The light goes on at sunset and off at 23:00.
If the light is on, and the tv is switched on, the light has to go off.
When I turn the tv off, the light has to go on again if it's between sunset and 23:00
Re: Script for light to switchOff when TV is on
Posted: Friday 19 October 2018 14:28
by waaren
drwurn wrote: Friday 19 October 2018 13:54
Hi Waaren,
What I want to accomplish is:
The light goes on at sunset and off at 23:00.
If the light is on, and the tv is switched on, the light has to go off.
When I turn the tv off, the light has to go on again if it's between sunset and 23:00
Untested but could be something like
Code: Select all
local timerString = "every minute between sunset and 23:01"
local tv = 48
return {
on = { timer = { timerString },
devices = { tv }},
execute = function(dz)
local LampWoonkamer = dz.devices(3)
local SamsungTV = dz.devices(tv)
if dz.time.matchesRule("between sunset and 22:59") then
if SamsungTV.state == "On" then LampWoonkamer.switchOff().checkFirst()
elseif SamsungTV.state == "Off" then LampWoonkamer.switchOn().checkFirst()
end
else
LampWoonkamer.switchOff().checkFirst()
end
end
}
Re: Script for light to switchOff when TV is on
Posted: Friday 19 October 2018 15:26
by drwurn
That works great.
I did read your feedback and your script and modified my own script. Not that I think that my script is better but it's like the first or second script that I write and this way I can learn from my own mistakes instead of copy/paste.
Please tell me what you think of my script, can I improve it somehow?
Code: Select all
local timerString = "every 1 minutes between 10 minutes before sunset and 23:01"
return {
on = { timer = { timerString }},
execute = function(dz)
local SamsungTV = dz.devices(48)
local LampWoonkamer = dz.devices(3)
local OnConditionsMet = LampWoonkamer.state == "On" and
SamsungTV.state =="On" and
dz.time.matchesRule("between 10 minutes before sunset and 23:00")
if OnConditionsMet then
LampWoonkamer.switchOff()
else
if LampWoonkamer.state == "Off" and
SamsungTV.state == "Off" and
dz.time.matchesRule("between 10 minutes before sunset and 23:00") then
LampWoonkamer.switchOn().checkFirst()
end
end
end
}
Re: Script for light to switchOff when TV is on
Posted: Friday 19 October 2018 17:10
by waaren
drwurn wrote: Friday 19 October 2018 15:26
That works great.
I did read your feedback and your script and modified my own script. Not that I think that my script is better but it's like the first or second script that I write and this way I can learn from my own mistakes instead of copy/paste.
Please tell me what you think of my script, can I improve it somehow?
Looks great. One smalle remark/question. Why do you have the first
Code: Select all
and
dz.time.matchesRule("between 10 minutes before sunset and 23:00")
?
If I understand correctly that part is always true and therewith redundant when using this time trigger.
I fully understand your remark about learning better from trial and error than from copy/paste. I try to find a mix of those for my own learning. I copy/paste almost everything and the trial and error starts when I try to glue the parts together to something that does exactly what I want and when I want it.
Re: Script for light to switchOff when TV is on
Posted: Friday 19 October 2018 18:21
by drwurn
Not sure why I have
Code: Select all
and
dz.time.matchesRule("between 10 minutes before sunset and 23:00")
planted there. Thought it look good there lol, but shall remove it if it's useless there
Thanks