Page 1 of 2

Light on at different dim level depending on daytime, with auto off

Posted: Friday 15 February 2019 16:50
by cogliostrio
Thought Id share a script I made for turning on a light at different dim levels, depending on the time of day.
Hopefully it can be useful for others as well. Id also be interested to know if there are better ways to write something like this.

Code: Select all

return {
	on = {
		devices = {'PIR'}, -- Change out PIR
	},
   execute = function(domoticz, PIR)
   local Light = domoticz.devices('Light') -- Change out light

        if (PIR.state == 'On') and (domoticz.time.matchesRule('at 00:01-08:00')) then
            Light.dimTo(2)
            domoticz.log('DzVents: Light 2%, 00:00-08:00')

        elseif (PIR.state == 'On') and (domoticz.time.matchesRule('at 08:01-12:00')) then
            Light.dimTo(20)
            domoticz.log('DzVents: Light 20%, 08:00-12:00')

        elseif (PIR.state == 'On') and (domoticz.time.matchesRule('at 12:01-23:00')) then
            Light.dimTo(100)
            domoticz.log('DzVents: Light 100%, 12:00-23:00')
         
        elseif (PIR.state == 'On') and (domoticz.time.matchesRule('at 23:01-24:00')) then
            Light.dimTo(30)
            domoticz.log('DzVents: Light 30%, 23:00-24:00')
         
        else
            Light.switchOff().afterMin(1)
            domoticz.log('DzVents: Light off')
        end
    end
}

Re: Light on at different dim level depending on daytime, with auto off

Posted: Saturday 16 February 2019 7:58
by waaren
cogliostrio wrote: Friday 15 February 2019 16:50 Thought Id share a script I made for turning on a light at different dim levels, depending on the time of day.
Hopefully it can be useful for others as well. Id also be interested to know if there are better ways to write something like this.
Thx for sharing. As long as the script does where it is designed for, 'better' is subjective. The example below could be seen as easier to maintain and/or extend but it is still a matter of personal preference how you like your code to look like.

Code: Select all

return {
	on = {
		devices = {'PIR'}, -- Change out PIR
	},
   
   execute = function(dz, PIR)
        local Light = dz.devices('Light') -- Change out light
   
        local dimTimeTable  = { --  [   'timeSlot'   ]  = dimValue
                                    ['at 00:01-08:00']  = 2,
                                    ['at 08:01-12:00']  = 20,
                                    ['at 12:01-23:00']  = 100,
                                    ['at 23:01-24:00']  = 30,
                              }
                              
        if PIR.state ~= 'On' then 
            Light.switchOff().afterMin(1)
            dz.log(Light.name .. ' switched Off')
        else
            for timeSlot, dimValue in pairs (dimTimeTable) do
               if dz.time.matchesRule(timeSlot) then 
                    Light.dimTo(dimValue)
                    dz.log(Light.name .. ' switched On (dimlevel: ' .. dimValue .. ')')
                    return false
               end      
            end
        end     
    end
}

Re: Light on at different dim level depending on daytime, with auto off

Posted: Sunday 17 February 2019 14:07
by tezzlicious
waaren wrote: Saturday 16 February 2019 7:58 Thx for sharing. As long as the script does where it is designed for, 'better' is subjective. The example below could be seen as easier to maintain and/or extend but it is still a matter of personal preference how you like your code to look like.
Hi Waaren. How would this script look like when you want to do this depending on lux level ranges from a lux device within time slots :?:

Re: Light on at different dim level depending on daytime, with auto off

Posted: Sunday 17 February 2019 18:14
by waaren
tezzlicious wrote: Sunday 17 February 2019 14:07 How would this script look like when you want to do this depending on lux level ranges from a lux device within time slots :?:
Can you give some examples on what mean ?
something like if between 08:00-12:00 and lux between 100-500 then dimLevel 30 ?

Re: Light on at different dim level depending on daytime, with auto off

Posted: Sunday 17 February 2019 20:53
by tezzlicious
waaren wrote: Sunday 17 February 2019 18:14
tezzlicious wrote: Sunday 17 February 2019 14:07 How would this script look like when you want to do this depending on lux level ranges from a lux device within time slots :?:
Can you give some examples on what mean ?
something like if between 08:00-12:00 and lux between 100-500 then dimLevel 30 ?

Yes, exactly that. I have groups, possible to dim them as a group? Or I can just make scenes and trigger them instead of dim level?

Re: Light on at different dim level depending on daytime, with auto off

