Author Topic: "Cannot read Property error" when adding event  (Read 1108 times)

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
"Cannot read Property error" when adding event
« on: January 04, 2020, 12:23:12 PM »
In trying to create an event as shown in the following thread;

https://lowpowerlab.com/forum/pi-gateway/dynamic-node-specific-event-trigger/msg21232/#msg21232

I keep getting the following error in the gateway.sys.log;

Code: [Select]
[01-04-20_10:53:30.630] [WARN]   Event tempTooColdSMSLimiter execution failed: Cannot read property 'lowTempValue' of undefined
[01-04-20_10:53:30.633] [WARN]   Event tempTooHotSMSLimiter execution failed: Cannot read property 'highTempValue' of undefined

The following was added to exports.events;

Code: [Select]
tempTooColdSMSLimiter : { label:'TooColdAlert : SMS', icon:'comment', descr:'Send SMS when Temp is < than the lowValue setting, once per 30 min.',
    serverExecute:function(node) {
      if (node.metrics['F'] && node.metrics['F'].value < node.settings['lowTempValue'] && (Date.now() - node.metrics['F'].updated < 2000)) /*check if M metric exists and value is MOTION, received less than 2s ago*/
      {
        var approveSMS = false;
        if (node.metrics['F'].lastSMS) /*check if lastSMS value is not NULL ... */
        {
          if (Date.now() - node.metrics['F'].lastSMS > 1800000) /*check if lastSMS timestamp is more than 1hr ago */
          {
            approveSMS = true;
          }
        }
        else
        {
          approveSMS = true;
        }
        if (approveSMS)
        {
          node.metrics['F'].lastSMS = Date.now();
          sendSMS('Too cold!', 'Temperature alert on: [' + node._id + '] :' + node.label.replace(/\{.+\}/ig, '') + ' @ ' + new Date().toLocaleTimeString());
          db.update({ _id: node._id }, { $set : node}, {}, function (err, numReplaced) { console.log('   ['+node._id+'] DB-Updates:' + numReplaced);}); /*save lastSMS timestamp to DB*/
        }
        else console.log('   ['+node._id+'] TooColdAlert SMS skipped.');
      };
    }
  },
 
  tempTooHotSMSLimiter : { label:'TooHotAlert : SMS', icon:'comment', descr:'Send SMS when Temp is > than the highValue setting, once per 30 min.',
    serverExecute:function(node) {
      if (node.metrics['F'] && node.metrics['F'].value > node.settings['highTempValue'] && (Date.now() - node.metrics['F'].updated < 2000)) /*check if M metric exists and value is MOTION, received less than 2s ago*/
      {
        var approveSMS = false;
        if (node.metrics['F'].lastSMS) /*check if lastSMS value is not NULL ... */
        {
          if (Date.now() - node.metrics['F'].lastSMS > 1800000) /*check if lastSMS timestamp is more than 1hr ago*/
          {
            approveSMS = true;
          }
        }
        else
        {
          approveSMS = true;
        }
        if (approveSMS)
        {
          node.metrics['F'].lastSMS = Date.now();
          sendSMS('Too hot!', 'Temperature alert on: [' + node._id + '] :' + node.label.replace(/\{.+\}/ig, '') + ' @ ' + new Date().toLocaleTimeString());
          db.update({ _id: node._id }, { $set : node}, {}, function (err, numReplaced) { console.log('   ['+node._id+'] DB-Updates:' + numReplaced);}); /*save lastSMS timestamp to DB*/
        }
        else console.log('   ['+node._id+'] TooHotAlert SMS skipped.');
      };
    }
  },

The following was added to exports.motes;

 
Code: [Select]
 },
    settings: { ip:'',
lowTempValue: '',
    highTempValue: ''}, //blank will inherit ip value from global settings.json
  }
}

and this is the section of the system.json5 file that was edited;

