Re: [mh] Returning 0 as a value and not false?
Timothy Spaulding <[email protected]> Tue, 14 Feb 2023 20:34:25 +0000
| Newsgroups | gmane.comp.misc.misterhouse.user |
|---|---|
| Message-ID | <BL3PR01MB7074E70910216AB832CEB6FCCDA29@BL3PR01MB7074.prod.exchangelabs.com> |
Would this work?
sub reset_power_state {
my ($self) = @_;
if (defined($self->{power_state_changed})) {
if ($self->{power_state_changed_loop}) { #wait for a loop to reset power_state_changed
-----Original Message-----
From: H Plato <[email protected]>
Sent: Tuesday, February 14, 2023 2:51 PM
To: misterhouse list <[email protected]>
Subject: [mh] Returning 0 as a value and not false?
I’ve been testing out the power monitoring on the tasmota module with good success. It’s nice to see an RRD as well to profile a few devices.
I’ve run into a snag though, that I think is easy but the solution is eluding me. To allow for monitoring power changes, I have a power_changed method. each time new power data comes in, I compare the new value to the old one, and if they are different, $self->{power_state_changed} is set to that value. This works great _except_ for 0 values. 0 can be a valid state (and in fact I need to see it to determine if something is turned off), however 0 is being interpreted as false, so the power_changed method is never set.
Does anyone have any suggestions on how to pass along a 0 value so the method will work? I thought maybe returning 0.0 as a 0 might work, but that seems like a kludge.
--
sub reset_power_state {
my ($self) = @_;
if ($self->{power_state_changed}) {
if ($self->{power_state_changed_loop}) { #wait for a loop to reset power_state_changed
undef $self->{power_state_changed};
$self->{power_state_changed_loop} = 0;
} else {
$self->{power_state_changed_loop} = 1;
}
}
}
sub update_power {
my ($self,$power, $current) = @_;
$self->{monitor}->{prevpower} = $self->{monitor}->{power};
$self->{monitor}->{power} = $power;
$self->{monitor}->{maxpower} = $power if ($self->{monitor}->{power} > $self->{monitor}->{maxpower});
$self->{power_state_changed} = $power unless ($self->{monitor}->{prevpower} == $self->{monitor}->{power});
$self->{monitor}->{prevcurrent} = $self->{monitor}->{current};
$self->{monitor}->{current} = $current;
$self->{monitor}->{maxcurrent} = $current if ($self->{monitor}->{current} > $self->{monitor}->{maxcurrent}); }
sub power_changed {
my ($self) = @_;
return $self->{power_state_changed};
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users
________________________________________________________
To unsubscribe from this list, go to: https://lists.sourceforge.net/lists/listinfo/misterhouse-users