Python Plugin: Homepage Counter - Linechart

Python and python framework
Post Reply
Posts: 11
Joined: Wednesday 10 June 2015 21:36
Target OS: Raspberry Pi
Domoticz version:

Python Plugin: Homepage Counter - Linechart

Post by rwblinn » 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.


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="">
        <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">
                <option label="True" value="Debug"/>
                <option label="False" value="Normal"  default="true" />

import Domoticz
import urllib
import urllib.request

class BasePlugin:
    enabled = False
    pluginState = "Not Ready"

    def __init__(self):

    def onStart(self):
        if Parameters["Mode6"] == "Debug":
        if (len(Devices) == 0):
            Domoticz.Device(Name="Homepage Counter", Unit=1, TypeName="Counter Incremental", Options={"Counter Incremental": "1;Hits"}).Create()
            Domoticz.Log("Device created.")

        #url = "http..."
        url = Parameters["Address"]
        response = urllib.request.urlopen(url).read()
        Domoticz.Debug("Previous Counter:" + str(response,'utf-8'))


    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

def onStop():
    global _plugin

def onHeartbeat():
    global _plugin

# 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 + "'")           
Domoticz v3.4834, Raspberry Pi 1 B+/2 B/3, RFXtrx433e USB, 2* Somfy RTS, 6* TFA TS34C

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests