Advertisement
ottelo15

Tasmota sonoff pow script V2 von User Dennis

Apr 22nd, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.77 KB | None | 0 0
  1. ;Tasmota sonoff pow script V2 von User Dennis
  2. >D 40
  3.  
  4. ; for external ScriptEditor
  5.  
  6. ; Version see >W section
  7.  
  8. IP=192.168.0.46
  9.  
  10. ; SB=6200 only if you have 1M flash
  11.  
  12. ; and compile with #define EEP_SCRIPT_SIZE 6200
  13.  
  14. ; and #define USE_EEPROM
  15.  
  16. SB=6200
  17.  
  18. ; Description:
  19.  
  20. ; Tasmota pow Script with Google Chart Support
  21.  
  22. ; 4h, 24h Consumption Line Charts
  23.  
  24. ; Month and Year Consumption Tables
  25.  
  26. ; HowTo:
  27.  
  28. ; Download the ScriptEditor from Tasmota Script page
  29.  
  30. ; Change the IP and maybe the SB (look for EEP_SCRIPT_SIZE Tasmota Script page)
  31.  
  32. ; change the -- SML -- script to your needs
  33.  
  34. ; search for -300 und change it to your needs (I have only 300W PV) or remove it
  35.  
  36. ; First start, go to console and type: script>=#dreset
  37.  
  38. ; This will set the correct day consumption value
  39.  
  40. ; 4h chart is not permament, after restart data is lost
  41.  
  42. ; 24h, month and year chart is permament. Arrays are saved at midnight.
  43.  
  44. ; to save immediately go to console and enter: script>=#save
  45.  
  46. ; to change some values enter e.g.: script >dcon[day]=xxx
  47.  
  48. ; console commands:
  49.  
  50. ; script?var = get variable
  51.  
  52. ; script >var=1 = set variable
  53.  
  54. ; script>=svars = save permament var
  55.  
  56. ; script>=#sub = run sub
  57.  
  58. ; -- ARRAYS --
  59.  
  60. ; 24h power chart
  61.  
  62. M:p:sday=0 96
  63.  
  64. ; daily energy consumption table day 1-31
  65.  
  66. M:p:dcon=0 31
  67.  
  68. ; monthly energy consumption table month 1-12
  69.  
  70. M:p:mcon=0 12
  71.  
  72. ; 4h power chart
  73.  
  74. M:s4h=0 960
  75.  
  76. ; -- VARS --
  77.  
  78. ; energy from grid [kWh]
  79.  
  80. EnFrGrid=0
  81.  
  82. ; energy from grid yesterday [kWh]
  83.  
  84. EnYesterday=0
  85.  
  86. ; actual power from or to grid [W]
  87.  
  88. power=0
  89.  
  90. ; monthval and dayval
  91.  
  92. p:mval=0
  93.  
  94. p:dval=0
  95.  
  96. tmp=0
  97.  
  98. m15=0
  99.  
  100. cstr="cnt0/4"
  101.  
  102. cstr2="cnth0/240"
  103.  
  104. avgval=0
  105.  
  106. avgvalcnt=0
  107.  
  108. hour=0
  109.  
  110. mont=0
  111.  
  112. minutes=0
  113.  
  114. ; -- BOOT --
  115.  
  116. >B
  117.  
  118. is(0 "Jan|Feb|Mär|Apr|Mai|Jun|Jul|Aug|Sep|Okt|Nov|Dez|")
  119.  
  120. ; -- SUBS --
  121.  
  122. ; 24h power chart
  123.  
  124. #dreset
  125.  
  126. for tmp 1 sday[-1] 1
  127.  
  128. sday[tmp]=0
  129.  
  130. next
  131.  
  132. svars
  133.  
  134. ; daily consumption month chart
  135.  
  136. ; !execute this at the first time!
  137.  
  138. #dtreset
  139.  
  140. for tmp 1 dcon[-1] 1
  141.  
  142. dcon[tmp]=0
  143.  
  144. next
  145.  
  146. dval=EnFrGrid
  147.  
  148. svars
  149.  
  150. ; monthly consumption year chart
  151.  
  152. #mtreset
  153.  
  154. for tmp 1 mcon[-1] 1
  155.  
  156. mcon[tmp]=0
  157.  
  158. next
  159.  
  160. mval=0
  161.  
  162. svars
  163.  
  164. ; save
  165.  
  166. #save
  167.  
  168. print saving
  169.  
  170. svars
  171.  
  172. ; daily consumption month table
  173.  
  174. #daysub
  175.  
  176. wcs <div id="chartday" style="text-align:center;width:400px;height:100%%;padding:0px"></div>
  177.  
  178. wcs <script language="JavaScript">function drawChart(){
  179.  
  180. wcs var cssc={'headerRow':'hRow','rowNumberCell':'hCol','tableCell':'tCell'};
  181.  
  182. wcs var data=google.visualization.arrayToDataTable([['Tag','Verbrauch [kWh]',{role: 'style'}],
  183.  
  184. for tmp 1 dcon[-1] 1
  185.  
  186. if (tmp<day)
  187.  
  188. then
  189.  
  190. wcs [%tmp%,%dcon[tmp]%,'green'],
  191.  
  192. endif
  193.  
  194. if (tmp==day)
  195.  
  196. then
  197.  
  198. wcs [%tmp%,%dcon[tmp]%,'red'],
  199.  
  200. endif
  201.  
  202. if (tmp>day)
  203.  
  204. then
  205.  
  206. wcs [%tmp%,%dcon[tmp]%,''],
  207.  
  208. endif
  209.  
  210. next
  211.  
  212. wcs ]);
  213.  
  214. wcs var options={chartArea:{left:40,width:'86%%'},width:'100%%',legend:'none',title:'Tagesverbräuche Monatsansicht',vAxis:{format:'# kWh'},hAxis:{title:'Tag',ticks:[1,5,10,15,20,25,30]}};
  215.  
  216. wcs var chart=new google.visualization.ColumnChart(document.getElementById('chartday'));
  217.  
  218. wcs chart.draw(data,options);}google.charts.setOnLoadCallback(drawChart);</script>
  219.  
  220. ; monthly consumption year table
  221.  
  222. #monthsub
  223.  
  224. wcs <div id="chartmonth" style="text-align:center;width:400px;height:100%%;padding:0px"></div>
  225.  
  226. wcs <script language="JavaScript">function drawChart(){
  227.  
  228. wcs var cssc={'headerRow':'hRow','rowNumberCell':'hCol','tableCell':'tCell'};
  229.  
  230. wcs var data=google.visualization.arrayToDataTable([['Monat','Verbrauch [kWh]',{role: 'style'}],
  231.  
  232. for tmp 1 mcon[-1] 1
  233.  
  234. if (tmp<month)
  235.  
  236. then
  237.  
  238. wcs ['%is[tmp]%',%mcon[tmp]%,'green'],
  239.  
  240. endif
  241.  
  242. if (tmp==month)
  243.  
  244. then
  245.  
  246. wcs ['%is[tmp]%',%mcon[tmp]%,'red'],
  247.  
  248. endif
  249.  
  250. if (tmp>month)
  251.  
  252. then
  253.  
  254. wcs ['%is[tmp]%',%mcon[tmp]%,''],
  255.  
  256. endif
  257.  
  258. next
  259.  
  260. wcs ]);
  261.  
  262. wcs var options={chartArea:{left:40,width:'86%%'},width:'100%%',legend:'none',title:'Monatsverbräuche Jahresansicht',vAxis:{format:'# kWh'}};
  263.  
  264. wcs var chart=new google.visualization.ColumnChart(document.getElementById('chartmonth'));
  265.  
  266. wcs chart.draw(data,options);}google.charts.setOnLoadCallback(drawChart);</script>
  267.  
  268. ; -- EVERY SECOND --
  269.  
  270. >S
  271.  
  272. ;be sure the unit has the correct date already, otherwise arrays can be corrupted
  273.  
  274. if (year>2022)
  275.  
  276. {
  277.  
  278. ;begin 15s section
  279.  
  280. if (secs%15==0)
  281.  
  282. {
  283.  
  284. hour=hours
  285.  
  286. mont=month
  287.  
  288. ; copy SML values
  289.  
  290. ; energy from grid [kWh]
  291.  
  292. EnFrGrid=enrg[11]
  293.  
  294. ; energy from grid yesterday [kWh]
  295.  
  296. EnYesterday=enrg[12]
  297.  
  298. ; actual power [W]
  299.  
  300. ;power=rnd(1000) for google chart testing
  301.  
  302. power=enrg[7]
  303.  
  304. ; day consumption [kWh]
  305.  
  306. dcon[day]=EnFrGrid
  307.  
  308. ; month consumption [kWh]
  309.  
  310. mcon[month]=mval+EnFrGrid
  311.  
  312. ; MQTT
  313.  
  314. ;=>publish tele/%topic%/SENSOR {"Time":"%tstamp%","MT175":{"daily_con":%2dcon[day]%,"monthly_con":%2mcon[month]%}}
  315.  
  316. ; 4h power chart
  317.  
  318. ; power to array, idx is set automatically
  319.  
  320. s4h=power
  321.  
  322. ; sum up power for "24h chart"
  323.  
  324. avgval+=power
  325.  
  326. avgvalcnt+=1;
  327.  
  328. ; set x axis index to 4 hours a 15 sec therefore /240, no decimal point
  329.  
  330. hour-=4
  331.  
  332. if (hour<0)
  333.  
  334. {
  335.  
  336. cstr2="cnth"+s(1.0((24-4+hours)*240)+(mins*4))+"/240"
  337.  
  338. }
  339.  
  340. else
  341.  
  342. {
  343.  
  344. cstr2="cnth"+s(1.0((hours-4)*240)+(mins*4))+"/240"
  345.  
  346. }
  347.  
  348. hour=hours
  349.  
  350.  
  351. ; set x axis idx to 24 hours a 15 min therefore /4, no decimal point
  352.  
  353. if (hours==23)
  354.  
  355. {
  356.  
  357. cstr="cnt0/4"
  358.  
  359. }
  360.  
  361. else
  362.  
  363. {
  364.  
  365. cstr="cnt"+s(1.0(hours+1)*4)+"/4"
  366.  
  367. }
  368.  
  369.  
  370. ; 24h power chart
  371.  
  372. ; calc avg power [W] for last 15min and put into array[1-96]
  373.  
  374. m15=int((((hours*60)+mins)/15)+1)
  375.  
  376. if ((mins%15==0) and (chg[m15]>0))
  377.  
  378. then
  379.  
  380. sday[m15]=int(avgval/avgvalcnt)
  381.  
  382. avgval=0
  383.  
  384. avgvalcnt=0
  385.  
  386. ;svars save too often will damage flash
  387.  
  388. endif
  389.  
  390. ; set idx (pointer starts from 0!!)
  391.  
  392. sday[0]=m15
  393.  
  394. ; day consumption calculation at midnight
  395.  
  396. if ((chg[hour]>0) and (hour==0))
  397.  
  398. then
  399.  
  400. print Its midnight
  401.  
  402. mval=mval+EnYesterday
  403.  
  404. ; month change?
  405.  
  406. if (chg[mont]>0)
  407.  
  408. then
  409.  
  410. mval=0
  411.  
  412. endif
  413.  
  414. ; save only 1 at midnight
  415.  
  416. svars
  417.  
  418. endif
  419.  
  420. }
  421.  
  422. ;end 15s section
  423.  
  424.  
  425. }
  426. ;end year section
  427.  
  428. ; WEB INTERFACE
  429.  
  430. >W
  431.  
  432. ; Auto reload
  433.  
  434. ;$<script> setTimeout("location.reload(true);",5000); </script>
  435.  
  436. ; consumption
  437.  
  438. Tagesverbrauch:{m}%5(EnFrGrid)% kWh
  439.  
  440. Monatsverbrauch:{m}%5(mval+EnFrGrid)% kWh
  441.  
  442. ; Time/Date
  443.  
  444. Datum:{m}%s(2.0day)%.%s(2.0month)%.%s(2.0year)% - %s(2.0hours)%:%s(2.0mins)%:%s(2.0secs)%
  445.  
  446. Uptime:{m}%0uptime% min
  447.  
  448. $<div style="margin-left:-20px">
  449.  
  450. ; 4h power chart
  451.  
  452. $<div id="chart1" style="text-align:center;width:400px;height:100%%;padding:0px"></div>
  453.  
  454. $gc(lt s4h "wr" "Leistung [W]" cstr2)
  455.  
  456. $var options = {
  457.  
  458. $chartArea:{left:60,width:'83%%'},
  459.  
  460. $width:'100%%',
  461.  
  462. $legend:'none',
  463.  
  464. $vAxis:{format:'# W',viewWindow:{min:0}},
  465.  
  466. $explorer:{actions:['dragToZoom', 'rightClickToReset']},
  467.  
  468. $series: {0: {type: 'area'}},
  469.  
  470. $title:'Verbrauch 4 Stunden [Watt]'
  471.  
  472. $};
  473.  
  474. $gc(e)
  475.  
  476. ; 24h power chart
  477.  
  478. $<div id="chart2" style="text-align:center;width:400px;height:100%%;padding:0px"></div>
  479.  
  480. $gc(lt sday "wr" "Leistung [W]" cstr)
  481.  
  482. $var options = {
  483.  
  484. $chartArea:{left:60,width:'83%%'},
  485.  
  486. $width:'100%%',
  487.  
  488. $legend:'none',
  489.  
  490. $vAxis:{format:'# W',viewWindow:{min:0}},
  491.  
  492. $explorer:{actions:['dragToZoom', 'rightClickToReset']},
  493.  
  494. $series: {0: {type: 'area'}},
  495.  
  496. $title:'Verbrauch 24 Stunden [Watt]'
  497.  
  498. $};
  499.  
  500. $gc(e)
  501.  
  502. ; monthly energy consumption year chart
  503.  
  504. %=#daysub
  505.  
  506. ; monthly energy consumption year chart
  507.  
  508. %=#monthsub
  509.  
  510. $<center><span style="font-size:10px;">
  511.  
  512. $Version 2023.02.13 (pow) by Ottelo mod by Dennis<br>
  513.  
  514. $free heap: %s(0.0heap)% bytes<br>
  515.  
  516. $Hinweis: Die Daten werden immer um Mitternacht gespeichert!<br>
  517.  
  518. $Sofort speichern dies in Console eingeben: "script>=&num;save"<br>
  519.  
  520. $</span></center>
  521. $</div>
  522.  
  523. ; -- END SCRIPT --
  524.  
  525. #
  526.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement