Fritzbox total bytes send / received counters (accurate)

Cheers Everyone,

Since I did not find anything usefull up to now, I dug a bit into the topic. I wanted to know how much my private network pulled and pushed from and into the WWW. I wanted to know that for “today”, “this week”, this “month” and “total”. The Fritzbox resets with every restart I think and the binding resets randomly. At least that’s what I thought. It’s not random at all and I can use it to get an accurate measure. The binding uses the unsinged integer datatype for the bytes and that means that the range is at an end at 4GB. So every 4GB it resets to zero. Therefore you need a working persistence for it to get going. I use a MariaDB via JDBC. The general persistence strategy is everyUpdate. Please check the tutorial to set it up if you don’t already have.

That’s my config:

Items

Number  fboxWanTotalBytesSent "WAN total bytes sent [JS(bytes.js):%s]" {fritzboxtr064="wanTotalBytesSent"}
Number  fboxWanTotalBytesReceived "WAN total bytes received [JS(bytes.js):%s]" {fritzboxtr064="wanTotalBytesReceived"}
Number  fboxBytesSentToday "WAN bytes sent today [JS(bytes.js):%s]" 
Number  fboxBytesReceivedToday "WAN bytes received today [JS(bytes.js):%s]" 
Number  fboxBytesSentWeek "WAN bytes sent this week [JS(bytes.js):%s]" 
Number  fboxBytesReceivedWeek "WAN bytes received this week [JS(bytes.js):%s]" 
Number  fboxBytesSentMonth "WAN bytes sent this month [JS(bytes.js):%s]" 
Number  fboxBytesReceivedMonth "WAN bytes received this month [JS(bytes.js):%s]" 
Number  fboxBytesSentTotal "WAN bytes sent total [JS(bytes.js):%s]" 
Number  fboxBytesReceivedTotal "WAN bytes received total [JS(bytes.js):%s]"

You need the Javascript Transform addon and create a bytes.js in the transform directory:

(function formatBytes(a){
var d = 2;
e=[“B”,“KB”,“MB”,“GB”,“TB”,“PB”];
if(0==a)return"0 “+e[0];
var c=1e3;
var f=Math.floor(Math.log(a)/Math.log(c));
return parseFloat((a/Math.pow(c,f)).toFixed(d))+” "+e[f];
})(input)

Sitemap

Text item=fboxBytesSentToday 
Text item=fboxBytesReceivedToday
Text item=fboxBytesSentWeek
Text item=fboxBytesReceivedWeek 	
Text item=fboxBytesSentMonth 
Text item=fboxBytesReceivedMonth
Text item=fboxBytesSentTotal 
Text item=fboxBytesReceivedTotal	

rules

rule "FB Total Bytes Send"
	when
		Item fboxWanTotalBytesSent changed
	then
		var pState = fboxWanTotalBytesSent.previousState(true,"jdbc").state
		var cState = fboxWanTotalBytesSent.state
		
		var long previousState = Long.parseLong(pState.toString(), 10) as Number
		var long currentState = Long.parseLong(cState.toString(), 10) as Number
		var long max = Long.parseLong("4294967295", 10) as Number
		if(previousState > currentState)
		{
			currentState = max - previousState + currentState
		}else{
			currentState = currentState - previousState 
		}
		
		
		pState = fboxBytesSentToday.state
		previousState = Long.parseLong(pState.toString(), 10) as Number
		postUpdate(fboxBytesSentToday, previousState + currentState)
		pState = fboxBytesSentWeek.state
		previousState = Long.parseLong(pState.toString(), 10) as Number
		postUpdate(fboxBytesSentWeek, previousState + currentState)
		pState = fboxBytesSentMonth.state
		previousState = Long.parseLong(pState.toString(), 10) as Number
		postUpdate(fboxBytesSentMonth, previousState + currentState)
		pState = fboxBytesSentTotal.state
		previousState = Long.parseLong(pState.toString(), 10) as Number
		postUpdate(fboxBytesSentTotal, previousState + currentState)
	end
		
