Divide 2 values in a function

How can i divide 2 values in a function ?

var Fan = context.get('Fan') || 0; // Value 0 - 100 step 5
var Pomp = context.get('Pomp') || 0; // Value 1 - 10 step 0.1

if (msg.topic == 'Fan')
{
    Fan = {payload: msg.payload};
    context.set('Fan', Fan);
}

if (msg.topic == 'Pomp')
{
    Pomp = {payload: msg.payload};
    context.set('Pomp', Pomp);
}
msg.payload = Fan / Pomp;
return msg

1/ If Pomp = 0 then you get a division by 0.
2/ You are not populating the context variables with numbers but with a { payload : }.

A possible correction:

var Fan = context.get('Fan') || 0; // Value 0 - 100 step 5
var Pomp = context.get('Pomp') || 0.1; // Value 1 - 10 step 0.1

if (msg.topic == 'Fan')
{
    Fan = msg.payload;
    context.set('Fan', Fan);
}

if (msg.topic == 'Pomp')
{
    Pomp = msg.payload;
    context.set('Pomp', Pomp);
}
msg.payload = Fan / Pomp;
return msg

Thanks, that is the solution.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.