Re: [mh] Returning 0 as a value and not false?
H Plato <[email protected]> Tue, 14 Feb 2023 21:22:06 -0700
| Newsgroups | gmane.comp.misc.misterhouse.user |
|---|---|
| Message-ID | <[email protected]> |
I thought about that, but I was hoping that power_changed would work similar to state_changed. I’d also have to put that defined check in reset_power, and I thought there was something about defined that was either being depreciated or was changing in a newer version of perl.
From what I’ve read, I think I’m going to check if power is 0 and then return 0E0 . This apparently is a ‘0 but true’ perl workaround. It should be seen as true, and should be treated as a 0 for numeric operations. The only downside to this is that it will display funny in print_log in this example:
if (my $power = power_changed $item) {
print_log("Power has now changed to $power");
}
However the workaround would be
if (power_changed $item) {
my $power = power $item;
print_log("Power has now changed to $power");
}
I’ll try this and see how it works.
> On Feb 14, 2023, at 6:28 PM, Timothy Spaulding <[email protected]> wrote:
>
> 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