Code: [Select]
radiothermostat: {
      editable: true,
      exposed: true,
      ip: {
        value: '10.0.0.41',
        description: 'If you have a wifi RadioThermostat CT50/CT80 this is the IP address',
      },
  lowTempValue: {
        value: 68,
        type: 'range',
        min:32,
        max:75,
        description: 'Send SMS when Temp is < than the lowValue setting, once per 30 min.'
      },
      highTempValue: {
        value: 75,
        type: 'range',
        min:50,
        max:90,
        description: 'Send SMS when Temp is > than the highValue setting, once per 30 min.'
      },
    },

Any help into what I have done wrong would be greatly appreciated.  Thank you!

Attached are a couple screenshots from phone.
« Last Edit: January 05, 2020, 11:00:04 PM by sparky »

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #1 on: January 04, 2020, 02:13:53 PM »
Try node.settings['lowTempValue'].min
And node.settings['highTempValue'].max

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #2 on: January 04, 2020, 03:03:39 PM »
ssmall,

Thanks for the reply..

I'm getting the same results;

Code: [Select]
tempTooColdSMSLimiter : { label:'TooColdAlert : SMS', icon:'comment', descr:'Send SMS when Temp is < than the lowValue setting, once per 30 min.',
    serverExecute:function(node) {
      if (node.metrics['F'] && node.metrics['F'].value < node.settings['lowTempValue'].min && (Date.now() - node.metrics['F'].updated < 2000)) /*check if M metric exists and value is MOTION, received less than 2s ago*/
      {
        var approveSMS = false;
        if (node.metrics['F'].lastSMS) /*check if lastSMS value is not NULL ... */
        {
          if (Date.now() - node.metrics['F'].lastSMS > 1800000) /*check if lastSMS timestamp is more than 1hr ago */
          {
            approveSMS = true;
          }
        }
        else
        {
          approveSMS = true;
        }
        if (approveSMS)
        {
          node.metrics['F'].lastSMS = Date.now();
          sendSMS('Too cold!', 'Temperature alert on: [' + node._id + '] :' + node.label.replace(/\{.+\}/ig, '') + ' @ ' + new Date().toLocaleTimeString());
          db.update({ _id: node._id }, { $set : node}, {}, function (err, numReplaced) { console.log('   ['+node._id+'] DB-Updates:' + numReplaced);}); /*save lastSMS timestamp to DB*/
        }
        else console.log('   ['+node._id+'] TooColdAlert SMS skipped.');
      };
    }
  },
 
  tempTooHotSMSLimiter : { label:'TooHotAlert : SMS', icon:'comment', descr:'Send SMS when Temp is > than the highValue setting, once per 30 min.',
    serverExecute:function(node) {
      if (node.metrics['F'] && node.metrics['F'].value > node.settings['highTempValue'].max && (Date.now() - node.metrics['F'].updated < 2000)) /*check if M metric exists and value is MOTION, received less than 2s ago*/
      {
        var approveSMS = false;
        if (node.metrics['F'].lastSMS) /*check if lastSMS value is not NULL ... */
        {
          if (Date.now() - node.metrics['F'].lastSMS > 1800000) /*check if lastSMS timestamp is more than 1hr ago*/
          {
            approveSMS = true;
          }
        }
        else
        {
          approveSMS = true;
        }
        if (approveSMS)
        {
          node.metrics['F'].lastSMS = Date.now();
          sendSMS('Too hot!', 'Temperature alert on: [' + node._id + '] :' + node.label.replace(/\{.+\}/ig, '') + ' @ ' + new Date().toLocaleTimeString());
          db.update({ _id: node._id }, { $set : node}, {}, function (err, numReplaced) { console.log('   ['+node._id+'] DB-Updates:' + numReplaced);}); /*save lastSMS timestamp to DB*/
        }
        else console.log('   ['+node._id+'] TooHotAlert SMS skipped.');
      };
    }
  },


