[gelöst] koppeln von Readings

Begonnen von JensS, 21 Februar 2024, 18:40:26

Vorheriges Thema - Nächstes Thema

JensS

Hallo zusammen,

da ich die Frage schwer formulieren, bzw. einem Thema zuordnen kann, ist sie hier in Anfängerfragen gelandet...
Ein openHASP-Schalter, welcher die Zustandsänderung per MQTT sendet, soll eine Lampe schalten. Ebenso soll bei der Zustandsänderung der Lampe, eine MQTT-Meldung an das openHASP-Device gesendet werden. Dazu habe ich ein DOIF eingerichtet.
([MQTT2_plate_72100c:p1b2_val] eq 1 )(set Fernsehschrank on)DOELSEIF([MQTT2_plate_72100c:p1b2_val] eq 0 )(set Fernsehschrank off)DOELSEIF([Fernsehschrank] eq "on")(set MQTT2_plate_72100c command p1b2.val 1)DOELSE(set MQTT2_plate_72100c command p1b2.val 0)
Das Ganze hat einen faden Beigeschmack, da bei beim Schalten auf dem openHASP-Interface, der angezeigte Schalter zweimal seinen Status wechselt.
Bitte helft mir auf die Sprünge, wie das verbessert werden kann. Vielen Dank schonmal für's Lesen.  :)

Gruß Jens
Debian auf APU2C4, HM-CFG-USB2, SIGNALduino, HM-ES-PMSw1-Pl, TFA 30.3121, TFA 30.3125, ITS-150, PIR-5000, configurable Firmata USB & LAN, 1-wire: DS-18B20, DS-18S20, DS-2408, DS-2413, diverse I2C-Komponenten, zigbee2mqtt, ESPEasy etc.

betateilchen

Zitat von: JensS am 21 Februar 2024, 18:40:26Ein openHASP-Schalter, welcher die Zustandsänderung per MQTT sendet, soll eine Lampe schalten. Ebenso soll bei der Zustandsänderung der Lampe, eine MQTT-Meldung an das openHASP-Device gesendet werden.

Warum um alles in der Welt macht man sich dafür die Mühe mit einem DOIF, anstatt das Ganze über eine MQTT_GENERIC_BRIDGE zu lösen?
Die Bridge wurde doch genau dafür geschaffen, jedes beliebige FHEM device "mqtt-fähig" zu machen?
-----------------------
Formuliere die Aufgabe möglichst einfach und
setze die Lösung richtig um - dann wird es auch funktionieren.
-----------------------
Lesen gefährdet die Unwissenheit!

JensS


Bitte hilf mir Unwissenden mit einem Beispiel.
Debian auf APU2C4, HM-CFG-USB2, SIGNALduino, HM-ES-PMSw1-Pl, TFA 30.3121, TFA 30.3125, ITS-150, PIR-5000, configurable Firmata USB & LAN, 1-wire: DS-18B20, DS-18S20, DS-2408, DS-2413, diverse I2C-Komponenten, zigbee2mqtt, ESPEasy etc.

betateilchen

Ok, hier kommt ein Beispiel. Es handelt sich um einen HomematicIP Zwischenstecker HMIP-PS, den ich per mqtt schalten kann und der beim Schalten auch mqtt Nachrichten verschickt.

Hier sind nur die beiden für das mqtt wesentlichen Attribute aufgeführt:

defmod sz_Bett_links HMCCUDEV 0002170993CCCE  sd=2.STATE cd=3.STATE

attr sz_Bett_links mqtt2_Local_Publish state:topic={"sz/Bett_links/$name"} state:expression={"{\"sz/Bett_links/$name\":\"$value\"}"} state:retain=1
attr sz_Bett_links mqtt2_Local_Subscribe state:stopic={"sz/Bett_links/set"}

Dazu gibt es eine MQTT_GENERIC_BRIDGE mit dem Präfix "mqtt2_Local_", der sich deshalb auch in den beiden Attributen für Subscribe und Publish wiederfindet.

https://commandref.fhem.de/#MQTT_GENERIC_BRIDGE
-----------------------
Formuliere die Aufgabe möglichst einfach und
setze die Lösung richtig um - dann wird es auch funktionieren.
-----------------------
Lesen gefährdet die Unwissenheit!

JensS

