Script for light to switchOff when TV is on  [Solved]

Easy to use, 100% Lua-based event scripting framework.
Post Reply
drwurn
Posts: 42
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Script for light to switchOff when TV is on  [Solved]

Post by drwurn » 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

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
}

User avatar
waaren
Posts: 1350
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for light to switchOff when TV is on

Post by waaren » Friday 19 October 2018 12:51

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"
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

drwurn
Posts: 42
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script for light to switchOff when TV is on

Post by drwurn » 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

User avatar
waaren
Posts: 1350
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for light to switchOff when TV is on

Post by waaren » Friday 19 October 2018 14:28

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
}
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

drwurn
Posts: 42
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script for light to switchOff when TV is on

Post by drwurn » 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?

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
}

User avatar
waaren
Posts: 1350
Joined: Tuesday 03 January 2017 15:18
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Script for light to switchOff when TV is on

Post by waaren » Friday 19 October 2018 17:10

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.
Raspberry (debian stretch via berryboot on Synology DS916+) , Domoticz (almost) latest Beta, , dzVents 2.6, RFLink, RFXtrx433e, P1, Youless, Harmony, Hue, Yeelight, Xiaomi, HomeWizard, Zwave, Amazon echo

drwurn
Posts: 42
Joined: Sunday 10 June 2018 16:59
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Script for light to switchOff when TV is on

Post by drwurn » Friday 19 October 2018 18:21

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

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests