# $Id$ #################################################################################################### # # 39_VALVES.pm # # originally developed by Florian Duesterwald # # heating valves average, with some adjust and ignore options # http://forum.fhem.de/index.php/topic,24658.0.html # # thanks to cwagner for testing and a great documentation of the module: # http://www.fhemwiki.de/wiki/Raumbedarfsabh%C3%A4ngige_Heizungssteuerung # http://www.fhemwiki.de/wiki/VALVES # thanks to stromer-12 for fixing attr probs # # This file is free contribution and not part of fhem. # # Fhem is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 2 of the License, or # (at your option) any later version. # # Fhem is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with fhem. If not, see . # #################################################################################################### package FHEM::Automation::VALVES; ## no critic 'Package declaration' use strict; use warnings; use Time::HiRes qw(gettimeofday); use List::Util qw(sum); use Scalar::Util qw(looks_like_number); use GPUtils qw(GP_Import); BEGIN { # Import from main context GP_Import( qw( defs init_done readingFnAttributes readingsSingleUpdate readingsBeginUpdate readingsEndUpdate readingsBulkUpdate readingsBulkUpdateIfChanged setReadingsVal AttrVal InternalVal ReadingsVal ReadingsTimestamp Log3 addToDevAttrList parseParams InternalTimer RemoveInternalTimer CommandSet CommandDeleteReading CommandDeleteAttr devspec2array IsDisabled ) ); } sub ::VALVES_Initialize { goto &Initialize } sub Initialize { my $hash = shift // return; $hash->{DefFn} = \&Define; $hash->{UndefFn} = \&Undefine; $hash->{SetFn} = \&Set; $hash->{GetFn} = \&Get; $hash->{AttrFn} = \&Attr; my $attrList = 'valvesPollInterval:1,2,3,4,5,6,7,8,9,10,11,15,20,25,30,45,60,90,120,240,480,900 decimals:0,1 valvesDeviceList valvesDeviceReading valvesIgnoreLowest valvesIgnoreHighest valvesIgnoreDeviceList valvesPriorityDeviceList valvesInitialDelay'; $hash->{AttrList} = "disable:0,1 disabledForIntervals $readingFnAttributes $attrList"; return; } sub Define { my $hash = shift // return; my $def = shift // return; my ( $name, $TYPE, $tomuch ) = split m{\s+}xms, $def; return 'Wrong syntax: use define VALVES' if defined $tomuch || !defined $TYPE; Log3( $name, 4, "VALVES $name has been defined" ); readingsBeginUpdate($hash); readingsBulkUpdate( $hash, 'state', 'initialized' ); readingsBulkUpdate( $hash, 'busy', 0 + gettimeofday() ); #waiting for attr check readingsEndUpdate( $hash, $init_done ); #first run after 61 seconds, wait for other devices InternalTimer( gettimeofday() + AttrVal( $name, 'valvesInitialDelay', 61 ), \&VALVES_GetUpdate, $hash, 0 ) if !$init_done; VALVES_GetUpdate($hash) if $init_done && !AttrVal( $name, 'disable', 0 ); return; } sub Undefine { my $hash = shift // return; RemoveInternalTimer($hash); return; } sub Get { my ( $hash, @arr ) = @_; my $name = $hash->{NAME}; my $get = $arr[1]; if ( $get eq 'attrHelp' ) { return _valvesAttribs( 'help', $arr[2] ); } my @stmgets = keys %{ $hash->{READINGS} }; $get = '?' if $get ne '?' && !( grep {m{$get}x} ( @stmgets, 'attrHelp', 'state', 'html' ) ); if ( $get ne '?' ) { return $get . ': ' . ReadingsVal( $name, $get, 'Unknown at line ' . __LINE__ ); } my $usage = "Unknown argument $get, choose one of"; for (@stmgets) { $usage .= " " . $_ . ":noArg"; } $usage .= " attrHelp:"; for ( _valvesAttribs( 'keys', '' ) ) { $usage .= ",$_"; } return $usage; } sub Set { my ( $hash, @arr ) = @_; my $name = shift @arr; return 'no set value specified' if !@arr; if ( $arr[0] eq 'reset' ) { Log3( $name, 4, "VALVES set $name " . join( q{ }, @arr ) ); for ( keys( %{ $hash->{READINGS} } ) ) { CommandDeleteReading( undef, "$name $_" ); } return; } my $setList = 'reset:noArg'; return "Unknown argument $arr[0], choose one of $setList"; } sub Attr { my ( $cmd, $name, $attrName, $attrVal ) = @_; my $hash = $defs{$name}; #special attr valvesDeviceList #valvesDeviceList: addToAttrList if ( $attrName eq 'valvesDeviceList' ) { if ( length($attrVal) > 2 ) { Log3( $name, 4, "VALVES $name attribute-value [$attrName] = $attrVal changed" ); for ( devspec2array($attrVal) ) { # split m{,}x,$attrVal ) { #addToDevAttrList("$name","valves".$_."Gewichtung",'VALVES'); addToDevAttrList( "$name", "valves" . $_ . "Weighting", 'VALVES' ); } VALVES_GetUpdate($hash) if $init_done && !IsDisabled($name); } else { Log3( $name, 3, "VALVES $name attribute-value [$attrName] = $attrVal wrong, string min length 2" ) if $attrVal; } return; } #validate special attr valvesPollInterval if ( $attrName eq 'valvesPollInterval' ) { if ( $attrVal >= 1 && $attrVal <= 900 ) { Log3( $name, 4, "VALVES $name attribute-value [$attrName] = $attrVal changed" ); VALVES_GetUpdate($hash) if $init_done && !AttrVal( $name, 'disable', 0 ); } else { Log3( $name, 3, "VALVES $name attribute-value [$attrName] = $attrVal wrong, use seconds >1 as float (max 900)" ); return "$attrVal is not a number or within allowed range!" if $init_done; } return; } if ( $attrName eq 'valvesDeviceReading' ) { delete $hash->{helper}->{valvesDeviceReading}; if ( length($attrVal) > 2 ) { my ( $unnamedParams, $namedParams ) = parseParams($attrVal); $hash->{helper}->{valvesDeviceReading} = $namedParams; $hash->{helper}->{valvesDeviceReading}->{valvesDeviceReading} = $unnamedParams->[0] if defined $unnamedParams->[0]; Log3( $name, 4, "VALVES $name attribute-value [$attrName] = $attrVal changed" ); VALVES_GetUpdate($hash) if $init_done; } else { Log3( $name, 3, "VALVES $name attribute-value [$attrName] = $attrVal wrong, string min length 2" ) if $attrVal; } return; } if ( $attrName eq 'disable' ) { RemoveInternalTimer($hash) if $cmd ne 'del'; InternalTimer( gettimeofday(), \&VALVES_GetUpdate, $hash, 0 ) if $cmd eq 'del' || !$attrVal && $init_done; return; } if ( $attrName eq 'valvesInitialDelay' ) { RemoveInternalTimer($hash) if !$init_done; return if AttrVal( $name, 'disable', 0 ); if ( !looks_like_number($attrVal) ) { return "$attrVal is not a number!" if $init_done; $attrVal = 61; } InternalTimer( gettimeofday() + $attrVal, \&VALVES_GetUpdate, $hash, 0 ) if !$init_done; } if ( $attrName eq 'decimals' && $cmd eq 'set' ) { if ( !looks_like_number($attrVal) ) { return "$attrVal is not a number!" if $init_done; } return; } #other attribs if ( $attrName =~ m{\Avalves\d+}x ) { if ( !defined $attrVal ) { CommandDeleteAttr( undef, "$name attrName" ); Log3( $name, 4, "VALVES $name: attribute [$attrName] deleted" ); } } else { Log3( $name, 4, "VALVES $name: attribute-value [$attrName] = $attrVal changed" ) if $attrVal; } return; } sub VALVES_GetUpdate { my $hash = shift // return; my $name = $hash->{NAME}; my $valvesPollInterval = AttrVal( $name, 'valvesPollInterval', 10 ); if ( $valvesPollInterval ne 'off' ) { RemoveInternalTimer($hash); $valvesPollInterval = 10 if !looks_like_number($valvesPollInterval); InternalTimer( gettimeofday() + $valvesPollInterval, \&VALVES_GetUpdate, $hash, 0 ) if !AttrVal( $name, 'disable', 0 ); } return if IsDisabled($name); if ( AttrVal( $name, 'valvesDeviceList', 'none' ) eq 'none' ) { readingsSingleUpdate( $hash, 'state', 'missing attr valvesDeviceList', 1 ); RemoveInternalTimer($hash); return; } #check all attr at first loop if ( ReadingsVal( $name, 'busy', 'done' ) ne 'done' ) { #"waiting for attr check" if ( ( gettimeofday() - AttrVal( $name, 'valvesInitialDelay', 61 ) ) > ReadingsVal( $name, 'busy', 0 ) ) { for ( devspec2array( AttrVal( $name, 'valvesDeviceList', '' ) ) ) { # split m{,}x, AttrVal($name,'valvesDeviceList','') ) { #addToDevAttrList("$name",'valves'.$_.'Gewichtung','VALVES'); addToDevAttrList( "$name", 'valves' . $_ . 'Weighting', 'VALVES' ); } CommandDeleteReading( undef, "$name busy" ); } } my ( %valveDetail, %valveShort, @raw_average, $pos, $prio, @prios ); my $valvesIgnoreDeviceList = AttrVal( $name, 'valvesIgnoreDeviceList', '0' ); for my $dev ( devspec2array( AttrVal( $name, 'valvesDeviceList', '' ) ) ) { #check ignorelist next if $valvesIgnoreDeviceList =~ m/$dev/x; #get val my $posRead = $hash->{helper}->{valvesDeviceReading}->{ InternalVal( $dev, 'TYPE', 'none' ) } // $hash->{helper}->{valvesDeviceReading}->{valvesDeviceReading} // 'valveposition'; $pos = ReadingsVal( $dev, $posRead, 'err' ); if ( !defined $pos || $pos eq 'err' || $pos eq 'lime-protection' ) { Log3( $name, 4, "VALVES $name " . $_ . " [$pos] DeviceReading not present" ); next; } #$pos =~ s/%//x; $pos = $pos =~ m{(-?\d+(\.\d+)?)}x ? $1 : -1; push @raw_average, $pos; #calc prio $prio = AttrVal( $name, 'valves' . $dev . 'Weighting', AttrVal( $name, 'valves' . $dev . 'Gewichtung', 1 ) ); #fill hash $valveDetail{$dev} = [ ( $pos, ReadingsTimestamp( $dev, $posRead, 0 ) ) ]; $valveShort{$dev} = $pos * $prio; } #ignore highest/lowest N values my @sorted = sort { $valveShort{$a} <=> $valveShort{$b} } keys %valveShort; if ( !@sorted ) { readingsSingleUpdate( $hash, 'state', 'attr valvesDeviceList is empty', 1 ); return; } my $valvesIgnoreLowest = AttrVal( $name, 'valvesIgnoreLowest', 0 ); while ( $valvesIgnoreLowest > 0 ) { shift @sorted; $valvesIgnoreLowest--; } my $valvesIgnoreHighest = AttrVal( $name, 'valvesIgnoreHighest', 0 ); while ( $valvesIgnoreHighest > 0 ) { pop @sorted; $valvesIgnoreHighest--; } return if !@sorted; #fill readings, bypass usual way to conserve valveposition timestamps for (@sorted) { setReadingsVal( $hash, 'valve_' . $_, $valveShort{$_}, $valveDetail{$_}[1] ); push @prios, AttrVal( $name, 'valves' . $_ . 'Weighting', AttrVal( $name, 'valves' . $_ . 'Gewichtung', 1 ) ); } #set min and max from sorted readingsBeginUpdate($hash); readingsBulkUpdateIfChanged( $hash, 'valve_min', $valveShort{ $sorted[0] }, 1 ); readingsBulkUpdateIfChanged( $hash, 'valve_max', $valveShort{ $sorted[-1] }, 1 ); my $valvesPriorityDeviceList = AttrVal( $name, 'valvesPriorityDeviceList', '0' ); for my $dev ( keys %valveDetail ) { if ( !exists $valveShort{$dev} ) { $valveShort{$dev} = 'ignored'; } #create double hash entry for prio dev if ( $valvesPriorityDeviceList =~ m/$dev/x ) { $valveShort{ $dev . 'P' } = $valveShort{$dev}; push @sorted, $dev . 'P'; push @prios, AttrVal( $name, 'valves' . $dev . 'Weighting', AttrVal( $name, 'valves' . $dev . 'Gewichtung', 1 ) ); } readingsBulkUpdate( $hash, 'valveDetail_' . $dev, 'pos:' . $valveDetail{$dev}[0] . ' calc:' . $valveShort{$dev} . ( $valvesPriorityDeviceList =~ m/$dev/x ? '-priority' : '' ) . ' time:' . $valveDetail{$dev}[1], 0 ); } my $state; for (@sorted) { $state += $valveShort{$_}; } my $corr = sum(@prios) / @prios; my $decimals = AttrVal($name,'decimals',0); $decimals = 0 if !looks_like_number($decimals); $state = sprintf "%.${decimals}f", $state / @sorted / $corr; if ( ReadingsVal( $name, 'state', 'err' ) ne $state ) { readingsBulkUpdate( $hash, 'valve_average', $state, 1 ); readingsBulkUpdate( $hash, 'state', $state, 1 ); } $state = 0; for (@raw_average) { $state += $_; } $state = sprintf "%.${decimals}f", $state / @raw_average; readingsBulkUpdateIfChanged( $hash, 'raw_average', $state, 1 ); readingsEndUpdate( $hash, 1 ); return; } sub _valvesAttribs { #usage: _valvesAttribs("type","stmVarName") # "keys" || "default" || "type" || "help" , my ( $type, $reqKey ) = @_; my %attribs = ( "valvesInitialDelay" => [ ( "61", "int", "Waiting time after FHEM start (or Define) before first calculation will be started", "Zeitintervall nach FHEM-Start oder Dev.-Define bevor die Berechnung gestartet wird" ) ], "valvesPollInterval" => [ ( "10", "int", "Zeitintervall nach dem FHEM die Daten versucht zu aktualisieren", "Zeitintervall nach dem FHEM die Daten versucht zu aktualisieren" ) ], "valvesDeviceList" => [ ( "none", "string", "Liste aller Heizungsthermostate mit Komma getrennt ohne Leerzeichen", "Liste aller Heizungsthermostate mit Komma getrennt ohne Leerzeichen" ) ], "valvesDeviceReading" => [ ( "valveposition", "string", "Reading das die Ventilposition zeigt, default: valveposition", "Reading das die Ventilposition zeigt, default: valveposition" ) ], "valvesIgnoreLowest" => [ ( "0", "int", "ignoriere die niedrigsten N Thermostate", "ignoriere die niedrigsten N Thermostate" ) ], "valvesIgnoreHighest" => [ ( "0", "int", "ignoriere die höchsten N Thermostate", "ignoriere die höchsten N Thermostate" ) ], "valvesIgnoreDeviceList" => [ ( "0", "string", "Ignoriere diese Devicenamen", "Ignoriere diese Devicenamen" ) ], "valvesPriorityDeviceList" => [ ( "0", "string", "Thermostate in dieser Liste werden doppelt gezählt", "Thermostate in dieser Liste werden doppelt gezählt" ) ], "valvesGewichtung" => [ ( "1", "float", 'Individual weighting factor for each thermostate. May e.g. be used to compensate hydraulic problems in the heating system.', "Für jedes Thermostat kann ein individueller Gewichtungsfaktor multipliziert werden. So kann zB ein schlechter hydraulischer Abgleich berücksichtigt werden" ) ], "valvesWeighting" => [ ( '1', 'float', 'Individual weighting factor for each thermostate. May e.g. be used to compensate hydraulic problems in the heating system.', "Für jedes Thermostat kann ein individueller Gewichtungsfaktor multipliziert werden. So kann zB ein schlechter hydraulischer Abgleich berücksichtigt werden" ) ], disable => [ ( "0", "int", 'Stop calculations and freeze values', "Berechnung anhalten und einfrieren" ) ], ); return keys %attribs if $type eq 'keys'; return $attribs{$reqKey}[0] if $type eq 'default'; return $attribs{$reqKey}[1] if $type eq 'type'; if ( $type eq 'description' ) { return $attribs{$reqKey}[2] if AttrVal( 'global', 'language', 'EN' ) ne 'DE'; return $attribs{$reqKey}[3]; } if ( $type eq 'help' ) { return "attrHelp for " . $reqKey . ":\n default:" . $attribs{$reqKey}[0] . " type:" . $attribs{$reqKey}[1] . " \ndescription:" . $attribs{$reqKey}[2]; } return '_ attribs?'; } 1; __END__ =pod =encoding utf8 =item helper =item summary generate a virtual valve position based on multiple thermostat devices =item summary_DE Generiert eine virtuelle Ventilöffnung aus eine Mehrzahl von Heizungsventilen =begin html