Super und vielen Dank! Wenn ich's hinbekommen habe, melde ich mich zurück.
Debian auf APU2C4, HM-CFG-USB2, SIGNALduino, HM-ES-PMSw1-Pl, TFA 30.3121, TFA 30.3125, ITS-150, PIR-5000, configurable Firmata USB & LAN, 1-wire: DS-18B20, DS-18S20, DS-2408, DS-2413, diverse I2C-Komponenten, zigbee2mqtt, ESPEasy etc.

JensS

#5
Zufällig sind beide devices MQTT2_Devices und so konnte ich (dank deines Tipps) die Verbindung mittels setList und readingsList durchführen und habe erstmal auf MQTT_GENERIC_BRIDGE verzichtet.
MQTT_GENERIC_BRIDGE ist (für mich) ein neues und spannendes Modul.
readingList hasp/plate/state/p1b2:.* { my $onoff= hasp2mqtt("val",$EVENT) == 1 ? 'on' : 'off';;fhem("set Fernsehschrank $onoff")}
setList on:noArg {fhem "set MQTT2Server publish $DEVICETOPIC/3/set {\"state\":\"ON\"}";;fhem "set MQTT2Server publish hasp/plate/command/p1b2.val 1"}\
off:noArg {fhem "set MQTT2Server publish $DEVICETOPIC/3/set {\"state\":\"OFF\"}";;fhem "set MQTT2Server publish hasp/plate/command/p1b2.val 0"}

99_myUtils.pm:
sub hasp2mqtt($$)
{
my ($haspvalue,@jsonhasp) = @_;
my $hashref = decode_json($jsonhasp[0]);
        for my $key (sort keys %{ $hashref} ){
                my $val = $hashref->{$key};
if($key eq $haspvalue){return $val};
        }
}
Danke nochmals!

Gruß Jens
Debian auf APU2C4, HM-CFG-USB2, SIGNALduino, HM-ES-PMSw1-Pl, TFA 30.3121, TFA 30.3125, ITS-150, PIR-5000, configurable Firmata USB & LAN, 1-wire: DS-18B20, DS-18S20, DS-2408, DS-2413, diverse I2C-Komponenten, zigbee2mqtt, ESPEasy etc.

appi

Hallo Jens
ich habe ein Openhasp 7" Display in Betrieb genommen, läuft eigentlich echt gut. Daten sende ich mit mqttPublish auf das Display. Allerdings wird die ganze Geschichte etwas unübersichtlich, in diversen Devices werden Readings per mqttPublish auf das Display gesendet..... Bist du in diesm Thema weitergekommen? Wie ist deine Implementation?
Gruss Remo

JensS

#7
@Remo

Bisher habe ich nur ein Device zum testen und zwischenzeitlich hatte ich einiges angepasst.
Geschaltet wird ein interne Relais sowie MQTT-Device "Fernsehschrank".

Gruß Jens
define MQTT2_plate_72100c MQTT2_DEVICE plate_72100c
attr MQTT2_plate_72100c IODev MQTT2Server
attr MQTT2_plate_72100c autocreate 0
attr MQTT2_plate_72100c mqtt2_Local_Publish *:topic={"$base/$name"} *:qos=2 *:retain=0
attr MQTT2_plate_72100c readingList plate_72100c:hasp/plate/LWT:.* LWT\
plate_72100c:hasp/discovery/3030f972100c:.* { json2nameValue($EVENT) }\
plate_72100c:hasp/plate/state/output40:.* {hasp2fhem("output40",$EVENT,$NAME)}\
plate_72100c:hasp/plate/state/page:.* page\
plate_72100c:hasp/plate/state/idle:.* idle\
plate_72100c:hasp/plate/state/antiburn:.* {hasp2fhem("antiburn",$EVENT,$NAME )}\
plate_72100c:hasp/plate/state/statusupdate:.* { json2nameValue($EVENT,$NAME ) }\
plate_72100c:hasp/plate/state/sensors:.* {hasp2fhem("sensors",$EVENT,$NAME )}\
plate_72100c:hasp/plate/state/p0b239:.* p0b239\
plate_72100c:hasp/plate/state/p1b1:.* {hasp2fhem("p1b1",$EVENT,$NAME )}\
plate_72100c:hasp/plate/state/backlight:.* {hasp2fhem("backlight",$EVENT,$NAME )}\
plate_72100c:hasp/plate/state/p1b0:.* p1b0\
plate_72100c:hasp/plate/state/p2b0:.* p2b0\
plate_72100c:hasp/plate/state/p12b0:.* p12b0\
plate_72100c:hasp/plate/state/p8b0:.* {hasp2fhem("p8b0",$EVENT,$NAME )}\
plate_72100c:hasp/plate/state/p3b0:.* {hasp2fhem("p3b0",$EVENT,$NAME )}\
plate_72100c:hasp/plate/state/p1b2:.* {hasp2fhem("p1b2",$EVENT,$NAME )}
attr MQTT2_plate_72100c setList on:noArg hasp/plate/command output40 {"state":"on"}\
off:noArg hasp/plate/command output40 {"state":"off"}\
command hasp/plate/command/$EVTPART1 $EVTPART2
attr MQTT2_plate_72100c stateFormat output40_state
sub hasp2fhem($$$)
{
my ($haspreading,@jsonhasp,$devname) = @_;
my $hashref = decode_json($jsonhasp
);
        for my $key (sort keys %{ $hashref} ){
                my $val = $hashref->{$key};
               fhem("setreading $devname $haspreading"."_"."$key $val");
        }
}