rule "FB Total Bytes Received"
	when
		Item fboxWanTotalBytesReceived changed
	then
		var pState = fboxWanTotalBytesReceived.previousState(true,"jdbc").state
		var cState = fboxWanTotalBytesReceived.state

		var long previousState = Long.parseLong(pState.toString(), 10) as Number
		var long currentState = Long.parseLong(cState.toString(), 10) as Number
		var long max = Long.parseLong("4294967295", 10) as Number
		if(previousState > currentState)
		{
			currentState = max - previousState + currentState
		}else{
			currentState = currentState - previousState 
		}
		
		pState = fboxBytesReceivedToday.state
		previousState = Long.parseLong(pState.toString(), 10) as Number
		postUpdate(fboxBytesReceivedToday, previousState + currentState)
		pState = fboxBytesReceivedWeek.state
		previousState = Long.parseLong(pState.toString(), 10) as Number
		postUpdate(fboxBytesReceivedWeek, previousState + currentState)
		pState = fboxBytesReceivedMonth.state
		previousState = Long.parseLong(pState.toString(), 10) as Number
		postUpdate(fboxBytesReceivedMonth, previousState + currentState)
		pState = fboxBytesReceivedTotal.state
		previousState = Long.parseLong(pState.toString(), 10) as Number
		postUpdate(fboxBytesReceivedTotal, previousState + currentState)
	end

rule "FB reset daily counter"
	when
		Time cron "0 0 0 * * ?"
	then
		postUpdate(fboxBytesReceivedToday, 0)
		postUpdate(fboxBytesSentToday, 0)
	end
	
rule "FB reset weekly counter"
	when
		Time cron "0 0 0 ? * MON"
	then
		postUpdate(fboxBytesReceivedWeek, 0)
		postUpdate(fboxBytesSentWeek, 0)
	end

rule "FB reset monthly counter"
	when
		Time cron "0 0 0 1 * ?"
	then
		postUpdate(fboxBytesReceivedMonth, 0)
		postUpdate(fboxBytesSentMonth, 0)
	end

Hope this helps
:wink:

1 Like

Sorry for grabbing this one out but this is excatly what im searching for.

But on OH3 the rules don’t work anymore, could anyone have a quick look on it.

that my bytes.js from above (fixed)

(function formatBytes(a){
var d = 2;
e=["B","KB","MB","GB","TB","PB"];
if(0==a)return"0 "+e[0];
var c=1e3;
var f=Math.floor(Math.log(a)/Math.log(c));
return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f];
})(input)

Thats my jdbc.cfg

url=jdbc:sqlite:./fboxBytesTest.db

this db is created with this jdbc.persist

Strategies {
    default = everyUpdate
}

Items {
    fbox* : strategy = everyChange, restoreOnStartup
}

anything else is from above

these are the errors

2021-01-26 08:36:57.493 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fritzbox-2' failed: For input string: "3445425974 B" in fritzbox
2021-01-26 08:36:58.720 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fritzbox-1' failed: For input string: "680199415 B" in fritzbox
2021-01-26 08:37:58.011 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fritzbox-2' failed: For input string: "3464949559 B" in fritzbox
2021-01-26 08:37:59.238 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fritzbox-1' failed: For input string: "681705689 B" in fritzbox
2021-01-26 08:44:02.309 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fritzbox-1' failed: cannot invoke method public abstract org.openhab.core.types.State org.openhab.core.persistence.HistoricItem.getState() on null in fritzbox
2021-01-26 08:45:01.569 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fritzbox-2' failed: cannot invoke method public abstract org.openhab.core.types.State org.openhab.core.persistence.HistoricItem.getState() on null in fritzbox

im not familiar with that kind of rules

dbitem0001

dbitem0002

they dont have any value so as the error above is NULL

but no data is written into it.

Hi, ihave added dummy data to the db

like

timestamp 2021-01-26 11:43:19.77
value 1

Now i got

2021-01-26 12:02:43.308 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fritzbox-2' failed: For input string: "1.0" in fritzbox
2021-01-26 12:02:43.308 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'fritzbox-1' failed: For input string: "1.0" in fritzbox

Hi,

on first glance I would say you need to remove the " B" from your item state value, like:

pState = fboxBytesSentToday.state.toString()
if(pState.endsWith(" B")
{
    previousState = Long.parseLong(pState.substring(0,pState.length()-2), 10) as Number
} else {
    previousState = Long.parseLong(pState, 10) as Number
}

hope that helps

Ok,

my next try to understand the difference between filebased rules and the rules via gui.

Why is this part working in a rule file

var long previousState = Long.parseLong(pState.toString(), 10) as Number
var long currentState = Long.parseLong(cState.toString(), 10) as Number
var long max = Long.parseLong("4294967295", 10) as Number

but this isn’t working if I copy it in a new rule in gui

Type mismatch: cannot convert from Number to long

then the error above occurs in log

If I replace var long with only var no error occurs but the values are wrong, which was to be expected.

Could someone please enlighten me? :slightly_smiling_face:

Edit: if I use it without var long it is totally wrong values
Today Received Box 1000MB
Today Received Script: 75MB
Both set to 0 at midnight

But I can’t use it with long in ui because of converting problems.
I could really need help here, I don’t see it. :sob:

@porsche maybe you have an idea why it don’t work in ui rules?