Re: [mh] Returning 0 as a value and not false?
Timothy Spaulding <[email protected]> Wed, 15 Feb 2023 01:28:57 +0000
| Newsgroups | gmane.comp.misc.misterhouse.user |
|---|---|
| Message-ID | <BL3PR01MB7074FC00CA1172D1BE561B31CDA39@BL3PR01MB7074.prod.exchangelabs.com> |
Ok, here is another idea; (my Perl is getting rusty and so I don't know which will work)
if (power_changed $item >= 0) {
...
}
or
if (power_changed $item ge 0) {
...
}
-----Original Message-----
From: H Plato <[email protected]>
Sent: Tuesday, February 14, 2023 4:33 PM
To: Timothy Spaulding <[email protected]>
Cc: misterhouse list <[email protected]>
Subject: Re: [mh] Returning 0 as a value and not false?
Thanks Tim. That part actually works, it’s the usercode:
if (power_changed $item) {
#do something
}
this works great except when the power is a 0. So the reset works, it’s just the power_changed sub, because I want the actual power value in power_changed (similar to state_changed), if the valid changed value is 0, the if statement in usercode will fail.
Does that explain it better?
> On Feb 14, 2023, at 1:34 PM, Timothy Spaulding <[email protected]> wrote:
>
> 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