Code: [Select]
[01-04-20_14:59:45.420] [WARN]   Event tempTooColdSMSLimiter execution failed: Cannot read property 'lowTempValue' of undefined
[01-04-20_14:59:45.421] [WARN]   Event tempTooHotSMSLimiter execution failed: Cannot read property 'highTempValue' of undefined

ssmall

  • Full Member
  • ***
  • Posts: 156
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #3 on: January 04, 2020, 03:46:59 PM »
Try  node.settings.radiothermostat.lowTempValue.min
Or
node.settings.lowTempValue

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #4 on: January 04, 2020, 04:08:42 PM »
No luck..

Code: [Select]
[01-04-20_16:02:56.333] [WARN]   Event tempTooColdSMSLimiter execution failed: Cannot read property 'radiothermostat' of undefined
[01-04-20_16:02:56.334] [WARN]   Event tempTooHotSMSLimiter execution failed: Cannot read property 'radiothermostat' of undefined

Code: [Select]
[01-04-20_16:06:37.475] [WARN]   Event tempTooColdSMSLimiter execution failed: Cannot read property 'lowTempValue' of undefined
[01-04-20_16:06:37.479] [WARN]   Event tempTooHotSMSLimiter execution failed: Cannot read property 'highTempValue' of undefined

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6571
  • Country: us
    • LowPowerLab
Re: "Cannot read Property error" when adding event
« Reply #5 on: January 07, 2020, 11:06:54 PM »
I suggest following existing patterns when in doubt.
Here's a good example event that uses a setting at the node level - motionEmailSnapshot.
Note the search for existing database node and the callback which yields the result (dbNode), and how that is checked for empty and default handling of the ipcam_snapURL  setting:

Code: [Select]
  motionEmailSnapshot : { label:'Motion : Email+IPCam Snapshot', icon:'camera', descr:'Send email when MOTION is detected, with snapshot from IPCamera',
    serverExecute:function(node) {
      if (node.metrics['M'] && node.metrics['M'].value == 'MOTION' && (Date.now() - new Date(node.metrics['M'].updated).getTime() < 2000))
      {
        db.findOne({ _id : node._id }, function (err, dbNode) {
          var snapshotURL = dbNode.settings && dbNode.settings.ipcam_snapURL ? dbNode.settings.ipcam_snapURL : (exports.motes[dbNode.type].settings.ipcam_snapURL || settings.misc.ipcam_snapURL.value);
          sendEmail('MOTION DETECTED', 'MOTION DETECTED ON NODE: [' + dbNode._id + ':' + dbNode.label.replace(/\{.+\}/ig, '') + '] @ ' + new Date().toLocaleTimeString(), [{path: snapshotURL, filename: 'snapshot.jpg'}]);
        });
      }
    }
  },

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #6 on: January 07, 2020, 11:40:58 PM »
Thanks for your reply Felix..

That Event looks a little over my head.  I'll try but I might have to wait until I return to tackle it 

Edit:

Ok, tried it this morning.. although I'm not getting the error message in the log any longer I am also not getting any indication that the Event is running

Code: [Select]
tempTooColdTSMSLimiter : { label:'TooColdTAlert : SMS', icon:'comment', descr:'Send SMS when Temp is < than the lowValue setting, once per 30 min.',
    serverExecute:function(node) {
      if (node.metrics['F'] && node.metrics['F'].value == 'lowTempValue' && (Date.now() - new Date(node.metrics['F'].updated).getTime() < 2000))
      {
        db.findOne({ _id : node._id }, function (err, dbNode) {
          var tempTooCold = dbNode.settings && dbNode.settings.lowTempValue ? dbNode.settings.lowTempValue : (exports.motes[dbNode.type].settings.lowTempValue || settings.radiothermostat.lowTempValue.value);
          sendEmail('Too cold!', 'Temperature alert on: [' + dbNode._id + ':' + dbNode.label.replace(/\{.+\}/ig, '') + '] @ ' + new Date().toLocaleTimeString() );
        });
      }
    }
  },