Posted: Sunday 17 February 2019 23:35
by waaren
tezzlicious wrote: Sunday 17 February 2019 20:53 something like if between 08:00-12:00 and lux between 100-500 then dimLevel 30 ?
Yes, exactly that. I have groups, possible to dim them as a group? Or I can just make scenes and trigger them instead of dim level?
Dim as a group to a variable dimLevel is not natively possible in domoticz but with dzVents you can loop over devices in a group to do this
See attached

Code: Select all

return {
	on = {
		devices = {'PIR'}, -- Change out PIR
	},
   
   execute = function(dz, PIR)
        local Lux           = dz.devices('Lux') 
        local dimmerGroup   = dz.groups("dimGroup")
        
        local dimTimeTable  = { --  [   'timeSlot'   ]  = { [measuredLux]   = dimLevel 
                                    ['at 00:01-08:00']  = { 
                                                                [200]       = 2,
                                                                [300]       = 6,
                                                                [500]       = 9,
                                                           },
                                    ['at 08:01-12:00']  = {     
                                                                [300]       = 20,
                                                                [500]       = 40,
                                                                [900]       = 60,
                                                           },
                                    ['at 12:01-23:00']  = { 
                                                                [800]       = 30,
                                                                [1200]      = 60,
                                                                [3000]      = 90,
                                                           },
                                    ['at 23:01-24:00']  = {
                                                                [0]         = 15    
                                                          },
                              }
                              
        function getDimlevelBasedOnLux(t, measuredLux)
            local delta = 999999
            local keyDimLevel = 1
            for key, dimLevel in pairs(t) do
                if math.abs(measuredLux - key) < delta then
                    delta = math.abs(measuredLux - key)
                    keyDimLevel = key
                
                end
            end
            return t[keyDimLevel]
        end                      
        
        function dimDevicesInGroup(groupName,dimValue)
            dz.groups(groupName).devices().forEach(function(device)
                device.cancelQueuedCommands()
                device.dimTo(dimValue)
                dz.log(device.name .. ' switched On (dimlevel: ' .. dimValue .. ')') 
            end)    
        end
        
        function switchOffDevicesInGroup(groupName,delay)
            dz.groups(groupName).devices().forEach(function(device)
                 device.switchOff().afterSec(delay)
                 dz.log(device.name .. ' will be switched Off after ' .. delay .. ' seconds.') 
            end)    
        end

        if PIR.state ~= 'On' then 
            switchOffDevicesInGroup(dimmerGroup.name,6)
            dz.log("Lights in " .. dimmerGroup.name .. ' will be switched Off after one minute')
        else
            for timeSlot, luxLevels in pairs (dimTimeTable) do
               if dz.time.matchesRule(timeSlot) then 
                    local dimValue = getDimlevelBasedOnLux(luxLevels, Lux.lux )
                    dz.log('Devices in ' .. dimmerGroup.name .. ' will be switched On (dimlevel: ' .. dimValue .. '); Based on: timeslot ('.. timeSlot .. '), measured Lux (' .. Lux.lux ..')') 
                    dimDevicesInGroup(dimmerGroup.name,dimValue)
                    return false
               end      
            end
        end     
    end
}

Re: Light on at different dim level depending on daytime, with auto off

Posted: Monday 25 February 2019 14:42
by tezzlicious
waaren wrote: Sunday 17 February 2019 23:35 Dim as a group to a variable dimLevel is not natively possible in domoticz but with dzVents you can loop over devices in a group to do this
See attached
Thanks waaren. Managed to test it. It seems to work, but a few things I noticed:
  • Using the following time slots:

    Code: Select all

            local dimTimeTable  = { --  [   'timeSlot'   ]  = { [measuredLux]   = dimLevel 
                                        ['at 00:01-15:00']  = { 
                                                                    [2]       = 70,
                                                                    [3]       = 70,
                                                                    [4]       = 70,
                                                                    [5]       = 70,
                                                                    [6]       = 80,
                                                                    [7]       = 80,																										 
                                                                    [8]       = 80,
                                                                    [9]       = 80,
                                                                    [10]       = 80,
                                                                    [12]       = 90,
                                                                    [15]       = 90,
                                                               },
    


    it still switches the lights on to 90% at a lux level of 73 at the moment.

  • I have a dummy on/off switch which groups 4 spots. The dummy switch is also in the group which I use in your script. When your script kicks in it switches this dummy switch to off. Where it should switch it to on.
  • How does your script handle the lux levels exactly? What if I want to match a range of lux level to the same dim level? Like the 0-5 lux range to dim level 70 and 6-10 to 80?
  • Is it possible to embed a threshold to switch off the lights above a certain lux level? Or does it work like that already? I didn't notice that maybe?
Thanks again. Already helped me a lot.

Re: Light on at different dim level depending on daytime, with auto off

Posted: Monday 25 February 2019 16:58
by waaren
tezzlicious wrote: Monday 25 February 2019 14:42
Thanks waaren. Managed to test it. It seems to work, but a few things I noticed:
  • Using the following time slots:

    Code: Select all

            local dimTimeTable  = { --  [   'timeSlot'   ]  = { [measuredLux]   = dimLevel 
                                        ['at 00:01-15:00']  = { 
                                                                    [2]       = 70,
                                                                    [3]       = 70,
                                                                    [4]       = 70,
                                                                    [5]       = 70,
                                                                    [6]       = 80,
                                                                    [7]       = 80,																										 
                                                                    [8]       = 80,
                                                                    [9]       = 80,
                                                                    [10]       = 80,
                                                                    [12]       = 90,
                                                                    [15]       = 90,
                                                               },
    

    it still switches the lights on to 90% at a lux level of 73 at the moment.
Correct. The script picks the percentages from the lux level that is closest. If you want to a different percentage above a certain level you should add that combination. eg if you want a dimlevel percentage of 0 when lux >= 50 you should add something like
[49] = 90,
[50] = 0,
to the dimTimeTable
  • I have a dummy on/off switch which groups 4 spots. The dummy switch is also in the group which I use in your script. When your script kicks in it switches this dummy switch to off. Where it should switch it to on.

The script is not designed for an on/off switch. It is designed and tested for dimmable devices.
  • How does your script handle the lux levels exactly? What if I want to match a range of lux level to the same dim level? Like the 0-5 lux range to dim level 70 and 6-10 to 80?

set dimTimeTable to
local dimTimeTable = { -- [ 'timeSlot' ] = { [measuredLux] = dimLevel
['at 00:01-15:00'] = {
[5] = 70, -- lux 0-5 will set dim% to 70
[6] = 80, -- lux 6-10 will set dim% to 80
[10] = 80, --
[11] = othervalue, -- lux 11 and above wil set dim% to othervalue
  • Is it possible to embed a threshold to switch off the lights above a certain lux level? Or does it work like that already? I didn't notice that maybe?
set dimTimeTable to
local dimTimeTable = { -- [ 'timeSlot' ] = { [measuredLux] = dimLevel
['at 00:01-15:00'] = {
[5] = 70,
[6] = 80,
[11] = 0,

Re: Light on at different dim level depending on daytime, with auto off

Posted: Monday 25 February 2019 22:56
by tezzlicious
Thanks waaren. Working perfectly now.

Re: Light on at different dim level depending on daytime, with auto off

Posted: Sunday 28 July 2019 23:04
by jaaap
Hi, this is exactly why I started using Domoticz in the first place :-) So good job!

However, I'm a huge n00b in this area and I need a little guidance, since I don't get the script to work. My question is: do I need to create something in order to make this work? For instance a dummy switch, groups, anything?

Or, mayby I don't understand the working of the script :-) As I understand it: lights are turned off, then when I switch them on, they flip on at a certain dim level, depending on the time. Correct?

Hope you can help!

Re: Light on at different dim level depending on daytime, with auto off

Posted: Monday 29 July 2019 2:13
by waaren
jaaap wrote: Sunday 28 July 2019 23:04 Or, maybe I don't understand the working of the script :-) As I understand it: lights are turned off, then when I switch them on, they flip on at a certain dim level, depending on the time. Correct?
The script is triggered by a motion sensor and set a dimmable light to a certain dim level based on a combination of measured Lux and time of day. So to get it working, you need a Lux measuring device (real or virtual), - a motion sensor (can also be a switch) and a dimmable light.

Re: Light on at different dim level depending on daytime, with auto off

Posted: Sunday 11 August 2019 13:34
by jaaap
What device do you use for the PIR? I mean, what hardware?

Re: Light on at different dim level depending on daytime, with auto off

Posted: Sunday 11 August 2019 17:07
by waaren
jaaap wrote: Sunday 11 August 2019 13:34 What device do you use for the PIR? I mean, what hardware?
I use three different types. I started with a KlikAan KlikUit (RFtrx433 /RFLink). Then I tried a Fibaro type (zwave) and now I use Xiaomi Mijia IR Intelligent Human Body Sensors.
They all do the job so that's why, for now, I stick to the last one (cheapest)

Re: Light on at different dim level depending on daytime, with auto off

Posted: Sunday 11 August 2019 17:46
by jaaap
Great, then I'll order a couple of those, with the gateway of course :-)

Verstuurd vanaf mijn POCOPHONE F1 met Tapatalk



Re: Light on at different dim level depending on daytime, with auto off

Posted: Wednesday 29 January 2020 16:13
by botany
is it possible to change dim level manual ?
and if there is movement. lights stay on same level as i put it manually?
because sometimes its nice to change the dim level depending on the situation .

Re: Light on at different dim level depending on daytime, with auto off

Posted: Thursday 30 January 2020 1:06
by waaren
botany wrote: Wednesday 29 January 2020 16:13 is it possible to change dim level manual ?
and if there is movement. lights stay on same level as i put it manually?
because sometimes its nice to change the dim level depending on the situation .
Yes that is possible. My approach would be to use a virtual dimmer as override that sets the dimlevel of the devices in the group to the same level as the virtual dimmer and use that same virtual dimmer as override if it is not 'Off' or has dimlevel 0.

Something like

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'PIR',
            'virtualDimmer',
        }, 
    },
    
    logging =   
    {   
        level = domoticz.LOG_DEBUG,  -- set to error when script is tested and OK
        marker = conditionalDimmer,
    },

    execute = function(dz, item)
        local Lux           = dz.devices('Lux') 
        local dimmerGroup   = dz.groups('dimGroup')
        local override = dz.devices('virtualDimmer')
        
        local dimTimeTable  = { --  [   'timeSlot'   ]  = { [measuredLux]   = dimLevel 
                                    ['at 00:01-08:00']  = { 
                                                                [200]       = 2,
                                                                [300]       = 6,
                                                                [500]       = 9,
                                                           },
                                    ['at 08:01-12:00']  = {     
                                                                [300]       = 20,
                                                                [500]       = 40,
                                                                [900]       = 60,
                                                           },
                                    ['at 12:01-23:00']  = { 
                                                                [800]       = 30,
                                                                [1200]      = 60,
                                                                [3000]      = 90,
                                                           },
                                    ['at 23:01-24:00']  = {
                                                                [0]         = 15    
                                                          },
                              }
                              
        function getDimlevelBasedOnLux(t, measuredLux)
            local delta = 999999
            local keyDimLevel = 1
            for key, dimLevel in pairs(t) do
                if math.abs(measuredLux - key) < delta then
                    delta = math.abs(measuredLux - key)
                    keyDimLevel = key
                
                end
            end
            return t[keyDimLevel]
        end                      
        
        function dimDevicesInGroup(groupName,dimValue)
            dz.groups(groupName).devices().forEach(function(device)
                device.cancelQueuedCommands()
                device.dimTo(dimValue)
                dz.log(device.name .. ' switched On (dimlevel: ' .. dimValue .. ')',dz.LOG_DEBUG) 
            end)    
        end
        
        function switchOffDevicesInGroup(groupName,delay)
            dz.groups(groupName).devices().forEach(function(device)
                 device.switchOff().afterSec(delay)
                 dz.log(device.name .. ' will be switched Off after ' .. delay .. ' seconds.',dz.LOG_DEBUG) 
            end)    
        end
        
        if item.switchType == 'Dimmer' then 
            if item.state ~= 'Off' then 
                dimDevicesInGroup(dimmerGroup.name, item.level)
            else
                dimDevicesInGroup(dimmerGroup.name, 0)
            end
        elseif item.state ~= 'On' and override.state ~= 'Off' then 
            switchOffDevicesInGroup(dimmerGroup.name,60)
            dz.log('Lights in ' .. dimmerGroup.name .. ' will be switched Off after one minute',dz.LOG_DEBUG)
        elseif override.state ~= 'Off' then
            for timeSlot, luxLevels in pairs (dimTimeTable) do
               if dz.time.matchesRule(timeSlot) then 
                    local dimValue = getDimlevelBasedOnLux(luxLevels, Lux.lux )
                    dz.log( 'Devices in ' .. dimmerGroup.name .. ' will be switched On (dimlevel: ' .. dimValue .. 
                            '); Based on: timeslot ('.. timeSlot .. '), measured Lux (' .. Lux.lux ..')',dz.LOG_DEBUG) 
                    dimDevicesInGroup(dimmerGroup.name,dimValue)
                    return false
               end      
            end
        else
            dz.log('Groupdim level is overrided by ' .. override.name .. ' and fixed at level ' .. override.level ,dz.LOG_DEBUG) 
        end     
    end
}
(not tested)

Re: Light on at different dim level depending on daytime, with auto off

Posted: Thursday 30 January 2020 14:53
by botany
thanks.
will try it out tonight.

I'm coming from Fibaro. trying to get out of the close system of them.
was only possible to use (expensive) zwave module/hardware.

i used a scene with fibaro made by Sankotronic.
Spoiler: show
very smart light- https://forum.fibaro.com/topic/28726-ve ... hts-scene/
now im looking for same scene like his.

so i can switch to Domoticz. this was the only scene is used with Fibaro. others where on and off and time based.

Re: Light on at different dim level depending on daytime, with auto off

Posted: Thursday 30 January 2020 15:13
by waaren
botany wrote: Thursday 30 January 2020 14:53 I'm coming from Fibaro. trying to get out of the close system of them.
Not only the system is closed; also the forum :) I am not allowed to view the topic without a user-id...

Re: Light on at different dim level depending on daytime, with auto off

Posted: Thursday 30 January 2020 15:31
by botany
https://drive.google.com/open?id=1AtGnK ... KNUvYTC3Ct

i download it for ya... if you like to look at it.. :roll:

Re: Light on at different dim level depending on daytime, with auto off

Posted: Thursday 30 January 2020 15:39
by botany
getting a error
2020-01-30 15:36:50.683 ...domoticz/scripts/dzVents/generated_scripts/dzevents3.lua:8: '}' expected (to close '{' at line 6) near ''virtualDimmer''

Code: Select all

return 
{
    on = 
    {
        devices = 
        {
            'Koof Sensor'
            'virtualDimmer',
        }, 
    },
    
    logging =   
    {   
        level = domoticz.LOG_DEBUG,  -- set to error when script is tested and OK
        marker = conditionalDimmer,
    },

    execute = function(dz, item)
        local Lux           = dz.devices('KoofLux') 
        local dimmerGroup   = dz.groups('Living room')
        local override = dz.devices('virtualDimmer')
        
        local dimTimeTable  = { --  [   'timeSlot'   ]  = { [measuredLux]   = dimLevel 
                                    ['at 00:01-08:00']  = { 
                                                                [30]       = 2,
                                                                [30]       = 6,
                                                                [30]       = 9,
                                                           },
                                    ['at 08:01-12:00']  = {     
                                                                [30]       = 20,
                                                                [30]       = 40,
                                                                [30]       = 60,
                                                           },
                                    ['at 12:01-23:00']  = { 
                                                                [30]        = 30,
                                                                [30]        = 60,
                                                                [30]        = 90,
                                                           },
                                    ['at 23:01-24:00']  = {
                                                                [30]         = 15    
                                                          },
                              }
                              
        function getDimlevelBasedOnLux(t, measuredLux)
            local delta = 999999
            local keyDimLevel = 1
            for key, dimLevel in pairs(t) do
                if math.abs(measuredLux - key) < delta then
                    delta = math.abs(measuredLux - key)
                    keyDimLevel = key
                
                end
            end
            return t[keyDimLevel]
        end                      
        
        function dimDevicesInGroup(groupName,dimValue)
            dz.groups(groupName).devices().forEach(function(device)
                device.cancelQueuedCommands()
                device.dimTo(dimValue)
                dz.log(device.name .. ' switched On (dimlevel: ' .. dimValue .. ')',dz.LOG_DEBUG) 
            end)    
        end
        
        function switchOffDevicesInGroup(groupName,delay)
            dz.groups(groupName).devices().forEach(function(device)
                 device.switchOff().afterSec(delay)
                 dz.log(device.name .. ' will be switched Off after ' .. delay .. ' seconds.',dz.LOG_DEBUG) 
            end)    
        end
        
        if item.switchType == 'Dimmer' then 
            if item.state ~= 'Off' then 
                dimDevicesInGroup(dimmerGroup.name, item.level)
            else
                dimDevicesInGroup(dimmerGroup.name, 0)
            end
        elseif PIR.state ~= 'On' and override.state ~= 'Off' then 
            switchOffDevicesInGroup(dimmerGroup.name,60)
            dz.log('Lights in ' .. dimmerGroup.name .. ' will be switched Off after one minute',dz.LOG_DEBUG)
        elseif override.state ~= 'Off' then
            for timeSlot, luxLevels in pairs (dimTimeTable) do
               if dz.time.matchesRule(timeSlot) then 
                    local dimValue = getDimlevelBasedOnLux(luxLevels, Lux.lux )
                    dz.log( 'Devices in ' .. dimmerGroup.name .. ' will be switched On (dimlevel: ' .. dimValue .. 
                            '); Based on: timeslot ('.. timeSlot .. '), measured Lux (' .. Lux.lux ..')',dz.LOG_DEBUG) 
                    dimDevicesInGroup(dimmerGroup.name,dimValue)
                    return false
               end      
            end
        else
            dz.log('Groupdim level is overrided by ' .. override.name .. ' and fixed at level ' .. override.level ,dz.LOG_DEBUG) 
        end     
    end
}