define Fernsehschrank MQTT2_DEVICE zigbee_xxx
attr Fernsehschrank userattr mqtt2_Local_Alias:textField-long mqtt2_Local_Defaults:textField-long mqtt2_Local_Disable:both,incoming,outgoing mqtt2_Local_Forward:all,none mqtt2_Local_Publish:textField-long mqtt2_Local_Subscribe:textField-long
attr Fernsehschrank IODev MQTT2Server
attr Fernsehschrank eventMap { dev=>{ON=>'on',OFF=>'off'} }
attr Fernsehschrank readingList $DEVICETOPIC:.* { json2nameValue($EVENT,'',$JSONMAP) }\
zigbee2mqtt/0xec1bbdfffed0efa5/availability:.* availability\
hasp/plate/state/p1b2:.* { my $onoff= hasp2mqtt("val",$EVENT) == 1 ? 'on' : 'off';;fhem("set Fernsehschrank $onoff")}
attr Fernsehschrank rhasspyMapping SetOnOff:cmdOn=on,cmdOff=off,response={ResponseOnOff($DEVICE)}\
GetOnOff:currentVal=state,valueOff=off
attr Fernsehschrank setList on:noArg {fhem "set MQTT2Server publish $DEVICETOPIC/3/set {\"state\":\"ON\"}";;fhem "set MQTT2Server publish hasp/plate/command/p1b2.val 1"}\
off:noArg {fhem "set MQTT2Server publish $DEVICETOPIC/3/set {\"state\":\"OFF\"}";;fhem "set MQTT2Server publish hasp/plate/command/p1b2.val 0"}\
toggle:noArg $DEVICETOPIC/3/set {"state":"TOGGLE"}
attr Fernsehschrank setStateList on off toggle

