@dave83
Der Ansatz von @liv-in-sky ist gut. Leider Protokolliert der Ansatz die Laufzeit nur. Anbei ein Beispiel welches die Laufzeit protokolliert UND nach 1 Stunde abschaltet, und nur einen State (Die bisher vom Gerät verbrauchte Laufzeit) benötigt.
Screenshot 2023-01-07 at 09.18.50.png
Im Spoiler findest du das auch als XML Import. Wichtig ist: Die Bausteine state value, state timestamp und previous timestamp sind identisch - der mit Objekt ID voreingestellte Baustein aus dem Triggerbereich, nur auf die jeweiligen Werte umgestellt.
Das Zurücksetzen via Zeitplan habe ich nicht nachgebaut - das brauchst du ggf. auch - zumindest zu anzeigezwecken oder in dem weiter unten beschriebenen Fall. Das Skript funktioniert aber auch ohne diesen Zeitplan.
Das Zurücksetzen der gespeicherten Laufzeit findet Statt sobald der Status des Gerätes sich das erste mal am "Folgetag" ändert.
Daraus folgt auch das
die Berechnete Laufzeit des Gerätes davon abhängt wie lange es am Einschalttag schon Gelaufen ist
die Laufzeit des Gerätes dem Ausschalttag zugeordnet wird.
Das ist interessant für Fälle wo das Gerät kurz vor Mitternacht eingeschaltet wird.
Nebenbei wird das Gerät auch direkt wieder ausgeschaltet sofern es irgendwie eingeschaltet wurde und die Laufzeit bereits erreicht war (extern)
Dadurch kann sich die Laufzeit um ein paar ms pro Versuch erhöhen. Wenn das Abschalten nicht gewünscht ist muss der else Zweig beim Vergleich von currentRunTime und MaxRunTime entfallen. In diesem Fall muss extern geprüft werden ob noch Laufzeit verfügbar ist bevor das Gerät eingeschaltet wird - Das erfordert dann wahrscheinlich das Rücksetzen der Laufzeit via Zeitplan um Mitternacht.
Spoiler
<xml xmlns="https://developers.google.com/blockly/xml">
<variables>
<variable id="84-fD!=hq}M8k+AJ4S)6">MaxRunTime</variable>
<variable id=")nYhOjt[M,SIo@JZ~$ha">CurrentRunTime</variable>
<variable type="timeout" id="timeout">timeout</variable>
</variables>
<block type="variables_set" id="G4+ubMXM%eUW||Cgm45?" x="37" y="38">
<field name="VAR" id="84-fD!=hq}M8k+AJ4S)6">MaxRunTime</field>
<value name="VALUE">
<block type="math_round" id="_ZZA#{!!SEpQ`%9fBLvr">
<field name="OP">ROUND</field>
<value name="NUM">
<shadow type="math_number" id="q+XTKZYijqmr_;|+b2Ej">
<field name="NUM">3.1</field>
</shadow>
<block type="math_arithmetic" id="]lbrmKh2Y~m0AVq!{V/J">
<field name="OP">MULTIPLY</field>
<value name="A">
<shadow type="math_number" id="tOTh=[C:S/S/tEaQL|^r">
<field name="NUM">1</field>
</shadow>
</value>
<value name="B">
<shadow type="math_number" id="_Q1kCu;#:HKRKd^Q,$)q">
<field name="NUM">3600000</field>
</shadow>
</value>
</block>
</value>
</block>
</value>
<next>
<block type="on_ext" id="n_q6rDEe0lBqbVoK.?iH">
<mutation xmlns="http://www.w3.org/1999/xhtml" items="1"></mutation>
<field name="CONDITION">ne</field>
<field name="ACK_CONDITION"></field>
<value name="OID0">
<shadow type="field_oid" id="Evmvh7x,F_w!]M3xoX_A">
<field name="oid">0_userdata.0.example_state</field>
</shadow>
</value>
<statement name="STATEMENT">
<block type="timeouts_cleartimeout" id="q5`0l),%y})*]b;~XXp+">
<field name="NAME">timeout</field>
<next>
<block type="controls_if" id="#]tB`:V%1xnybmp+Pe{z">
<mutation else="1"></mutation>
<value name="IF0">
<block type="logic_compare" id=":x(oo}C1-7-%[|;Xlk*," inline="false">
<field name="OP">NEQ</field>
<value name="A">
<block type="math_modulo" id="%[8_2OktCW~=`j#.L0+z">
<value name="DIVIDEND">
<shadow type="math_number" id="1Keopd=XH9t5ki?;!euu">
<field name="NUM">64</field>
</shadow>
<block type="on_source" id="b~vNc_.Y[4VO9Vdz@_ME">
<field name="ATTR">state.ts</field>
</block>
</value>
<value name="DIVISOR">
<shadow type="math_number" id="8X,sYoXe+;]f+2^+p0Ep">
<field name="NUM">86400000</field>
</shadow>
</value>
</block>
</value>
<value name="B">
<block type="math_modulo" id="y_sJWO+$U/r]K@{MAV~o">
<value name="DIVIDEND">
<shadow type="math_number" id="+S^V~AkC!TKmX5sJ[[vT">
<field name="NUM">64</field>
</shadow>
<block type="on_source" id="!F$]n.!OxSg//$k!.X*~">
<field name="ATTR">oldState.ts</field>
</block>
</value>
<value name="DIVISOR">
<shadow type="math_number" id="~|$@rjQkBZD+d(q9n!NY">
<field name="NUM">86400000</field>
</shadow>
</value>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="variables_set" id="-eLjMsoBxcOzzc`1ri+G">
<field name="VAR" id=")nYhOjt[M,SIo@JZ~$ha">CurrentRunTime</field>
<value name="VALUE">
<block type="get_value" id="=?IBNdc-4Zd=4KRARi$:">
<field name="ATTR">val</field>
<field name="OID">select ID</field>
</block>
</value>
</block>
</statement>
<statement name="ELSE">
<block type="variables_set" id=")^CyYvO]oY9$/{=BIH_S">
<field name="VAR" id=")nYhOjt[M,SIo@JZ~$ha">CurrentRunTime</field>
<value name="VALUE">
<block type="math_number" id="Qh@!+8GT5b_flmAn?5)j">
<field name="NUM">0</field>
</block>
</value>
<next>
<block type="update" id="@8W?v}HmBJe*LghGdpyz">
<mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="false"></mutation>
<field name="OID">Object ID</field>
<field name="WITH_DELAY">FALSE</field>
<value name="VALUE">
<block type="variables_get" id="o~!k++_E[xW(!~`HVr^5">
<field name="VAR" id=")nYhOjt[M,SIo@JZ~$ha">CurrentRunTime</field>
</block>
</value>
</block>
</next>
</block>
</statement>
<next>
<block type="controls_if" id="DzH43V#WCKok#hV*IZRZ">
<mutation else="1"></mutation>
<value name="IF0">
<block type="on_source" id="!.+2/o6BNf%F}g@|h4O~">
<field name="ATTR">state.val</field>
</block>
</value>
<statement name="DO0">
<block type="controls_if" id="BC-2y?P9)?|8ltz79?4?">
<mutation else="1"></mutation>
<value name="IF0">
<block type="logic_compare" id="%;yejoMnX~xyt9D6Vjlt">
<field name="OP">LT</field>
<value name="A">
<block type="variables_get" id="8wNSX)#XWR_D~J_Fz5SD">
<field name="VAR" id=")nYhOjt[M,SIo@JZ~$ha">CurrentRunTime</field>
</block>
</value>
<value name="B">
<block type="variables_get" id="1ebj(S24SbVsIC]c?}RS">
<field name="VAR" id="84-fD!=hq}M8k+AJ4S)6">MaxRunTime</field>
</block>
</value>
</block>
</value>
<statement name="DO0">
<block type="timeouts_settimeout_variable" id="OJYjtak,1Gfl10q5w]?m">
<field name="NAME">timeout</field>
<value name="DELAY_MS">
<shadow type="math_number" id="hbHOWH9M@2RckL].Lq^k">
<field name="NUM">1000</field>
</shadow>
<block type="math_arithmetic" id="v$JXenIM2f]WT3*ldI,X">
<field name="OP">MINUS</field>
<value name="A">
<shadow type="math_number" id="2Qp097K0|R[:N2#[SpZL">
<field name="NUM">1</field>
</shadow>
<block type="variables_get" id="(E0F`JYM!4(%er2.A`JD">
<field name="VAR" id="84-fD!=hq}M8k+AJ4S)6">MaxRunTime</field>
</block>
</value>
<value name="B">
<shadow type="math_number" id="X)g`d:`,I6Q2WOvFttxN">
<field name="NUM">1</field>
</shadow>
<block type="variables_get" id="_Zjb_0}q|}hDU=VT6RhE">
<field name="VAR" id=")nYhOjt[M,SIo@JZ~$ha">CurrentRunTime</field>
</block>
</value>
</block>
</value>
<statement name="STATEMENT">
<block type="control" id="8N7U0JA#k}[$+)8{/Lv[">
<mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="false"></mutation>
<field name="OID">Object ID</field>
<field name="WITH_DELAY">FALSE</field>
<value name="VALUE">
<block type="logic_boolean" id="C9jkT**U{AuOI;e}oi[{">
<field name="BOOL">FALSE</field>
</block>
</value>
</block>
</statement>
</block>
</statement>
<statement name="ELSE">
<block type="control" id="yyygqK1kcH]VkTl~nXB(">
<mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="false"></mutation>
<field name="OID">Object ID</field>
<field name="WITH_DELAY">FALSE</field>
<value name="VALUE">
<block type="logic_boolean" id="17gLk35f649C~i|{hgj/">
<field name="BOOL">FALSE</field>
</block>
</value>
</block>
</statement>
</block>
</statement>
<statement name="ELSE">
<block type="update" id="%$D3%/}6iof%dtK?7CDx">
<mutation xmlns="http://www.w3.org/1999/xhtml" delay_input="false"></mutation>
<field name="OID">Object ID</field>
<field name="WITH_DELAY">FALSE</field>
<value name="VALUE">
<block type="math_arithmetic" id="x41UvXU8Al9N5-66vJXp" inline="false">
<field name="OP">ADD</field>
<value name="A">
<shadow type="math_number" id="#fZnVL03hHR$L[jpn$}E">
<field name="NUM">1</field>
</shadow>
<block type="variables_get" id="mtscSryz8GzJ3yBq!i]u">
<field name="VAR" id=")nYhOjt[M,SIo@JZ~$ha">CurrentRunTime</field>
</block>
</value>
<value name="B">
<shadow type="math_number" id="{?,linc];S74%/x/A:w$">
<field name="NUM">1</field>
</shadow>
<block type="math_arithmetic" id="r;^x:N:CVnisA2e5k4K%">
<field name="OP">ADD</field>
<value name="A">
<shadow type="math_number" id="1oSOVWw1np}-*D7/Lme*">
<field name="NUM">1</field>
</shadow>
<block type="on_source" id="vrfVZ-v@aQltO:DL5rNp">
<field name="ATTR">state.ts</field>
</block>
</value>
<value name="B">
<shadow type="math_number" id="E[]1XpGB6o;-)zog1kvT">
<field name="NUM">1</field>
</shadow>
<block type="on_source" id="q:fVd!R3X$#ivd`}~[+;">
<field name="ATTR">oldState.ts</field>
</block>
</value>
</block>
</value>
</block>
</value>
</block>
</statement>
</block>
</next>
</block>
</next>
</block>
</statement>
</block>
</next>
</block>
</xml>