Python Plugin: Homepage Counter - Linechart
Posted: Thursday 10 August 2017 8:56
Whilst learning building a Python Plugin (with Domoticz V3.8263) developed a simple Homepage Counter plugin (running on the Raspberry Pi 3 latest Raspian) with a device typename "Counter Incremental". The example widget shows the total hits (1752735) and the hits per day (3). The chart shows the hits per day by hour etc.


Question:
What device can be used and how to define in the plugin to display the total hits (1752735) as a linechart with total hits on the Y-Axis (like f.e. the distance device showing total length in cm/inch). The widget to display the total hits as an int value (and not e notation).
Plugin Code


Question:
What device can be used and how to define in the plugin to display the total hits (1752735) as a linechart with total hits on the Y-Axis (like f.e. the distance device showing total length in cm/inch). The widget to display the total hits as an int value (and not e notation).
Plugin Code
Code: Select all
# Read the content of a homepagecounter text file and display as device
"""
<plugin key="HomepageCounter" name="Homepage Counter" author="rwbl" version="1.0.0" externallink="http://www.rwblinn.de">
<params>
<param field="Address" label="URL Homepage Counter" width="300px" required="true" default="http://address/pagecounterfile.txt"/>
<param field="Mode1" label="Check Interval (seconds)" width="75px" required="true" default="60"/>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import urllib
import urllib.request
class BasePlugin:
enabled = False
pluginState = "Not Ready"
def __init__(self):
return
def onStart(self):
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
if (len(Devices) == 0):
Domoticz.Device(Name="Homepage Counter", Unit=1, TypeName="Counter Incremental", Options={"Counter Incremental": "1;Hits"}).Create()
Domoticz.Log("Device created.")
DumpConfigToLog()
#url = "http..."
url = Parameters["Address"]
response = urllib.request.urlopen(url).read()
Domoticz.Debug("Previous Counter:" + str(response,'utf-8'))
Domoticz.Heartbeat(int(Parameters["Mode1"]))
def onStop(self):
Domoticz.Log("Plugin is stopping.")
def onHeartbeat(self):
Domoticz.Debug("onHeartbeat called")
# Get the url with the text file containing the homepage counter value
url = Parameters["Address"]
# Open the url and read the content
response = urllib.request.urlopen(url).read()
# Convert the response from byte to string and strip special characters
hpc = str(response,'utf-8')
hpc = hpc.strip(' \t\n\r')
# Set the new counter as int
NewCounter = int(hpc)
# Capture the current sensor value for device with unit = 1
CurCounter = Devices[1].nValue
# If first time the CurCounter is 0
if CurCounter == 0:
CurCounter = NewCounter
Devices[1].Update( nValue=NewCounter, sValue=str(NewCounter) )
Domoticz.Log("HomepageCounter initialized:" + str(NewCounter) )
# Calculate the delta counter
DeltaCounter = NewCounter - CurCounter
# Update the counter if the delta counter > 0
if DeltaCounter > 0:
Devices[1].Update( nValue=DeltaCounter, sValue=str(DeltaCounter) )
CurCounter = Devices[1].nValue
Domoticz.Log("HomepageCounter updated by " + str(DeltaCounter) + " to " + str(CurCounter) )
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
return