diff --git a/fhem/CHANGED b/fhem/CHANGED index 7d8960e0e..22f1865b6 100644 --- a/fhem/CHANGED +++ b/fhem/CHANGED @@ -1,5 +1,6 @@ # Add changes at the top of the list. Keep it in ASCII, and 80-char wide. # Do not insert empty lines here, update check depends on it. + - added: 98_CustomReadings.pm (maintainer: HCS) - change: 98_Text2Speech.pm: fix a problem with microseconds in time() by using mp3-templates or playing mp3 directly - feature: state definition and split attribute added to 66_ECMD, diff --git a/fhem/FHEM/98_CustomReadings.pm b/fhem/FHEM/98_CustomReadings.pm new file mode 100644 index 000000000..e66593a29 --- /dev/null +++ b/fhem/FHEM/98_CustomReadings.pm @@ -0,0 +1,201 @@ + +# $Id$ +# +# TODO: + +package main; + +use strict; +use warnings; +use SetExtensions; + +sub +CustomReadings_Initialize($) +{ + my ($hash) = @_; + + $hash->{DefFn} = "CustomReadings_Define"; + $hash->{UndefFn} = "CustomReadings_Undef"; + $hash->{AttrList} = "readingDefinitions " + . "interval " + . "$readingFnAttributes"; +} + +sub +CustomReadings_Define($$) +{ + my ($hash, $def) = @_; + my $name = $hash->{NAME}; + + CustomReadings_read($hash); + + return undef; +} + +sub CustomReadings_read($) +{ + my ($hash) = @_; + my $name = $hash->{NAME}; + + RemoveInternalTimer($hash); + InternalTimer(gettimeofday()+ AttrVal( $name, "interval", 5), "CustomReadings_read", $hash, 0); + + # Get the readingDefinitions and remove all newlines from the attribute + my $readingDefinitions = AttrVal( $name, "readingDefinitions", ""); + $readingDefinitions =~ s/\n//g; + + my @definitionList = split(',', $readingDefinitions); + my @used = ("state"); + + readingsBeginUpdate($hash); + foreach (@definitionList) { + my @definition = split(':', $_, 2); + push(@used, $definition[0]); + + my $value = eval($definition[1]); + if($value) { + $value =~ s/^\s+|\s+$//g; + } + else { + $value = "ERROR"; + } + + readingsBulkUpdate($hash, $definition[0], $value); + } + + readingsEndUpdate($hash, 1); + + foreach my $r (keys %{$hash->{READINGS}}) { + if (not $r ~~ @used ) { + delete $hash->{READINGS}{$r}; + } + } + +} + + +sub +CustomReadings_Undef($$) +{ + my ($hash, $arg) = @_; + my $name = $hash->{NAME}; + + RemoveInternalTimer($hash); + + return undef; +} + + +1; + +=pod +=begin html + + +
+define myReadings CustomReadings
+attr myReadings room 0-Test
+attr myReadings interval 2
+attr myReadings readingDefinitions hdd_temperature:qx(hddtemp /dev/sda 2>&1),
+ac_powersupply_voltage:qx(cat /sys/class/power_supply/ac/voltage_now 2>&1) / 1000000,
+ac_powersupply_current:qx(cat /sys/class/power_supply/ac/current_now 2>&1) / 1000000,
+perl_version:$],
+timezone:qx(cat /etc/timezone 2>&1),
+kernel:qx(uname -r 2>&1),
+device_name:$hash->{NAME},
+bullshit: $hash->{bullshit},
+fhem_backup_folder_size:qx(du -ch /opt/fhem/backup | grep total | cut -d 't' -f1 2>&1)
+
+
+ Resulting readings:
+
+
+
+ ac_powersupply_current
+ 0.236
+ 2014-08-09 15:40:21
+
+
+ ac_powersupply_voltage
+ 5.028
+ 2014-08-09 15:40:21
+
+
+ bullshit
+ ERROR
+ 2014-08-09 15:40:21
+
+
+ device_name
+ myReadings
+ 2014-08-09 15:40:21
+
+
+ fhem_backup_folder_size
+ 20M
+ 2014-08-09 15:40:21
+
+
+ hdd_temperature
+ /dev/sda: TS128GSSD320: 47°C
+ 2014-08-09 15:40:21
+
+
+ kernel
+ 3.4.103-sun7i+
+ 2014-08-09 15:40:21
+
+
+ perl_version
+ 5.014002
+ 2014-08-09 15:40:21
+
+
+ timezone
+ Europe/Berlin
+ 2014-08-09 15:40:21
+
+
+
+
+ kernel:qx(uname -r 2>&1)