From d31a235139a790b0e69616c4b881a265ff4f744e Mon Sep 17 00:00:00 2001
From: Beta-User <>
Date: Thu, 15 Jul 2021 16:18:11 +0000
Subject: [PATCH] mqtt2.template +ebus: changes to ebus, #122048
git-svn-id: https://svn.fhem.de/fhem/trunk@24753 2b470e98-0d58-463d-a4d8-8e2adae1ed80
---
fhem/FHEM/lib/AttrTemplate/mqtt2.template | 15 +-
.../99_attrT_z2m_thermostat_Utils.pm | 3 +-
.../AttrTemplate/99_attrTmqtt2_ebus_Utils.pm | 149 ++++++++++++++++--
3 files changed, 148 insertions(+), 19 deletions(-)
diff --git a/fhem/FHEM/lib/AttrTemplate/mqtt2.template b/fhem/FHEM/lib/AttrTemplate/mqtt2.template
index 3c8fb0b32..a0abff26d 100644
--- a/fhem/FHEM/lib/AttrTemplate/mqtt2.template
+++ b/fhem/FHEM/lib/AttrTemplate/mqtt2.template
@@ -3389,24 +3389,25 @@ name:eBus_daemon_splitter
filter:TYPE=MQTT2_DEVICE
desc:Device containing all status messages from the ebus daemon itself
NOTE: acts also as a bridge device to split up the hardware on the bus into different mqtt2_devices
NOTE:
- for use with MQTT2_CLIENT use a copy of the Device with the same clientId than the IO, delete original's readingList after applying the template!
- this might change the devices CID
order:E_01a
-par:DEVTYPE;Internal TYPE of the device; { InternalVal("DEVICE","TYPE",undef)}
-par:DEV_ID;base topic set ebus;{ AttrVal("DEVICE","readingList","") =~ m,[^:]+:?(ebus[a-zA-Z][^/]*)[/].*:, ? $1 : "ebusd" }
+par:DEVTYPE;Internal TYPE of the device; { InternalVal("DEVICE",'TYPE',undef)}
+par:DEV_ID;base topic set ebus;{ AttrVal("DEVICE",'readingList','') =~ m,[^:]+:?(ebus[a-zA-Z][^/]*)[/].*:, ? $1 : 'ebusd' }
par:ICON;ICON as set, defaults to sani_boiler_temp;{ AttrVal("DEVICE","icon","sani_boiler_temp") }
-{ Svn_GetFile("contrib/AttrTemplate/99_attrTmqtt2_ebus_Utils.pm", "FHEM/99_attrTmqtt2_ebus_Utils.pm", sub(){ CommandReload(undef, "99_attrTmqtt2_ebus_Utils") }) }
+{ Svn_GetFile('contrib/AttrTemplate/99_attrTmqtt2_ebus_Utils.pm', 'FHEM/99_attrTmqtt2_ebus_Utils.pm', sub(){ CommandReload(undef, '99_attrTmqtt2_ebus_Utils') }) }
{ Svn_GetFile("contrib/AttrTemplate/mqtt2.ebus.template", "FHEM/lib/AttrTemplate/mqtt2.ebus.template", sub(){ AttrTemplate_Initialize() }) }
attr DEVICE icon ICON
modify DEVICE DEV_ID
attr DEVICE autocreate 1
attr DEVICE bridgeRegexp (ebus\S[^/]*?)/(bai|\d+|cc|e7f|ehp|f\d\d|hc|hc\d+|he.|hmu|hwc|mc|mc.\d|omu|omu.\d|pms|rcc|rcc.\d|sc|sdr_p|solar|ui|uih|v\d\d|v81.\d|vd\d|vl\d|vr_\d\d|zeo)/.*:.* "$1_$2"\
(ebus\S[^/]*?)/(global|broadcast|general|scan[^/]+)/.*:.* "$1"
-attr DEVICE readingList DEV_ID/scan.\d+/.*:.* { $TOPIC =~ m,scan.(\d+)/,; json2nameValue($EVENT,"scan_${1}_") }\
+attr DEVICE readingList DEV_ID/scan[^/]+/.*:.* { $TOPIC =~ m,scan.([^/]+)/,; FHEM::aTm2u_ebus::j2nv($EVENT,"scan_${1}_") }\
DEV_ID/global/uptime:.* uptime\
DEV_ID/global/running:.* running\
DEV_ID/global/version:.* version\
DEV_ID/global/signal:.* signal\
DEV_ID/global/updatecheck:.* updatecheck\
- DEV_ID/global/scan:.* scan
-attr DEVICE userReadings formatedUptime:uptime.* {my $m = ReadingsVal($name,"uptime",0)/60;; return sprintf "0 000 00:%02d", $m if $m < 60;; my $h = $m / 60;; $m %= 60;; return sprintf "0 000 %02d:%02d", $h, $m if $h < 24;; my $d = $h / 24;; $h %= 24;; return sprintf "0 %03d %02d:%02d", $d, $h, $m if $d <365;; my $y = $d / 365;; $d %= 365;; return sprintf "%d %03d %02d:%02d", $y, $d, $h, $m}
+ DEV_ID/global/scan:.* scan\
+ DEV_ID/broadcast/datetime:.* { FHEM::aTm2u_ebus::j2nv($EVENT) }
+attr DEVICE userReadings formatedUptime:uptime.* {my $m = ReadingsVal($name,'uptime',0)/60; return sprintf '0 000 00:%02d', $m if $m < 60; my $h = $m / 60; $m %= 60; return sprintf '0 000 %02d:%02d', $h, $m if $h < 24; my $d = $h / 24; $h %= 24;; return sprintf '0 %03d %02d:%02d', $d, $h, $m if $d <365; my $y = $d / 365; $d %= 365; return sprintf '%d %03d %02d:%02d', $y, $d, $h, $m}
attr DEVICE stateFormat Status: \
1:running\
Signal: \
@@ -3419,7 +3420,7 @@ set DEVICE getKnown
attr DEVICE comment NOTE: additional templates and code have been downloaded from svn (contrib).
Pls. inform the maintainer, if the bridgeRegexp doesn't fit to all of your devices connected to the bus.
farewell:template has been applied successfully.
NOTE: additional templates and code have been downloaded from svn (contrib).
To configure further parts of your ebus ecosystem, have a look at these templates and the Wiki.
attr DEVICE model eBus_daemon_splitter
-setreading DEVICE attrTemplateVersion 20200824
+setreading DEVICE attrTemplateVersion 20210715
###########################################
diff --git a/fhem/contrib/AttrTemplate/99_attrT_z2m_thermostat_Utils.pm b/fhem/contrib/AttrTemplate/99_attrT_z2m_thermostat_Utils.pm
index 420e69e33..eaad48f2e 100644
--- a/fhem/contrib/AttrTemplate/99_attrT_z2m_thermostat_Utils.pm
+++ b/fhem/contrib/AttrTemplate/99_attrT_z2m_thermostat_Utils.pm
@@ -6,6 +6,7 @@ package FHEM::attrT_z2m_thermostat_Utils; ## no critic 'Package declaration'
use strict;
use warnings;
+use JSON qw(decode_json);
#use Time::HiRes qw( gettimeofday );
#use List::Util qw( min max );
@@ -29,9 +30,9 @@ BEGIN {
ReadingsVal
ReadingsNum
ReadingsAge
- decode_json
json2nameValue
defs
+ Log3
)
);
}
diff --git a/fhem/contrib/AttrTemplate/99_attrTmqtt2_ebus_Utils.pm b/fhem/contrib/AttrTemplate/99_attrTmqtt2_ebus_Utils.pm
index 311f3db4e..65441d7d5 100644
--- a/fhem/contrib/AttrTemplate/99_attrTmqtt2_ebus_Utils.pm
+++ b/fhem/contrib/AttrTemplate/99_attrTmqtt2_ebus_Utils.pm
@@ -1,23 +1,143 @@
##############################################
-# $Id: attrTmqtt2_ebus_Utils.pm 2020-01-18 Beta-User $
+# $Id: attrTmqtt2_ebus_Utils.pm 2021-07-15 Beta-User $
#
-package main;
+package FHEM::aTm2u_ebus; ## no critic 'Package declaration'
use strict;
use warnings;
-sub
-attrTmqtt2_ebus_Utils_Initialize
-{
- my $hash = shift;
- return;
+use JSON qw(decode_json);
+use Scalar::Util qw(looks_like_number);
+
+use GPUtils qw(GP_Import);
+
+#-- Run before package compilation
+BEGIN {
+
+ # Import from main context
+ GP_Import(
+ qw(
+ json2nameValue
+ AttrVal
+ InternalVal
+ CommandGet
+ CommandSet
+ readingsSingleUpdate
+ readingsBulkUpdate
+ readingsBeginUpdate
+ readingsEndUpdate
+ ReadingsVal
+ ReadingsNum
+ ReadingsAge
+ json2nameValue
+ defs
+ Log3
+ )
+ );
}
+sub ::attrTmqtt2_ebus_Utils_Initialize { goto &Initialize }
+sub ::attrTmqtt2_ebus_createBarView { goto &createBarView }
+
+# initialize ##################################################################
+sub Initialize {
+ my $hash = shift;
+ return;
+}
# Enter you functions below _this_ line.
-sub
-attrTmqtt2_ebus_createBarView {
+sub j2nv {
+ my $EVENT = shift // return;
+ my $pre = shift;
+ my $filt = shift;
+ my $not = shift;
+ $EVENT=~ s{[{]"value":\s("[^"]+")[}]}{$1}g;
+ return json2nameValue($EVENT, $pre, $filt, $not);
+}
+
+sub send_weekprofile {
+ my $name = shift;
+ my $wp_name = shift;
+ my $wp_profile = shift // return;
+ my $model = shift // ReadingsVal($name,'week','selected'); #selected,Mo-Fr,Mo-So,Sa-So? holiday to set actual $wday to sunday program?
+ my $topic = shift // AttrVal($name,'devicetopic','') . '/hcTimer.$wkdy/set ';
+
+ my $onLimit = shift // '20';
+
+ my $hash = $defs{$name};
+
+ my $wp_profile_data = CommandGet(undef,"$wp_name profile_data $wp_profile 0");
+ if ($wp_profile_data =~ m{(profile.*not.found|usage..profile_data..name)}xms ) {
+ Log3( $hash, 3, "[$name] weekprofile $wp_name: no profile named \"$wp_profile\" available" );
+ return;
+ }
+
+ my @Dl = ("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
+ my @D = ("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
+
+ my $payload;
+ my @days = (0..6);
+ my $text = decode_json($wp_profile_data);
+
+ ( $model, @days ) = split m{:}xms, $model;
+ (my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst) = localtime;
+
+ @days = ( $model eq 'Mo-Fr' || $model eq 'Mo-So' ) ? (1) : ($model eq 'Sa-So' || $model eq 'holiday' ) ? (0) : (0..6) if !@days;
+
+ for my $i (@days) {
+ $payload = q{};
+ my $pairs = 0;
+ my $onOff = 'off';
+
+ for my $j (0..20) {
+ my $time = '00:00';
+ if (defined $text->{$D[$i]}{time}[$j]) {
+ $time = $text->{$D[$i]}{time}[$j-1] // '00:00';
+ my $val = $text->{$D[$i]}{temp}[$j];
+ if ( $val eq $onOff || (looks_like_number($val) && _compareOnOff( $val, $onOff, $onLimit ) ) ) {
+ $time = '00:00' if !$j;
+ $payload .= qq{$time;;$text->{$D[$i]}{time}[$j];;};
+ $pairs++;
+ $val = $val eq 'on' ? 'off' : 'on';
+ }
+ }
+ while ( $pairs < 3 && !defined $text->{$D[$i]}{time}[$j] ) {
+ #fill up the three pairs with last time
+ $time = $text->{$D[$i]}{time}[$j-1];
+ $pairs++;
+ $payload .= qq{-:-;;-:-;;};
+ }
+ last if $pairs == 3;
+ }
+
+ if ( $model eq 'holiday' ) {
+ $payload .= 'selected';
+ CommandSet($defs{$name},"$name $Dl[$wday] $payload")
+ } else {
+ $payload .= $model;
+ CommandSet($defs{$name},"$name $Dl[$i] $payload");
+ }
+ }
+
+ readingsSingleUpdate( $defs{$name}, 'weekprofile', "$wp_name $wp_profile",1);
+ return;
+}
+
+sub _compareOnOff {
+ my $val = shift // return;
+ my $onOff = shift // return;
+ my $lim = shift;
+
+ if ( $onOff eq 'on' ) {
+ return $val < $lim;
+ } else {
+ return $val >= $lim;
+ }
+ return;
+}
+
+sub createBarView {
my ($val,$maxValue,$color) = @_;
$maxValue = $maxValue//100;
$color = $color//"red";
@@ -38,6 +158,7 @@ attrTmqtt2_ebus_createBarView {
1;
+__END__
=pod
=begin html
@@ -47,14 +168,20 @@ attrTmqtt2_ebus_createBarView {
Functions to support attrTemplates for ebusd
attrTmqtt2_ebus_createBarView($,$$)
aTm2u_ebus::j2nv($,$$$)
$EVENT=~ s{[{]"value":\s("[^"]+")[}]}{$1}g;
.
+ aTm2u_ebus::createBarView($,$$)