Domoticz Daily Usage Energy

From Domoticz
Jump to: navigation, search

Purpose

This script will send you the daily usage of your switch (in my example the Fibaro Wall Plug) in a Prowl notification at 23:59 every day AND will write a .txt file on your device which contains the readings + date.
If you want to keep track of the usage of a particular device outside of Domoticz, this script will help you. The Prowl message will contain the same values as if you would look at the values in Domoticz, but with a long(er) history.

Example.jpg

Dependencies - hardware / software / operating system

Prowl

Prowl is currently only for iOS devices:
Prowl is the Growl client for iOS. Push to your iPhone, iPod touch, or iPad notifications from a Mac or Windows computer, or from a multitude of apps and services. Easily integrate the Prowl API into your applications.
For this script you need a Prowl account with an API. Create a free account at http://www.prowlapp.com
When logged in, click on the tab 'Api Keys', and copy the API key.

Python

The scripts needs 'python' to be installed. If not yet installed, you can do so by running:

sudo apt-get install python

from the terminal.
If it says that 'python' is already installed, you can continue further with this tutorial.

Energy switch

The scripts needs data from a switch like the Fibaro Wall Plug or a similar switch which can track the usage of a connected device.

Installation instructions

Create the script

Create a new file in the /home/pi/domoticz/scripts/python folder. Name the file 'daily_usage_prowl.py'. Copy the contents from the script on this page to the file, and save it.

The Python code for the script

import sys
import json
import urllib2
import re
import time
import datetime
import httplib, urllib
import sys
import subprocess
import datetime
import time
 
# Prowl settings
 
API = "YOUR_OWN_PROWL_API_KEY"
PRIO = "0"
Appl = "Domoticz"
Evnt = "Fibaro Wall Plug"
ElVerbr = "Verbruik " # Dutch for 'Usage'
 
# haal de juiste info uit Domoticz / retrieve the info from Domoticz
OUTPUT = subprocess.check_output("curl -s http://127.0.0.1:8084/json.htm\?type=devices\&rid=120 | jq -r .result[].CounterToday", shell=True)
 
# creeer een bestand met de waarde / create a file with the readings
name = '/volume1/@appstore/domoticz/scripts/python/verbruik.txt'  # Naam tekstbestand / Name of text file 
 
today = datetime.date.today()
 
try: # keep trying / probeer zo lang het goed gaat
    file = open(name,'a')   # create file / creeer bestand
    # write the readings to the file / schrijf de waarde naar het bestand
    file.write(ElVerbr + Evnt + ' op ' + str(today) + ' was ' + OUTPUT)
    # close the file / sluit het bestand (vergelijkbaar met het 'save' en daarna 'close' commando in word)
    #file.close()
except: # something went wrong / in het vorige blok is iets niet goed gegaan (error)
    print('Something went wrong! Can\'t tell what....')
    sys.exit(0) # quit if something goes wrong Python / stop als er iets mis is gegaan
 
# send a message with Prowl / stuur bericht naar prowl
urllib.urlopen("https://prowl.weks.net/publicapi/add?apikey=" + API + "&priority=" + PRIO + "&application=" + Appl + "&event=" + Evnt + "&description=" + ElVerbr + Evnt + ' op ' + str(today) + ' was ' + OUTPUT)


The script needs a few variables to be edited to your needs, see 'Prowl settings' and also change the path for the .txt file to your needs. See the line 'name = .....etc'. In this script, the Fibaro Wall Plug has the IDX (rid) 120, so change that if your device has another IDX (see the line 'OUTPUT = .....etc'

Edit the file and save the script.

Make script executable

In the terminal, go to /home/pi/domoticz/scripts/python
Execute the command chmod +x daily_usage_prowl.py

Let the script run at certain times (cron)

To let the script work correctly, it needs to be run automatically at certain times. I prefer it to run at 23:59 everyday. This can be done by using 'cron', the task scheduler on Linux.

On the terminal, run crontab -e (for Raspberry) or vi /etc/crontab for Synology and possibly others. In the screen that opens, add the line below

59      23      *       *       *       root    python /volume1/@appstore/domoticz/scripts/python/daily_usage_prowl.py

Exit the crontab editor by pressing CTRL-O (character 'O', not zero) and hitting Enter.

Test the script

With the script saved, having it made executable, we can now test it.
In the terminal, type: python daily_usage_prowl.py and press Enter. You should receive a message with the usage for that day until that moment. Check the .txt file on your device to see if there is a value + date written. It should read something like:

Verbruik Fibaro Wall Plug op 2015-05-30 was 0.620 kWh