more G-Labs products

Author Topic: Multiple "When.ModuleParameterIsChanging" events?  (Read 1654 times)

December 24, 2014, 08:03:22 AM
Read 1654 times

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I've noticed that HG is experiencing multiple When.ModuleParameterIsChanging events with a single switch command.  Is there something new in the code that would cause this?  I have code that relies on a single event for a single command and this breaks things.  I haven't noticed this in the past, but I'm not sure if this is new or just not noticed.

This is from my Advanced Smart Lights code:
Code: [Select]
20141223 22:49:59.1426360 ; 0.1-module=Entry switch ; Status.Level= 1
20141223 22:49:59.6281330 ; 2-Entry switch ; Status.Level ; 1
20141223 22:50:01.6509450 ; 3-Entry Lights ; ON
20141223 22:50:01.6576780 ; 8.5-event=1 ; LAST_EVENT=1 ; double timer=TRUE
20141223 22:50:01.6621340 ; 9-Entry switch ; Consecutine ON detected
20141223 22:50:01.6663300 ; 10-Double switch ON
20141223 22:50:01.7136960 ; 0.1-module=Entry Lights ; Status.Level= 1
20141223 22:50:02.7250980 ; 11-Entry Lights ; ON
20141223 22:50:02.7316710 ; 8.5-event=1 ; LAST_EVENT=1 ; double timer=
20141223 22:50:02.7358990 ; 8.5-event=1 ; LAST_EVENT=1 ; double timer=FALSE

I sent a single "off" command from HG.  As can be seen, HG sees 2 off commands for some reason (the 0.1 lines).  I am continuing to review my code to determine if I changed something to cause this, but I know it wasn't an issue as of a few weeks ago.  If something changed in the HG code, that might explain it.

December 25, 2014, 03:25:37 AM
Reply #1

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
It looks like the issue is not multiple events so much as that I have an issue with LastValue and LastUpdateTime.  I have a timer set up such that if two instances of a given command are received within that elapsed time, a "double trigger" is detected.  The issue I have just started seeing is that when a single command is received, my code is detecting it as though it's multiple.

The issue I see is that it appears that the script ID seems to indicate the order in which the code runs.  If script1 is ID 1000 and script2 is ID 1001, then I believe script1 will run first.  However, if script1 is somehow moved to ID 1002, it will then run second.

As a result to my scripts, if my Advanced Smart Lights runs after LastEvent, the module.Parameter("LastEvent.LastEvent").Value becomes the same as module.Parameter("LastEvent.LastEvent").LastValue and the same goes for UpdateTime and LastUpdateTime.  As a result, a single command makes my Advanced Smart Lights script believe that 2 instances of the same command were received at the same time creating a 0 second elapsed time.

I don't know the solution to this other than incorporating a few new parameters into the core HG code.  I am looking at this now, but I've never forked code so I don't know how difficult it might be.

December 25, 2014, 04:15:06 AM
Reply #2

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I think this would be all that's needed.  I haven't figured out how to compile so it's possible I missed something.  Let me know what you think.

ModuleParameter.cs

Code: [Select]
        public ModuleParameter()
        {
            // initialize
            Name = "";
            Value = "";
            Description = "";
            UpdateTime = DateTime.Now;
            //
            LastValue = "";
            LastUpdateTime = DateTime.Now;
            //
            LastEvent = "";
            LastEventUpdateTime = DateTime.Now;
            LastOn = "";
            LastOnUpdateTime = DateTime.Now;
            LastOff = "";
            LastOffUpdateTime = DateTime.Now;
        }

and

Code: [Select]
        public string Value
        {
            get
            {
                return parameterValue;
            }
            set
            {
                //TODO: find a better solution for "Meter.Watts" case
                //if (_value != value || Name == Properties.METER_WATTS || Name.StartsWith("ZWaveNode."))
                {
                    if ((!string.IsNullOrEmpty(parameterValue) && parameterValue != value)) // || Name == Properties.METER_WATTS || Name.StartsWith("ZWaveNode."))
                    {
                        //LastValue is unique
                        LastValue = parameterValue;
                        LastUpdateTime = UpdateTime.ToUniversalTime();
                    }
                    else if(!string.IsNullOrEmpty(parameterValue))
                    {
                        //LastEvent is last absolute command
                        LastEvent = parameterValue;
                        LastEventUpdateTime = UpdateTime.ToUniversalTime();

                        if (value == 1)
                        {
                            //LastOn is last time ON (1) was received
                            LastOn = 1;
                            LastOnUpdateTime = UpdateTime.ToUniversalTime();
                        }
                        else if (value == 0)
                        {
                            //LastOn is last time OFF (0) was received
                            LastOff = 0;
                            LastOffUpdateTime = UpdateTime.ToUniversalTime();
                        }
                    }
                    UpdateTime = DateTime.UtcNow;
                    parameterValue = value;
                    //
                    // can we add this value for statistics?
                    double v;
                    if (value != "" && StatisticsLogger.IsValidField(this.Name) && double.TryParse(value.Replace(",", "."), NumberStyles.Float | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out v))
                    {
                        Statistics.AddValue(v, this.UpdateTime);
                    }
                }
            }
        }
« Last Edit: December 25, 2014, 04:23:38 AM by bkenobi »

December 26, 2014, 05:15:21 PM
Reply #3

bkenobi

  • *****
  • Information
  • Global Moderator
  • Posts: 1525
I consider this topic incorrect.  There are not multiple events firing within HG.  I did locate a weakness of the system (in my opinion) and I added a github issue (#80).

December 31, 2014, 09:51:14 PM
Reply #4

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
Hi b,

see my reply to the issue #80.

Happy New Year =)

g.