Boolean Casing hours for tariffs

Discussion in 'Pascal Logic Code Examples' started by Inverness Smart lec, Sep 6, 2019.

  1. Inverness Smart lec

    Inverness Smart lec

    Joined:
    Jan 11, 2017
    Messages:
    7
    Likes Received:
    0
    Location:
    Inverness
    Hi everyone I really need some help trying to make my metering more accurate, I've used an example from the Cbus tutorial pages to try to achieve what I want but its not perfect.
    I've set two tariffs to match on peak and off peak hours that change at different hours throughout the day example below. This works really well and does what's intended.

    Problem 1.
    The thing is that in the UK the on peak and off peak hours start on the half past and not on the hour. So I've been trying to case the half hour and not the hour but I can't seem to get anything to work.

    Also

    Problem 2.
    Daylight Saving the meters don't follow the daylight savings time so the on peak and off peak hours will off set by an hour depending on the time of the year.

    Really hope someone can help thanks Chris

    Global Vairables
    Tariff,Tariffcost:real;

    Modual
    Tariffcost:= GetRealIBSystemIO("Energy Tariff", "General");
    case hour of
    0,1,2,3:
    Tariff := 0.1281;
    4,5,6:
    Tariff := 0.0732;
    7,8,9,10,11,12:
    Tariff := 0.1281;
    13,14,15:
    Tariff := 0.0732;
    16,17,18,19:
    Tariff := 0.1281;
    20,21,22,23:
    Tariff := 0.0732;

    end;

    SetRealIBSystemIO("Energy Tariff", "General", Tariff);

    delay("0:30:00"); // only recalculate every 1/2 hour
     
    Inverness Smart lec, Sep 6, 2019
    #1
    1. Advertisements

  2. Inverness Smart lec

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    950
    Likes Received:
    65
    Location:
    Adelaide, Australia
    Using a delay like that is not accurate because you delay after your code has run, so the total delay is 30 minutes plus however long the code takes rounded up to the next 200 mSecs when the next scan kicks in.

    Simply get rid of the delay and enclose the logic in a :

    Once minute=30 then
    begin
    ...your code...
    end;

    This will run every hour on the 1/2 hour.

    As for daylight savings, you can get the current state via a system io call. If it is set, just subtract 1 hour from the current time before using it in the case statement.
     
    Ashley, Sep 7, 2019
    #2
    Inverness Smart lec and Mr Mark like this.
    1. Advertisements

  3. Inverness Smart lec

    Inverness Smart lec

    Joined:
    Jan 11, 2017
    Messages:
    7
    Likes Received:
    0
    Location:
    Inverness
    Ashley I’m glad it was you that replied I’ve seen some of your reply’s and there good, but you've actually just blown me away right now!

    So instead of

    case hour 0,1,2,3:
    Tariff:=0.1281

    Where going with

    Once Minuit = 30 then
    Begin
    Tariff:=0.1281

    Will this still allow me to create my tariff periods? my off peak actually starts at 00:30 till 03:30 and on peak starts 3:30 to 06:30 and so on? But at the moment it’s starting the off peak recalculation from 00:00. How will it know which half hour of what hour to start the tariff on?

    I kind of want to case half hours rather then hours

    So An example that I’ve tried but doesn’t work would be

    Case hour and Minuit of
    0.30,1,2,3:
    Tariff:=0.1281;
    3.30,4,5,6:
    Tariff:=0.0732;


    Thank you for everything
     
    Inverness Smart lec, Sep 7, 2019
    #3
  4. Inverness Smart lec

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    950
    Likes Received:
    65
    Location:
    Adelaide, Australia
    The statement "ONCE minute = 30 then" will become true when minute(s) is exactly 30, so it can only trigger on the 1/2 hour (not hour because minute will be 0). Once it has triggered, it will not trigger again until the minute goes away from 30 (i.e. 31) then back to 30 exactly 1 hour later. So it will run exactly every hour on the 30 minute interval. Your case statement will still work since the hour value will be the same at the 30 minute interval.

    Make sure you spell 'minute' correctly :)

    'case hour and minute' is kind of meaningless. The 'and' takes 2 booleans, and an integer is true if non zero and false if zero. So that statement will execute whenever both hour and minute are non zero, which is not what you want.

    So what you need is:

    Code:
    once minute=30 then
    begin
      case hour of
          0,1,2,3:   Tariff := 0.1281;
          4,5,6:  Tariff := 0.0732;
          7,8,9,10,11,12:  Tariff := 0.1281;
         13,14,15:  Tariff := 0.0732;
         16,17,18,19: Tariff := 0.1281;
         20,21,22,23:  Tariff := 0.0732;
        end;
    end;
    or simply:

    Code:
    once minute=30 then
      if hour in  [4,5,6,13,14,15,20,21,22,23] then
          Tariff := 0.0732
       else
          Tariff := 0.1281;
    
     
    Ashley, Sep 7, 2019
    #4
    Inverness Smart lec and Mr Mark like this.
    1. Advertisements

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.