« Last Edit: January 08, 2020, 09:55:33 AM by sparky »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6571
  • Country: us
    • LowPowerLab
Re: "Cannot read Property error" when adding event
« Reply #7 on: January 08, 2020, 10:22:49 AM »
It's running if it's enabled in the UI and it turns green.
Add more logging to determine where it's not doing what you want.

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #8 on: January 08, 2020, 11:02:55 AM »
Yes, green and enabled.. 

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6571
  • Country: us
    • LowPowerLab
Re: "Cannot read Property error" when adding event
« Reply #9 on: January 09, 2020, 09:47:50 AM »
Ok so bugs solved. Now its time to debug your code and see where it doesn't do what you need.
Logging is your best friend there.

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #10 on: January 09, 2020, 12:00:21 PM »
Ok Thanks Felix,
This programming is pretty easy for some of you but it isn't for me.  I'll have to read up on debugging with code..

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6571
  • Country: us
    • LowPowerLab
Re: "Cannot read Property error" when adding event
« Reply #11 on: January 09, 2020, 01:29:48 PM »
Just add logging statements as you see in other places in the code... doesn't get much easier than that.

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #12 on: January 09, 2020, 01:40:13 PM »
Ouch, right to the gut!

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6571
  • Country: us
    • LowPowerLab
Re: "Cannot read Property error" when adding event
« Reply #13 on: January 10, 2020, 09:49:39 AM »
Ouch, right to the gut!
Well, if you see it that way ...  ::)
Even in the first post you added in this thread, basically the second was written by you  :-X

console.log('   ['+node._id+'] DB-Updates:' + numReplaced);}); /*save lastSMS timestamp to DB*/
console.log('   ['+node._id+'] TooColdAlert SMS skipped.');

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #14 on: January 10, 2020, 12:01:28 PM »
Well as I've said many times on this forum; this programming is all new to me and that's it's been a real struggle trying to learn it.  So when someone makes a statement "doesn't get much easier than that" I feel it's pretty demeaning.

In regards to the code you just posted, yes I did indeed post that in my first post but it was posted from a user named fix which I also provided a link to.  Now If your saying this is the kind of code I'm to add to try and figure out why my code is not working then I'll have to read up on debugging code as already stated.

I thank you and ssmall again for trying to help me with this!  It is appreciated.








Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6571
  • Country: us
    • LowPowerLab
Re: "Cannot read Property error" when adding event
« Reply #15 on: January 10, 2020, 01:10:49 PM »
No demeaning intended, more like to encourage you to try it since it's very simple. Please folks dont get offended, I am trying to help, but ultimately can't help people getting offended by reading it different.  :'(

Debugging in programming can mean a lot of things. I'm not sure what your plans were on doing more "read up on debugging".
But in this case it's as simple as writing console.log(text and variables) statements.

Sorry if that was not that obvious from my statement, I assumed at least you were familiar with the code you were using (writing yourself? or copy pasting?).
Since I do spend my very limited and valuable time to try to help people for free using this freely offered software, I do have a few (I think pretty low expectations) of posters and the content.

Unfortunately - if copy paste was the case - I would encourage anyone not to assume that will work at all, not in programming, at any level of experience.

sparky

  • Sr. Member
  • ****
  • Posts: 293
  • Country: us
Re: "Cannot read Property error" when adding event
« Reply #16 on: January 10, 2020, 01:46:52 PM »
Thanks for clearing that up and for the encouragement, I need all the help i can get.

Yes, the code was copied and pasted because frix had no issues so I had no reason to think it wouldn't work for me but then again I'm a newbie.

In regards to what I meant by "read up on debugging".  You stated in one of your post "Now its time to debug your code and see where it doesn't do what you need"  it is this statement that I didn't understand and would need to read up on.  Thanks for the explanation.