I am trying to find the sum of each channel to get the total energy for energy_ac. This is the mapping.
var mappings = [
{
floor: 13,
name: 1301,
room_type: 'room',
energy_ac: [
{deviceId: 15062, channels: ['ct1']},
{deviceId: 15063, channels: ['ct1', 'ct2', 'ct3']}
],
energy_light: [
{deviceId: 15062, channels: ['ct4']}
],
energy_socket1: [
{deviceId: 15062, channels: ['ct5']}
],
energy_socket2: [
{deviceId: 15062, channels: ['ct5']}
]
} ];
This is the data:
data = { '15062':
{ _id: 550fea1b46758f1505dd70c7,
deviceId: '15062',
link: 'http://egauge15062.egaug.es/cgi-bin/egauge-show?S&s=0&n=6&C&Z=LST-8',
timestamp: 1427106327,
ct1: 34,
ct2: 0,
ct3: 7,
ct4: 572,
ct5: 527 },
'15063':
{ _id: 550fea1b46758f1505dd70c8,
deviceId: '15063',
link: 'http://egauge15062.egaug.es/cgi-bin/egauge-show?S&s=0&n=6&C&Z=LST-8',
timestamp: 1427106327,
ct1: 34,
ct2: 0,
ct3: 7,
ct4: 572,
ct5: 527 },
'15064':
{ _id: 550fea1b46758f1505dd70c9,
deviceId: '15064',
link: 'http://egauge15062.egaug.es/cgi-bin/egauge-show?S&s=0&n=6&C&Z=LST-8',
timestamp: 1427106327,
ct1: 34,
ct2: 0,
ct3: 7,
ct4: 572,
ct5: 527 },
'15065':
{ _id: 550fea1b46758f1505dd70ca,
deviceId: '15065',
link: 'http://egauge15062.egaug.es/cgi-bin/egauge-show?S&s=0&n=6&C&Z=LST-8',
timestamp: 1427106327,
ct1: 34,
ct2: 0,
ct3: 7,
ct4: 572,
ct5: 527 } }
This is my code:
mappings.forEach(function(room) {
var hash = {};
hash.floor = room.floor;
hash.name = room.name;
hash.room_type = room.room_type;
hash.energy_ac = 0;
room.energy_ac.forEach(function(device) {
device.channels.forEach(function(channel){
hash.energy_ac += room[device][channel];
});
});
room[device][channel] is not working for me. I also tried room[device].channel . I am not sure how to get the value of channel. Any help would be appreciated.