Please help with Logic Code.

Discussion in 'General Discussion' started by KK06, Jul 18, 2010.

  1. KK06

    KK06

    Joined:
    Sep 19, 2006
    Messages:
    22
    Likes Received:
    0
    Currently I had 2 PIRs (multi-sensor 5753PEIRL) one controls the walkway light and the other controls the toilet light. I had a PAC.

    Objective:
    I wanted between 2am and 6am, whenever i go to toilet the walkway and toilet lights turn on or ramp on at 20% for 2 mintues and then turn off.

    I was using the PICED logic wizard and the code as follows:

    {
    after 2am and before 6am when walkway light on turn
    walkway light and toilet light at 20% for 2 mins
    }
    if ((Time > "2:00:00 AM") and
    (Time < "6:00:00 AM")) and
    (GetLightingState("walkwaylight") = ON) then
    begin
    PulseCBusLevel("Local Network", "Lighting", "walkwaylight", 20%, 4, "0:02:00", 0%);
    PulseCBusLevel("Local Network", "Lighting", "toiletlight", 20%, 4, "0:02:00", 0%);
    end;

    Problems:
    Whenever I walk through the walkway light will turn on 100% before it ramps back to 20% (i knew that would happen) however I am trying to write a code that once the PIR senses me walking through the walkway then it starts to ramp to 20% rather than 100% then drops back to 20%.

    Somehow I am thinking of getting the PIR state as a conditional statement that might be it. But don?t know how. Please help with the code..
     
    KK06, Jul 18, 2010
    #1
  2. KK06

    Newman

    Joined:
    Aug 3, 2004
    Messages:
    2,203
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    You're having trouble because you have the PIR and the logic code sending messages out on the same group address. The PIR is turning on the light at full brightness and then the logic is ramping it down.

    There are a couple of ways to deal with this.

    One way is to limit the brightness of the dimmer channel at certain times of the day. Set up a logic group in the dimmer using MIN logic and then set a schedule up in your logic unit to control the level of this logic group. Set it to 20% at 2am and set it to 100% at 6am.

    Another way is to make the PIR control a group address that is different to the actual channel groups. You could call it "Walkway logical group" or similar. Then, in your logic code, whenever this group turns on you set the walkway group to the appropriate level.
     
    Newman, Jul 18, 2010
    #2
  3. KK06

    KK06

    Joined:
    Sep 19, 2006
    Messages:
    22
    Likes Received:
    0
    thanks for the reply Newman,

    I will have a go at it.
     
    KK06, Jul 20, 2010
    #3
  4. KK06

    KK06

    Joined:
    Sep 19, 2006
    Messages:
    22
    Likes Received:
    0
    Hi Newman,

    Can you please elaborate more on the 1st option? Because when I set the schedule in the PAC unit;

    at 2am set walkway light at 20% and 100% at 6am, what happened was at 2am it turned on at 20% by itself.

    However I want it to turn ?on? at 20% only & when the PIR initiates it.

    Also can you please show me how to use the MIN logic in the dimmer? I have set the dimmer ?MIN? at 20% then I don?t know what to do next.

    Thanks
     
    KK06, Jul 21, 2010
    #4
  5. KK06

    Newman

    Joined:
    Aug 3, 2004
    Messages:
    2,203
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    OK, there's a few steps.

    Firstly, assign the motion sensor so that it controls the dimmer channel like normal. By this I mean that the dimmer channel and the motion sensor are using the same C-Bus group address and the motion sensor is able to turn the hallway light on and off when it detects motion.

    Secondly, on the logic tab of the dimmer, tick the box that links the Walkway Light channel with a free Logic Group. Then, click on the green "+" button next to the logic group to create a new one. Call it something like Walkway Logic. Make sure that the radio button for that channel is set to Min.

    Thirdly, set up a schedule that sets the Walkway Logic group to 20% at 2am and to 100% at 6am.

    Lastly, the new Walkway Logic group will be Off, so the dimmer channel will not turn on even if the PIR detect motion initially. It will start working when the Walkway Logic group turns on the first time, according to your schedule. To avoid the need to wait until 2am you can turn the Walkway Logic group on from toolkit by right clicking on the Walkway Logic group and selecting 'Set Group On'. This will allow you to get the Walkway light working again without having to wait for the scheduled event to tick over.

    The way MIN logic works is that it compares the normal channel group with any of the Min logic groups associated with that channel and chooses the lowest value. If your Walkway group is set to 100% and the Walkway Logic group is set to 20%, you'll get 20%. If your Walkway group is set to 50% and the Walkway Logic group is set to 100%, you'll get 50%. The initial state of the Walkway Logic group when we create it is Off. The minimum value of Off & anything else is Off. That's why we need to turn on the group from Toolkit to test it... or wait for the Scheduled event to roll over. Make sense?
     
    Newman, Jul 21, 2010
    #5
  6. KK06

    Phil Summers

    Joined:
    Jun 26, 2009
    Messages:
    41
    Likes Received:
    0
    Location:
    UK
    and one last job for robustness...

    I also find that I need to put a line of code in the initialisation section of the logic to set the "Walkway Logic" group to 100% after a power failure, otherwise it will sometimes default to 0% and then the lights wouldn't come on (until the schedule had run).

    There's probably a clever way of acheiving the same result by storing the value of "Walkway logic" in an unused channel and relying on MMI to sort out the discrepancy after a power failure. I find that using logic makes it easier for others to see what I'm up to.

    The initialisation code runs once when the logic-containing unit (PAC/Ctouch/whatever) is first powered up. It does NOT get called every 200mS like the modules. (PS I call my group "Night Mode Brightness Limit" cos I think that's more descriptive).

    Hope the following is not teaching you to suck eggs...


    • open the logic editor in PICED


    • Open the initialisation code window in the pane at the top left (See attached image)


    • In the logic window you'll need a line like

    • SetLightingState("Walkway Logic Group", ON);

    logic.JPG
     
    Last edited by a moderator: Jul 21, 2010
    Phil Summers, Jul 21, 2010
    #6
  7. KK06

    NickD Moderator

    Joined:
    Nov 1, 2004
    Messages:
    1,420
    Likes Received:
    62
    Location:
    Adelaide
    A good idea, although if you have the "Auto Level Store" box checked under "Logic Recovery", then the logic group level should restore to its previous value.

    That said, we have lots of power failures at my place and the usual problem with this setup is that the power is restored after the time for the schedule that sets the level of the logic group back to 100% has passed, so the lights are still dim in the morning.

    A better plan might be to have a piece of code like :

    Code:
    once (Time >= "2:00AM") then 
      begin
        SetLightingState("Walkway Logic Group", ON);
      end;
    
    I've been meaning to do this for a while... thanks for reminding me :)

    Nick
     
    Last edited by a moderator: Jul 22, 2010
    NickD, Jul 22, 2010
    #7
  8. KK06

    KK06

    Joined:
    Sep 19, 2006
    Messages:
    22
    Likes Received:
    0
    Thank you all for replying and helping.

    I have used Newman's method and works beautifully, thanks again.

    Phil, I'll also use the initialisation code in case of power failure. Eventhough I haven't experinence any power failure yet.

    Cheers...
     
    KK06, Jul 23, 2010
    #8
  9. KK06

    Ambro

    Joined:
    Nov 23, 2010
    Messages:
    104
    Likes Received:
    1
    Location:
    Adelaide
    I've set up this code to run instead of a schedule to get the lights to dim at certain times :

    Code:
    once (Time >= sunset + "0:30:00") then
    begin
        SetLightingLevel("Entrance Hall - Nite Dim Logic", 35%, "15m");
    end;
    
    once (Time >= "12:00:00 AM") and
         (Time < sunrise + "0:15:00") then
    begin
        SetLightingLevel("Entrance Hall - Nite Dim Logic", 35%, "0s");
    end;
    
    once (Time >= sunrise + "0:15:00") and
         (Time < sunset + "0:30:00") then
    begin
        SetLightingLevel("Entrance Hall - Nite Dim Logic", 100%, "2m");
    end;
    
    It seems to work when the times are meet and the CTC is running; however when I simulate a power failure by hitting the reset on the CTC the code doesn't seem to kick in. Shouldn't the module by parsed on the first run of the CTC being powered up and deliver the appropriate levels or have I overlooked something?

    Cheers,
    Ambro
     
    Last edited by a moderator: Nov 17, 2011
    Ambro, Nov 17, 2011
    #9
  10. KK06

    Darren Senior Member

    Joined:
    Jul 29, 2004
    Messages:
    2,361
    Likes Received:
    0
    Location:
    Adelaide, South Australia
    I suspect that parts of this are not what you intend.

    This code will set the Entrance Hall group (which appears to be used with logic in an output unit to set a maximum level at night) to 35% 30 minutes after sunset. This is probably what is required.

    Code:
    once (Time >= sunset + "0:30:00") then
    begin
        SetLightingLevel("Entrance Hall - Nite Dim Logic", 35%, "15m");
    end;
    
    This code will set the Entrance Hall group to 35% at midnight (even though it is probably already at that level). This is probably not what is intended.

    Code:
    once (Time >= "12:00:00 AM") and
         (Time < sunrise + "0:15:00") then
    begin
        SetLightingLevel("Entrance Hall - Nite Dim Logic", 35%, "0s");
    end;
    
    This code will set the Entrance Hall group to 100% 15 minutes after sunrise. This is probably what is required.

    Code:
    once (Time >= sunrise + "0:15:00") and
         (Time < sunset + "0:30:00") then
    begin
        SetLightingLevel("Entrance Hall - Nite Dim Logic", 100%, "2m");
    end;
    
    From the logic help file topic "once statement":

    If the condition is true when the Logic Engine first runs, the statement will not be executed. The condition needs to change from false to true in order for the statement to be executed.

    To make this code put the group to the correct level even if the CTC powers up after having missed the sunrise or sunset change, you will need to change the code to something like this:

    Code:
    if (Time >= sunset + "0:30:00") or (Time < sunrise + "0:15:00") then
    begin
      if GetLightingLevel("Entrance Hall - Nite Dim Logic") <> 35% then    
        SetLightingLevel("Entrance Hall - Nite Dim Logic", 35%, "15m");
    end
    else
    begin
      if GetLightingLevel("Entrance Hall - Nite Dim Logic") <> 100% then    
        SetLightingLevel("Entrance Hall - Nite Dim Logic", 100%, "2m");
    end;
    If it is critical that the ramp rate be 0s for setting to 35% if the time is after midnight, then add an extra couple of lines of code to do that.

    I would be inclined to just use schedules for this sort of thing. There is a Schedule Sync process you can use with Colour C-Touch to ensure that everything is at the right level after start-up, but it is only ever going to be needed if there is a power failure during the sunrise/sunset transition time. Have a think about whether it might be best to keep it really simple and accept that once in a decade it might fail.
     
    Darren, Nov 17, 2011
    #10
  11. KK06

    Robbo_VIC

    Joined:
    Jan 24, 2011
    Messages:
    142
    Likes Received:
    0
    Location:
    Melbourne, VIC
    What about this option? Where _ToiletSensorGroup_ is a dummy group address that you assign to the sensor, which gets pulsed for 3 seconds when it triggers

    Code:
    once (GetLightingState("_ToiletSensorGroup_") = ON)   and
         ((Time > "2:00:00 AM") and
         (Time < "6:00:00 AM")) then
    begin
      PulseCBusLevel("local", "Lighting", "walkwaylight", 20%, 4, "0:02:00", 0%);
      PulseCBusLevel("local", "Lighting", "toiletlight", 20%, 4, "0:02:00", 0%);
    end;
    
    once (GetLightingState("_ToiletSensorGroup_") = ON)   and
         ((Time < "2:00:00 AM") or
         (Time > "6:00:00 AM")) then
    begin
      PulseCBusLevel("local", "Lighting", "walkwaylight", 100%, 4, "0:02:00", 0%);
      PulseCBusLevel("local", "Lighting", "toiletlight", 100%, 4, "0:02:00", 0%);
    end;
    
     
    Robbo_VIC, Nov 20, 2011
    #11
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.