FHEM Forum

FHEM => Anfängerfragen => Thema gestartet von: Maxl am 26 Mai 2025, 11:22:44

Titel: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: Maxl am 26 Mai 2025, 11:22:44
Hallo,

ich versuche ein Python Skript aus FEHM aufzurufen und die Ausgabe in FHEM zu übernehmen, in FHEM ist folgendes definiert:

define KACOExec at +*00:00:30 {my $json = qx(D:\\Program Files\\Python\\Python313\\python.exe D:\\perl\\fhem-6.2\\python\\kaco_export_run.py);; fhem("setreading MyJsonDevice data $json");;}
attr KACOExec event-on-update-reading .*
attr KACOExec room Wetter
#   CFGFN     
#   COMMAND    {my $json = qx(D:\\Program Files\\Python\\Python313\\python.exe D:\\perl\\fhem-6.2\\python\\kaco_export_run.py); fhem("setreading MyJsonDevice data $json");}
#   DEF        +*00:00:30 {my $json = qx(D:\\Program Files\\Python\\Python313\\python.exe D:\\perl\\fhem-6.2\\python\\kaco_export_run.py); fhem("setreading MyJsonDevice data $json");}
#   FUUID      68342ea2-f33f-7117-4c72-745ed69e72c619e2
#   NAME       KACOExec
#   NR         71
#   NTM        11:06:04
#   PERIODIC   yes
#   RELATIVE   yes
#   REP        -1
#   STATE      Next: 11:06:04
#   TIMESPEC   00:00:30
#   TRIGGERTIME 1748250364.75441
#   TRIGGERTIME_FMT 2025-05-26 11:06:04
#   TYPE       at
#   eventCount 3
#   READINGS:
#     2025-05-26 11:05:34   state           Next: 11:06:04
#
setstate KACOExec Next: 11:06:04
setstate KACOExec 2025-05-26 11:05:34 state Next: 11:06:04


führe ich das py-File unter in Windows CMD aus erhalte ich folgende Ausgabe

D:\perl\fhem-6.2\python>python kaco_export_run.py
{
    "power_inv_L1": 1190,
    "power_inv_L2": 1191,
    "power_inv_L3": 1195,
    "power_inv_total": 3576,
    "power_net_total": 0,
    "U_PV_1": 550,
    "U_PV_2": 402,
    "P_PV": 6000,
    "U_Bat": 433,
    "P_Bat": 2425,
    "SOC_Bat": 96,
    "power_sum_cons": 3576,
    "today_grid_feed_in": 0.0,
    "this_month_grid_feed_in": 3.3,
    "today_cons_from_grid": 0.0,
    "this_month_cons_from_grid": 3.3,
    "today_cons_self": 5.8,
    "this_month_today_cons_self": 656.0,
    "today_battery_energy_volume": 6.0,
    "this_month_battery_energy_volume": 328.8,
    "real_grid_feed_in_today": 0.0,
    "real_grid_feed_in_month": 0.0,
    "temp_inverter": 420691337
}

wie bekomme ich nun die Werte in FHEM?

Danke
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: betateilchen am 26 Mai 2025, 14:03:07
Warum machst Du dir das Leben unnötig schwer und benutzt nicht einfach JsonMod?
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: Maxl am 26 Mai 2025, 14:25:03
In der Referenz  define <name> JsonMod <http:example.com:/somepath/somefile.json> soll ich eine Adresse angeben, die habe ich nicht.
Verstehe das nicht recht, ich muss zuerst einmal das Pyton Skript laufen lassen welches dann übergibt.


Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: Otto123 am 26 Mai 2025, 17:32:38
Hallo Maxl,

Ich habe eine grundlegende Frage, in deiner Signature steht:
ZitatFHEM auf Raspberry
Du sagst weiter:
Zitat von: Maxl am 26 Mai 2025, 11:22:44führe ich das py-File unter in Windows CMD aus erhalte ich folgende Ausgabe
Dein qx Befehl sieht aus, als ob Du FHEM unter Windows laufen lässt?