{
    "wifi": {
        "ssid": "WLAN",
        "pass": "********"
    },
    "wg": {
        "vpnip": "",
        "privkey": "********",
        "host": "",
        "port": 51820,
        "pubkey": ""
    },
    "mqtt": {
        "host": "192.168.x.x",
        "user": "",
        "pass": "********",
        "topic": {
            "node": "hasp/%hostname%/%topic%",
            "group": "hasp/plates/%topic%",
            "broadcast": "hasp/broadcast/%topic%",
            "hass": "homeassistant/status"
        },
        "port": xxxx,
        "name": "plate"
    },
    "telnet": {
        "enable": 1,
        "port": 23
    },
    "mdns": {
        "enable": 1
    },
    "http": {
        "enable": true,
        "port": 80,
        "user": "",
        "pass": "********"
    },
    "gpio": {
        "config": [
            721192,
            0,
            0,
            0,
            0,
            0,
            0,
            0
        ]
    },
    "debug": {
        "ansi": 0,
        "baud": 115200,
        "tele": 300,
        "host": "",
        "port": 514,
        "proto": 0,
        "log": 0
    },
    "gui": {
        "idle1": 60,
        "idle2": 120,
        "bckl": 38,
        "bcklinv": 0,
        "rotate": 0,
        "cursor": 0,
        "invert": 0,
        "calibration": [
            0,
            65535,
            0,
            65535,
            0,
            0,
            0,
            0
        ]
    },
    "hasp": {
        "startpage": 1,
        "startdim": 255,
        "theme": 3,
        "color1": "#cddae6",
        "color2": "#181c18",
        "font": "",
        "pages": "/pages.jsonl"
    }
}
{"page":0,"comment":"Hintergrundlayout"}
{"id":1,"obj":"btn","action":{"down": "page prev"},"x":0,"y":430,"w":160,"h":50,"bg_color":"#2C3E50","text":"\uE141","text_color":"#FFFFFF","radius":0,"border_side":0,"text_font":48}
{"id":2,"obj":"btn","action":{"down": "page back"},"x":160,"y":430,"w":160,"h":50,"bg_color":"#2C3E50","text":"\uE2DC","text_color":"#FFFFFF","radius":0,"border_side":0,"text_font":48}
{"id":3,"obj":"btn","action":{"down": "page next"},"x":320,"y":430,"w":160,"h":50,"bg_color":"#2C3E50","text":"\uE142","text_color":"#FFFFFF","radius":0,"border_side":0,"text_font":48}
{"id":4,"obj":"btn","x":0,"y":0,"w":160,"h":50,"text":"#808080 1%","text_font":48,"action":{"up":"backlight {'state':'on','brightness':2}"}}
{"id":5,"obj":"btn","x":160,"y":0,"w":160,"h":50,"text":"#808080 On/Off","text_font":48,"action":{"up":"backlight {'state':'off'}"}}
{"id":6,"obj":"btn","x":320,"y":0,"w":160,"h":50,"text":"#808080 100%","text_font":48,"action":{"up":"backlight {'state':'on','brightness':255}"}}

{"page":1,"id":0,"prev":2,"comment":"diverse Schalter"}
{"id":1,"obj":"switch","x":300,"y":150,"w":180,"h":75,"radius":40,"radius20":40,"groupid":1}
{"id":2,"obj":"switch","x":300,"y":250,"w":180,"h":75,"radius":40,"radius20":40}
{"id":3,"obj":"label","x":10,"y":160,"w":249,"h":75,"text":"Relais","text_color":"#808080","text_font":40}
{"id":4,"obj":"label","x":10,"y":260,"w":249,"h":75,"text":"Fernsehschrank","text_color":"#808080","text_font":40}

{"page":2,"id":0,"next":1,"comment":"Uhr"}
{"id":1,"obj":"label","x":0,"y":100,"h":180,"w":480,"text":"00:00","align":"center","text_color":"#808080","text_font":120, "template": "%H:%M:%S","action":{"up":"backlight {'state':'off'}"}}
{"id":2,"obj":"label","x":0,"y":320,"h":180,"w":480,"text":"00:00","align":"center","text_color":"#808080","text_font":80, "template": "%d.%m.%Y","action":{"up":"backlight {'state':'off'}"}}
Debian auf APU2C4, HM-CFG-USB2, SIGNALduino, HM-ES-PMSw1-Pl, TFA 30.3121, TFA 30.3125, ITS-150, PIR-5000, configurable Firmata USB & LAN, 1-wire: DS-18B20, DS-18S20, DS-2408, DS-2413, diverse I2C-Komponenten, zigbee2mqtt, ESPEasy etc.

appi

hey, danke, ich hänge mich mal rein.
Ich verfolge noch den Weg mit mqttpublish in den Devices von welchen ich Readings aud das Display bringen will.

z,B.:

attr SMAInverter_8KW mqttPublish
BAT_TEMP :topic=hasp/plate_2/command/p1b40.text\\
BAT_UNLOADTODAY:topic=hasp/plate_2/command/p1b39.text\\
BAT_LOADTODAY:topic=hasp/plate_2/command/p1b38.text\\
ChargeStatus:topic=hasp/plate_2/command/p1b36.text\\
PV_alle_aktuell:topic=hasp/plate_2/command/p1b44.text

appi

Hallo
wollte mal meinen Zwischenstand weitergeben. Ich kann die gewünschten Readings aus Fhem mit mqttpublish im Origin Device an mein Display (mit Openhasp) via Broker senden.

