🔦 Tasmota über HTTP-WEB-API, WebRequests schalten und pollen, HTTPMOD

Begonnen von Torxgewinde, 10 August 2026, 23:05:34

Vorheriges Thema - Nächstes Thema

Torxgewinde

Hallo,
Bei einem im Außenbereich montiertem Tasmota-Gerät (hier Nous D3T) wollte ich nicht die MQTT Zugangsdaten eintragen, da das Gerät ggf. gestohlen und ausgelesen werden könnte. Tasmota und FHEM bevorzugen eindeutig MQTT, aber man kann die Tasmota Geräte auch genausogut  per WebAPI bzw Web-Requests steuern.

Das Device wird alle 60 Sekunden gepollt. Die Werte kann man mit HTTPMOD auswerten und als Readings erhalten, da sie direkt als JSON bereitstehen.

Wenn man das Relais schaltet wird mittels "FollowGet" direkt auch der Status zurückgelesen. Das ist so schnell wie ein WebRequest eben geht, auf jeden Fall schneller als auf das nächste Pollinginterval zu warten.

Da HTTPMOD eigentlich kein SetMagic anbietet, ich aber auch nicht die URL mehrfach hardcoden möchte, ist es mit einem Attribut gelöst. Damit man es nutzen kann wird ein recht universell nützliches SetMagic-ähnliches Verhalten erreicht indem replacementRegex genutzt wird.

Hier der Snippet dazu:
defmod TasmotaRelais HTTPMOD http://[$name:TasmotaDeviceAddress]/cm?cmnd=STATUS+0 60
attr TasmotaRelais userattr TasmotaDeviceAddress
attr TasmotaRelais TasmotaDeviceAddress 192.168.123.123
attr TasmotaRelais alias Relais für Dingsda
attr TasmotaRelais comment '\
Unter dem Attribut "TasmotaDeviceAddress" trägt man die IP oder den Namen des Tasmota Gerätes ein.\
\
In diesem Beispiel wird der Status abgefragt und in Readings aktualisiert.\
Dies passiert mit dem Pollinterval wie im Define angegeben.\
\
Mit der Set-Funktion kann man das erste Relais "Power1" schalten.\
Da FHEM gerne mit on/off als Wert dafür arbeitet wird dies mit Map angepasst.\
\
Wenn das Tasmota Device direkt geschaltet wird, dauert es solange bis wieder gepollt wird bevor der \
Status korrekt übernommen wird. Das ist naturgemäß ein Problem beim Polling.\
Setzt man mittels FHEM den Relaiszustand, wird direkt danach der Status abgerufen und übernommen.\
'
attr TasmotaRelais disable 0
attr TasmotaRelais extractAllJSON 0
attr TasmotaRelais get01JSON POWER
attr TasmotaRelais get01Map ON:on,OFF:off
attr TasmotaRelais get01Name SwitchState
attr TasmotaRelais get01URL http://[$name:TasmotaDeviceAddress]/cm?cmnd=POWER
attr TasmotaRelais reading01JSON StatusSNS_Time
attr TasmotaRelais reading01Name Time
attr TasmotaRelais reading02JSON StatusSNS_ANALOG_Temperature1
attr TasmotaRelais reading02Name Temperature
attr TasmotaRelais reading03JSON StatusSNS_ENERGY_Total
attr TasmotaRelais reading03Name Total
attr TasmotaRelais reading04JSON StatusSNS_ENERGY_Yesterday
attr TasmotaRelais reading04Name Yesterday
attr TasmotaRelais reading05JSON StatusSNS_ENERGY_Today
attr TasmotaRelais reading05Name Today
attr TasmotaRelais reading06JSON StatusSNS_ENERGY_Power
attr TasmotaRelais reading06Name Power
attr TasmotaRelais reading07JSON StatusSNS_ENERGY_ApparentPower
attr TasmotaRelais reading07Name ApparentPower
attr TasmotaRelais reading08JSON StatusSNS_ENERGY_ReactivePower
attr TasmotaRelais reading08Name ReactivePower
attr TasmotaRelais reading09JSON StatusSNS_ENERGY_Factor
attr TasmotaRelais reading09Name Factor
attr TasmotaRelais reading10JSON StatusSNS_ENERGY_Frequency
attr TasmotaRelais reading10Name Frequency
attr TasmotaRelais reading11JSON StatusSNS_ENERGY_Voltage
attr TasmotaRelais reading11Name Voltage
attr TasmotaRelais reading12JSON StatusSNS_ENERGY_Current
attr TasmotaRelais reading12Name Current
attr TasmotaRelais reading13JSON Status_Power
attr TasmotaRelais reading13Map 1:on,0:off
attr TasmotaRelais reading13Name SwitchState
attr TasmotaRelais reading14JSON StatusSTS_Wifi_Signal
attr TasmotaRelais reading14Name WifiSignal
attr TasmotaRelais replacement01Mode expression
attr TasmotaRelais replacement01Regex \[([^:]+):([^\]]+)\]
attr TasmotaRelais replacement01Value my $device = $name if ($1 eq "\$name") // $1;;\
ReadingsVal($device, $2, undef) or AttrVal($device, $2, "???");;
attr TasmotaRelais room Dingsda
attr TasmotaRelais set01FollowGet SwitchState
attr TasmotaRelais set01Hint on,off
attr TasmotaRelais set01Name Relais
attr TasmotaRelais set01TextArg 1
attr TasmotaRelais set01URL http://[$name:TasmotaDeviceAddress]/cm?cmnd=Power1%20$val
attr TasmotaRelais showBody 0
attr TasmotaRelais showMatched 0
attr TasmotaRelais stateFormat {\
  return ReadingsVal($name, 'SwitchState', '???');;\
}
attr TasmotaRelais webCmd Relais
attr TasmotaRelais widgetOverride Relais:uzsuSelectRadio,off,on


So sieht es dann aus:
Du darfst diesen Dateianhang nicht ansehen.