Gruß Otto
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: betateilchen am 26 Mai 2025, 18:39:32
wo ist eigentlich die Antwort, die ich vorhin hier geschrieben habe? *grübel*
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: betateilchen am 26 Mai 2025, 18:43:05
ok, ich versuchs nochmal...

Zitat von: Maxl am 26 Mai 2025, 14:25:03In der Referenz  define <name> JsonMod <http:example.com:/somepath/somefile.json> soll ich eine Adresse angeben, die habe ich nicht.
Verstehe das nicht recht, ich muss zuerst einmal das Pyton Skript laufen lassen welches dann übergibt.

Du solltest die gesamte commandref lesen, die Variante mit der Adresse ist nur eine von mehreren Möglichkeiten.

Define
define <name> JsonMod <http[s]:example.com:/somepath/somefile.json>

defines the device and set the source (file:|http:|https:|system://).

files example:
file:[//]/path/file (absolute)
file:[//]path/file (realtive)

system example:
system://curl -X POST "https://httpbin.org/anything" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"login":"my_login","password":"my_password"}'

In Deinem Szenario wäre das "system example" relevant. Damit lassen sich Betriebssystemaufrufe durchführen und das Ergebnis auswerten.
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: Maxl am 26 Mai 2025, 21:15:10
Ich hätte es versucht, es kommt aber nichts an, in KACOOutput ist nichts zu finden. Zu Ottos Frage, ich bin unter Windows unterwegs, gerade einfacher zum debuggen, später dann auf den Raspberry.

versuch mit JsonMod
define KACOExec at +*00:00:30 {\
  my $json = qx(D:\\Program Files\\Python\\Python313\\python.exe D:\\perl\\fhem-6.2\\python\\kaco_export_run.py);;\
  $json =~ s/"/\\"/g;; \
  fhem("setreading KACOOutput data $json");; \
  system("curl -X POST \"https://httpbin.org/anything\" -H \"Accept: application/json\" -H \"Content-Type: application/json\" -d \"$json\"");; \
}
attr KACOExec event-on-update-reading .*
attr KACOExec room Wetter
#   CFGFN     
#   COMMAND    {
#  my $json = qx(D:\\Program Files\\Python\\Python313\\python.exe D:\\perl\\fhem-6.2\\python\\kaco_export_run.py);
#  $json =~ s/"/\\"/g;
#  fhem("setreading KACOOutput data $json");
#  system("curl -X POST \"https://httpbin.org/anything\" -H \"Accept: application/json\" -H \"Content-Type: application/json\" -d \"$json\"");
#}
#   DEF        +*00:00:30 {
#  my $json = qx(D:\\Program Files\\Python\\Python313\\python.exe D:\\perl\\fhem-6.2\\python\\kaco_export_run.py);
#  $json =~ s/"/\\"/g;
#  fhem("setreading KACOOutput data $json");
#  system("curl -X POST \"https://httpbin.org/anything\" -H \"Accept: application/json\" -H \"Content-Type: application/json\" -d \"$json\"");
#}
#   FUUID      6834bc02-f33f-7117-2318-3e9c59a57896bc38
#   NAME       KACOExec
#   NR         66
#   NTM        21:14:16
#   PERIODIC   yes
#   RELATIVE   yes
#   REP        -1
#   STATE      Next: 21:14:16
#   TIMESPEC   00:00:30
#   TRIGGERTIME 1748286856.49238
#   TRIGGERTIME_FMT 2025-05-26 21:14:16
#   TYPE       at
#   eventCount 13
#   READINGS:
#     2025-05-26 21:13:46   state           Next: 21:14:16
#
setstate KACOExec Next: 21:14:16
setstate KACOExec 2025-05-26 21:13:46 state Next: 21:14:16



im Logfile finde ich Folgendes
2025.05.26 21:11:16.495 5: exec at command KACOExec
2025.05.26 21:11:16.495 5: Cmd: >{
  my $json = qx(D:\\Program Files\\Python\\Python313\\python.exe D:\\perl\\fhem-6.2\\python\\kaco_export_run.py);
  $json =~ s/"/\\"/g;
  fhem("setreading KACOOutput data $json");
  system("curl -X POST \"https://httpbin.org/anything\" -H \"Accept: application/json\" -H \"Content-Type: application/json\" -d \"$json\"");
}<
2025.05.26 21:11:16.532 5: Cmd: >setreading KACOOutput data<
2025.05.26 21:11:16.532 3: setreading KACOOutput data  : Usage: setreading <name> [YYYY-MM-DD HH:MM:SS] <reading> <value>
where <name> is a single device name, a list separated by comma (,) or a regexp. See the devspec section in the commandref.html for details.

2025.05.26 21:11:16.827 3: KACOExec: 8960
2025.05.26 21:11:16.828 5: redefine at command KACOExec as +*00:00:30 {
  my $json = qx(D:\\Program Files\\Python\\Python313\\python.exe D:\\perl\\fhem-6.2\\python\\kaco_export_run.py);
  $json =~ s/"/\\"/g;
  fhem("setreading KACOOutput data $json");
  system("curl -X POST \"https://httpbin.org/anything\" -H \"Accept: application/json\" -H \"Content-Type: application/json\" -d \"$json\"");
}
2025.05.26 21:11:16.828 5: Starting notify loop for KACOExec, 1 event(s), first is Next: 21:11:46
2025.05.26 21:11:16.829 5: End notify loop for KACOExec
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: betateilchen am 26 Mai 2025, 21:22:52
Du hast das Prinzip von JsonMod nicht verstanden.
Da kommt weder perlcode in das DEF, noch der Beispielcode mit dem curl aus der commandref...

Und glaube mir bitte: das debuggen ist viel einfacher, wenn Du das auf dem raspberry machst. Unter windows ist in FHEM vieles anders als unter Linux.
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: Maxl am 27 Mai 2025, 09:22:32
Kann schon sein das ich das mit dem JsonMod nicht verstehe, wäre daher für jede Hilfe dankbar. Wo finde ich ein konkretes Beispiel damit ich weiterkomme?
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: passibe am 27 Mai 2025, 11:55:31
Vielleicht hilft das? Keine Ahnung wie gut/passend die Beispiele sind. Aber da scheinen jedenfalls Beispiele zu sein.
https://wiki.fhem.de/wiki/JsonMod#Anwendungsbeispiele
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: Maxl am 28 Mai 2025, 07:34:29
Leider sind hier nur Beipiele mit Webaufrufen (system example) enthalten, funktioniert auch,
sobald ich das Gleiche über meine externen Aufrufe (files example) mache wie hier beschrieben
JSON Mode (https://fhem.de/commandref.html#JsonMod) kommt nichts zurück. Aber worin liegt der Fehler?
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: Beta-User am 28 Mai 2025, 08:17:51
Zitat von: Maxl am 28 Mai 2025, 07:34:29meine externen Aufrufe (files example) [...] Aber worin liegt der Fehler?
Zitat von: betateilchen am 26 Mai 2025, 18:43:05system example:
system://curl -X POST "https://httpbin.org/anything" -H "Accept: application/json" -H "Content-Type: application/json" -d '{"login":"my_login","password":"my_password"}'
Vermutlich darin, dass unklar zu sein scheint, dass "system command" und "shell" (https://commandref.fhem.de/#command) mehr oder weniger synonym verwendet werden.

Anders gesagt: JsonMod erwartet, dass die RÜCKGABE des Aufrufs ein gültiger JSON-Blob ist, egal, ob den ein "normaler Web-Server", ein file oder eben ein Betriebssystem-Befehl wie curl zurückgibt.
Titel: Aw: Ausgabe unter Python in FHEM als Reading übernehmen
Beitrag von: betateilchen am 28 Mai 2025, 08:34:15
Eigentlich ist das doch alles völlig simpel:

defmod jm JsonMod system://<NameDesPythonSkripts>
attr jm readingList complete()

Das PythonSkript muss das Ergebnis als Json zurückliefern (das scheint ja nach Deiner Beschreibung bereits der Fall zu sein) und von FHEM aus aufrufbar sein.

Wenn das erfüllt ist, entstehen alle readings durch das complete() automatisch.
Sollte sich herausstellen, dass man nicht alle readings braucht, kann man danach "verfeinern".
Aber zuerst muss der Aufruf und die Rückgabe korrekt funktionieren.