PV_alle_aktuell!json:topic=hasp/plate_2/command/p1b44.jsonl PV_alle_aktuell!json:expression={toJSON({text=>$value,text_color=>"red"})}
PV_alle_Tag!json:topic=hasp/plate_2/command/p1b43.jsonl PV_alle_Tag!json:expression={toJSON({text=>$value,text_color=>"black"})}
BAT_P_aktuell!json:topic=hasp/plate_2/command/p1b37.jsonl BAT_P_aktuell!json:expression={toJSON({text=>$value,text_color=>"red"})}
BAT_TEMP!json:topic=hasp/plate_2/command/p1b40.jsonl BAT_TEMP!json:expression={toJSON({text=>$value,text_color=>"red"})}
BAT_LOADTODAY!json:topic=hasp/plate_2/command/p1b38.jsonl BAT_LOADTODAY!json:expression={toJSON({text=>$value,text_color=>"black"})}
BAT_UNLOADTODAY!json:topic=hasp/plate_2/command/p1b39.jsonl BAT_UNLOADTODAY!json:expression={toJSON({text=>$value,text_color=>"black"})}
ChargeStatus!json:topic=hasp/plate_2/command/p1b36.jsonl ChargeStatus!json:expression={toJSON({text=>$value,text_color=>"red"})}
Haus_total_aktuell!json:topic=hasp/plate_2/command/p1b46.jsonl Haus_total_aktuell!json:expression={toJSON({text=>$value,text_color=>"red",comment=>"test"})}


Nun suche ich noch nach einer Möglichkeit die Farben im Json entsprechen einem Schwellwert zu ändern, ist sicher in FHEM kein Problem 8)

appi

#10
Hallo
ich benötige mal wieder etwas Hilfe...

in meinem Device SMAInverter habe ich ein Userreading welches ich per mqttpublisch an den Broker weiterleite.

PV_alle_Tag!json:topic=hasp/plate_2/command/p1b43.jsonl PV_alle_Tag!json:expression={toJSON({text=>($value/1000)." kW",text_color=>"black"})}
im Broker kommt auch der richtige Wert an:
p1b43.jsonl = {"text":"10.836 kW","text_color":"black"}
Nun würde ich gerne noch die Kommastellen einschränken und übe intensiv am folgenden Code rum

PV_alle_Tag!json:topic=hasp/plate_2/command/p1b45.jsonl PV_alle_Tag!json:expression={toJSON({text=>({(sprintf("%.2f kWh",ReadingsVal("SMAInverter_8KW","PV_alle_Tag",0)/1000))})." kW",text_color=>"black"})}
am Brocker kommt leider nichts venünftiges an.......

p1b45.jsonl = {"text":"HASH(0x5dd42dcc8710) kW","text_color":"black"}
Nach intensiver Suche im Forum bin ich leider nicht fündig geworden, ich wäre um eine Hilfestellung dankbar.

gruss
Remo

tobi01001

#11
Zitat von: appi am 13 März 2025, 19:29:32PV_alle_Tag!json:topic=hasp/plate_2/command/p1b45.jsonl PV_alle_Tag!json:expression={toJSON({text=>({(sprintf("%.2f kWh",ReadingsVal("SMAInverter_8KW","PV_alle_Tag",0)/1000))})." kW",text_color=>"black"})}
am Brocker kommt leider nichts venünftiges an.......

Was mir auffällt: Dein JSON-Objekt ist falsch.
du Bildest über ein objekt aus Schlüssel - Wert also "text=>Wert". Das geschieht über die eckigen Klammern. Allerdings hast du zwischen deinen Schlüssel "text" und "text_color" noch ein ." kw" was zu viel ist.

Zudem steht im sprintf ja bereits die Einheit mit dran (Da stellt sich die Frage welche Einheit es denn nun sein soll).
Ich vermute, dass da die eckicken Klammern als statement da stören
Evtl funktiniert sowas::
PV_alle_Tag!json:topic=hasp/plate_2/command/p1b45.jsonl PV_alle_Tag!json:expression={toJSON({text=>(sprintf("%.2f kWh",ReadingsVal("SMAInverter_8KW","PV_alle_Tag",0)/1000)),text_color=>"black"})}
FHEM@UbuntuServer on Lenovo ThinkCentre M900 [i5-6500T / 8GB RAM] MySQL-DbLog, Grafana, FTUI3 / HmIP incl. CCU3 / LGESS / Wärempumpe über TA CMI und CANoE / Shellies u.v.m.

appi

#12
das wars, mit deinem Vorschlag läuft es auf Anhieb.
besten Dank