VALVES

    German docu is available in FHEM Wiki.

    Define

      define <name> VALVES

      Defines a virtual device for VALVES calculations based on multiple thermostat devices
      How VALVE calculates and handles readings:
    • After FHEM startup, VALVES will wait valvesInitialDelay and then will do a calculation every valvesPollInterval seconds using the follwoing scheme:
      • Get a list of valve value and timestamp for each device (not ignored by name)
      • Delete lowest and highest valves/devices from this list
      • recalculate valve positions according to weighting settings
      • Double priority devices in the list
      • Derive readings (main and debug)
    • Main readings (triggering when changed) are valve_min, valve_max, valve_average, raw_average and state.
    • Debug readings valve<Devicename> shows the calculated virtual valve position, valveDetail_<Devicename> the real and calculated position and the timestamp of the original reading.

    Set

    • reset
    • deletes already calculated readings and starts from the scratch

    Get

    • <reading>
    • get <name> <reading>
      Any of the actual reading values.

    • attrHelp <attribute>
    • get <name> attrHelp <attribute>
      Get help text to named attribute.

    Attributes

    • readingFnAttributes
    • valvesInitialDelay <delay>
      Waiting time after FHEM start (or define) before first calculation will be started. Defaults to 61.
    • valvesPollInterval <interval>
      Polling interval (in seconds, between 1 to 900) between each attempt to update values. Defaults for compability reasons to 10.
    • valvesDeviceList <devspec>
      devspec is as usual, e.g. use a comma-separated list (no spaces allowed!) of all thermostate devices to make part of calculations
    • valvesDeviceReading [<positionreading>] ;
      Reading to base calculations upon, default: valveposition. You may set a key value list as follows as well with device-TYPE and reading name pairs like e.g.:
      attr <device> valvesDeviceReading CUL_HM=ValvePosition ZWave=reportedState
    • valvesIgnoreLowest <number>
      ignore the <number> of the thermostate devices with (actual) lowest valve values.
    • valvesIgnoreHighest <number>
      ignore the <number> of the thermostate devices with (actual) highest valve values.
    • valvesIgnoreDeviceList <deviceA,deviceB,[....]>
      ignore the listed thermostate devices (comma separated).
    • valvesPriorityDeviceList <regex>
      Thermostates matching the regex will be doubled in the calculation process
    • valves<Devicename>Weighting <float value>
      Individual weighting factor (float value) for each thermostate. May e.g. be used to compensate hydraulic problems in the heating system
    • decimals <number of decimals>
      Number of decimals for state, valve_raw and valve_average readings. Default is "0"
=end html