Hey Guys.
In thts Tutorial i will show you how to create a external logfile for monthly logging operational times for switches. It could be used for variables and states too.
In my example i log 2 devices - the kitchen light as switch-device and the tv as presence-device
In the first step we create a few variables:
the first one “$Stromverbrauch_tagfaktor” was for the price of the electricity consumption per Watt, 4.8239 cent as example
the second “$count_minutes_kitchenswitch” as value we set to zero, this will increasingly later by a rule.
the third “$count_hours_kitchenswitch” as expression is for converting the minutes in hours.
the fourth “$leistung_kitchenlight” was for the power consumption of the kitchen bright, as example 15 Watts
the fifth “$count_verbrauch_kitchenswitch” was the formula to calculate the consumption rate.
In the second step we create a rule for counting $count_minutes_kitchenswitch up every minute
If you want to show it in pimatic too, you can create 2 VariableDevices like this:
{
"id": "energieverbrauch_küchenlicht",
"class": "VariablesDevice",
"name": "Energieverbrauch Küchenlicht",
"variables": [
{
"name": "Betriebsstunden",
"type": "number",
"expression": "$count_hours_kitchenswitch",
"unit": "h"
},
{
"name": "Verbrauchspreis",
"type": "number",
"expression": "$count_verbrauch_kitchenswitch",
"unit": "€"
}
]
},
{
"id": "energieverbrauch_tv",
"class": "VariablesDevice",
"name": "Energieverbrauch TV",
"variables": [
{
"name": "Betriebsstunden",
"type": "number",
"expression": "$count_hours_tv_presence",
"unit": "h"
},
{
"name": "Verbrauchspreis",
"type": "number",
"expression": "$count_verbrauch_tv",
"unit": "€"
}
]
}
and that will look like this in the GUI
In the next step we will create a rule for putting the variables in an outputfile called “energycounter.log”, but before we have to create 2 shellscripts and VariableDevices which make it possible pimatic knows the end of the month.
connect to your pi via ssh and create the first shellscript for getting the last day of currently month:
pi@raspberrypi:~ $ sudo nano /home/pi/getlastdayofmonth.sh
insert
#!/bin/bash
date -d "-$(date +%d) days +1 month" +%d.%m.%Y
and make it executable with sudo chown +x /home/pi/getlastdayofmonth.sh
create a second shellscript for getting the actual date,
pi@raspberrypi:~ $ sudo nano /home/pi/getdate.sh
insert
#!/bin/bash
date +"%d.%m.%Y"
and make them also executable.
now create 2 VariableDevices like this in Pimatic:
{
"id": "date_lastdayofmonth",
"name": "Letzter Monatstag",
"class": "ShellSensor",
"attributeName": "lastdayofmonth",
"attributeType": "string",
"command": "/home/pi/getlastdayofmonth.sh",
"discrete": true,
"interval": 300000
},
{
"id": "date_today",
"name": "Aktuelles Datum",
"class": "ShellSensor",
"attributeName": "todaydate",
"attributeType": "string",
"command": "/home/pi/getdate.sh",
"discrete": true,
"interval": 300000
}
after a restart we should have 2 date-variables in pimatic.
now we can compare them together and triggering at the last of the month to get all logged counting-variables in the external logfile with following rule:
if
its 23:58 and $date_today.todaydate = $date_lastdayofmonth.lastdayofmonth
then
execute "sudo sed -i '\$ a Kitchenlight $date_today.todaydate $count_hours_kitchenswitch hours runtime' /home/pi/energycounter.log"
and execute "sudo sed -i '\$ a TV $date_today.todaydate $count_hours_tv_presence hours runtime' /home/pi/energycounter.log"
and execute "sudo sed -i '\$ a #################################' /home/pi/energycounter.log"
and a second rule to reset the counting-values to zero one minute later
if
its 23:59 and $date_today.todaydate = $date_lastdayofmonth.lastdayofmonth
then
set $count_minutes_kitchenswitch to "0" and set $count_minutes_tv_presence to "0"
now you will have a energycounter.log-file like this:
pi@raspberrypi:~ $ sudo nano energycounter.log
## 01.04.2016 // Begin Logfile
################################
Kitchenlight 31.04.2016 2.0833333333333335 hours runtime
TV 31.04.2016 10.966666666666667 hours runtime
#################################
Kitchenlight 31.05.2016 1.5 hours runtime
TV 31.05.2016 7.5 hours runtime
if you want to show the content in pimatic, you could install the filebrowser-plugin
{
"plugin": "filebrowser",
"mappings": [
{
"path": "/pi",
"directory": "/home/pi"
}
]
}
and use the iframe-plugin to create a device:
{
"class": "iframeDevice",
"id" : "energycounter",
"name": "Logfile",
"url": "http://user:password@IP-of-PI/pi/energycounter.log",
"width": 250,
"height": 700,
"border": 0,
"scrolling": "yes",
"scale": 1,
"reload": 120
}
if you’re finish it could looks like this
I hope it was not too complicated and apologize for my bad english
a special thanks goes to @leader21 for his support, without him I would still pull my hairs out of my head