Virtual device for usage last day/week/month

Forum over Homeseer scripts (DUTCH forum)

Moderators: TANE, Ruud

raymonvdm
Senior Member
Senior Member
Posts: 1153
Joined: Sun Dec 18, 2011 1:23 am

Virtual device for usage last day/week/month

Post by raymonvdm »

I would like to have some virtual devices wich give me the total power usage for last day/week/month but i don`t know how to get this.

I currently use and Aeon Labs Home Energy meter which is giving me actuale wattage every 60 seconds (also supports kwh) But how can i set a virtual device with the state of today minus the status of yesterday = Today`s usage
Running HS3PRO on PC with Z-Wave / OpenTherm / Plugwise / RFXcom / MQTT / XAP400 / Logitech Media Server and Squeezelite on PI`s
AshaiRey
Senior Member
Senior Member
Posts: 1310
Joined: Mon Feb 02, 2009 5:27 pm
Location: Netherlands
Contact:

Re: Virtual device for usage last day/week/month

Post by AshaiRey »

Have a look at vbscript command DateDiff
For example here http://www.w3schools.com/vbscript/func_datediff.asp

You need some kind of logging also because you want to know what the device value was yesterday
Bram
raymonvdm
Senior Member
Senior Member
Posts: 1153
Joined: Sun Dec 18, 2011 1:23 am

Re: Virtual device for usage last day/week/month

Post by raymonvdm »

I`m no script guru, but here is my idea

- Create a virtual device K94
- Create a Event
- Make the event set the current status of device K94 with the value of device Q3 every day at 23:59

- Make a virtual device K95
- Create an event which calculates K95 status by Q3 - K94 (Day Usage)

But what to do next .....
Running HS3PRO on PC with Z-Wave / OpenTherm / Plugwise / RFXcom / MQTT / XAP400 / Logitech Media Server and Squeezelite on PI`s
Sooty
Member
Member
Posts: 204
Joined: Sat Mar 22, 2008 11:29 pm
Location: United Kingdom

Re: Virtual device for usage last day/week/month

Post by Sooty »

There are a number of ways to achieve this, but the simplest is probably to create some virtual devices for day, week, month and year.
Create an event that triggers whenever the total kWh device changes.
Run a script from this event that calculates how much energy has been consumed since the last change. This step will require the last kWh reading to be available which will need to be stored somewhere. Perhaps in an ini file.
Once the consumed kWh has been calculated, then add this value to all of the virtual devices.
At 00:00:01 every day, run a script that resets the virtual devices to zero. Some logic will be required here to detect if it is the first day of the week, month or year etc so only the correct counters are reset.

I have something similar set up here for monitoring various min and max sensor values etc.

If you need some help with the scripting etc, then let me know.

Paul..
AshaiRey
Senior Member
Senior Member
Posts: 1310
Joined: Mon Feb 02, 2009 5:27 pm
Location: Netherlands
Contact:

Re: Virtual device for usage last day/week/month

Post by AshaiRey »

raymonvdm wrote:I`m no script guru, but here is my idea

- Create a virtual device K94
- Create a Event
- Make the event set the current status of device K94 with the value of device Q3 every day at 23:59

- Make a virtual device K95
- Create an event which calculates K95 status by Q3 - K94 (Day Usage)

But what to do next .....
I am either a script guru but i don't like to wait for answers all the time
No critism btw. I just hope to motivate you for some vb learning :D

I presume that Q3 is the actual total value.
K94 is the previous day total value
K95 is the daily usage

So at the end of the day make an event that does
Q3 - K94 -> put it in K95 and then
Put Q3 into K94

The event must do (not tested code, just from my mind. Check otherwise the online help files)
hs.SetDeviceString("K95", hs.DeviceValue("Q3") - hs.DeviceValue("K94") )

Next it should do
hs.SetDeviceString("K94", hs.SetDeviceString("Q3") )

You can put that in one script and run it as a event, put it in two events or in one event on one line seperated with a :
Bram
raymonvdm
Senior Member
Senior Member
Posts: 1153
Joined: Sun Dec 18, 2011 1:23 am

Re: Virtual device for usage last day/week/month

Post by raymonvdm »

I came up with the following

Events

Image


Devices

Image

p.s. I don`t know how to resize the images, the look small on topic

Maak_nachstand.vb

Code: Select all

Sub Main(parm as object)
hs.SetDeviceString("K90", hs.DeviceValue("Q3") )
End Sub
Maak_Dagverbruik.vb

Code: Select all

Sub Main(parm as object)
hs.SetDeviceString("K91", hs.DeviceValue("Q3") - hs.DeviceValue("K90") )
End Sub
It seems that the Maak_Nachstand.vb script is working. However the Maak_Dagverbruik script is not giving me the expected valua, i would expect a single or dual digit value but it looks subtract command is not working
Running HS3PRO on PC with Z-Wave / OpenTherm / Plugwise / RFXcom / MQTT / XAP400 / Logitech Media Server and Squeezelite on PI`s
AshaiRey
Senior Member
Senior Member
Posts: 1310
Joined: Mon Feb 02, 2009 5:27 pm
Location: Netherlands
Contact:

Re: Virtual device for usage last day/week/month

Post by AshaiRey »

Put it in numbers to see what's happening

example:
Q3 = 75

In Maak_nachstand.vb you do K90 = Q3 so now are
Q3 = 75
K90 = 75

In Maak_Dagverbruik.vb you do Q3 - K90
That is 75 - 75 = 0
And that is put into K90

Maak_Dagverbruik should be called first and then Maak_nachstand. You can put them i to one script like

Code: Select all

Sub Main(parm as object)
  hs.SetDeviceString("K91", hs.DeviceValue("Q3") - hs.DeviceValue("K90") )
  hs.SetDeviceString("K90", hs.DeviceValue("Q3") )
End Sub
btw, i am glad you tried
Bram
raymonvdm
Senior Member
Senior Member
Posts: 1153
Joined: Sun Dec 18, 2011 1:23 am

Re: Virtual device for usage last day/week/month

Post by raymonvdm »

When i use numbers the maak_dagverbruik is working but when i use device codes the dagvebruik is always set equel to q3, even if the nachtstand is lower then q3

Conlusion: Substract is not working, i also tried using dim command but i don't know how it should be used.
Running HS3PRO on PC with Z-Wave / OpenTherm / Plugwise / RFXcom / MQTT / XAP400 / Logitech Media Server and Squeezelite on PI`s
Sooty
Member
Member
Posts: 204
Joined: Sat Mar 22, 2008 11:29 pm
Location: United Kingdom

Re: Virtual device for usage last day/week/month

Post by Sooty »

Perhaps I don't fully understand your requirements here. From what I understand Q3 is the accumulated total kWh device which is incremented every time the device updates.

In your screenshot the device shows a status of 4788,37. hs.DeviceValue will return an integer value of 478837 for this device which will need to be divided by 100 to get the true kWh value.

To obtain usage for today, you need to know what value device Q3 had at 00:00:00. So if device Q3 had a value of 478837 at 00:00:00 and now it is 10:00:00 and it has a value of 478950, then the usage so far for today is 478950 - 478837 / 100 = 1.13.

Paul..
AshaiRey
Senior Member
Senior Member
Posts: 1310
Joined: Mon Feb 02, 2009 5:27 pm
Location: Netherlands
Contact:

Re: Virtual device for usage last day/week/month

Post by AshaiRey »

In vbscript you can use variables on the fly. Most of the programmers don't like this. They say if you use a variable then you must define them first and give them a type. This is done wit the DIM statement
DIM <variablename> AS <type>vb: Dim teller as integer

You can put the starting value right behind it like : Dim teller as integer = 22
(tip: for the different types google on vbscript variables)

Having said this. You would expect HS to allow instant variables but vbscript in HS runs in a wrapper and that puts the commant Option Explicit to it.
This command tells the script that all variables must be declared before they can be used.

So what next.
It could be that the value of the device isn't set but only the device string
Have you checked that the devices actually hold the value?
Click on device - Additional information tab - value

If the value is only in the string then you need to convert the string to a integer first
(Google for details : vbs convert string to integer) The W3school.com site usualy have simple examples.

Look up what the vbscript CInt and CStr does for you so you know what you will doing.

It could be like this if only the devicestring is used
hs.SetDeviceString("K91", CStr( hs.DeviceValue("Q3") - hs.DeviceValue("K90") ) )

Another PS. I hope you don't mind like this. It's better to have some basic knowledge about this so you will be able to solve more puzzles in the future. If you do mind you can PM about this and i will help you further offline. However i think other starters could benefit of this thread too.
Bram
Sooty
Member
Member
Posts: 204
Joined: Sat Mar 22, 2008 11:29 pm
Location: United Kingdom

Re: Virtual device for usage last day/week/month

Post by Sooty »

Not sure if the original poster was intending to learn some VB script or not, but I think a good understanding of the problem / solution is required.

This is my take on the solution.
In order to calculate accumulated values, you need to know what the previous reading was so that you can calculate by how much it has increased or decreased and then add or subtract this from the accumulated value.

At the end of each day you can then move values around. So today's accumulated value is moved to yesterday, this week is moved to last week etc..

There are a number of ways to deal with the "last value" in Homeseer but for simplicity, I think the best way would be to store this in an ini file so that it is preserved between re-starts.

So if we have the following HS devices, Today, This Week, This Month, This Year.
The script could be triggered on every device change of Q3, or done at say 15 minute intervals and the "change" value is simply added to each device.

Add some more devices: Yesterday, Last Week, Last Month, Last Year.
At 00:00:01 every day run a second script function that
moves today's value to yesterday
If it is the first day of the week, then
move this week's value to last week and reset this week counter to 0
If it is the first day of the month, then
move this month's value to last month and reset this month counter to 0
etc...

Paul..
AshaiRey
Senior Member
Senior Member
Posts: 1310
Joined: Mon Feb 02, 2009 5:27 pm
Location: Netherlands
Contact:

Re: Virtual device for usage last day/week/month

Post by AshaiRey »

Sooty wrote:There are a number of ways to deal with the "last value" in Homeseer but for simplicity, I think the best way would be to store this in an ini file so that it is preserved between re-starts.
That's not realy more simple then just store it in a virtual device since state is also kept automaticly during reboots
Bram
Sooty
Member
Member
Posts: 204
Joined: Sat Mar 22, 2008 11:29 pm
Location: United Kingdom

Re: Virtual device for usage last day/week/month

Post by Sooty »

You can indeed use a Homeseer device for this puprose and in this case it would be fine, however my personal preference would be to use an ini file. My reason for this is that I do use quite a few calculated values within my Homeseer system like min / max values (with dates / times) plus trends etc of various sensor values and this would require the use of a lot of virtual devices.

I also don't feel that HS devices are ideal for storing multiple values. One of my calculated values is the average of the last 25 readings of a particular sensor with date/time stamps. With an ini file I can simply store all the information in a single entry and then have the result displayed in a single virtual device. With an HS device I would need to store all the information either in the device string or use 25 virtual devices.

Paul..
raymonvdm
Senior Member
Senior Member
Posts: 1153
Joined: Sun Dec 18, 2011 1:23 am

Re: Virtual device for usage last day/week/month

Post by raymonvdm »

It`s nice to see that you want me to learn VB but i don`t have enough time for it (it`s not going fast enough for me)

For now i`m using to additional virtual devices which i can also use in HStouch. I tried the following script to see if it works but it does not (note: this is my first VB spin-off)

Code: Select all

Sub Main(parm as object)
Dim A As String
Dim B As String
Dim C As String
A = hs.DeviceValue("Q3")
B = hs.DeviceValue("K90")
C = A - B
hs.SetDeviceString("K91", C )
End Sub
I also tested this one

Code: Select all

hs.SetDeviceString("K91", CStr( hs.DeviceValue("Q3") - hs.DeviceValue("K90") ) )
But same result K91 is set equal to Q3 so substraction is not working


Victory :-)

The following is working

Code: Select all

Sub Main(parm as object)
hs.SetDeviceString("K91", CStr( hs.DeviceValue("Q3") - hs.DeviceString("K90") ) )
End Sub
Next up divide by 100(0) to get kW

Code: Select all

Sub Main(parm as object)
hs.SetDeviceString("K91", CStr( hs.DeviceValue("Q3") - hs.DeviceString("K90") ) /100 )
End Sub
SideStep

I now understand why twittseer is sending strange tweets about the room setpoint of my OTG. It is sending a DV (DeviceValue)

Code: Select all

Tweet: Setpoint changed to EXPR(DV(O46)) °C 
But the setpoint is 20,50 so i should make it

Code: Select all

Tweet: Setpoint changed to EXPR(DS(O46)) °C
but then it complains about the ,

Code: Select all

Reference ID	2457
Status	17 = Unknown
Value	20  
String	 20,50 °C
Running HS3PRO on PC with Z-Wave / OpenTherm / Plugwise / RFXcom / MQTT / XAP400 / Logitech Media Server and Squeezelite on PI`s
AshaiRey
Senior Member
Senior Member
Posts: 1310
Joined: Mon Feb 02, 2009 5:27 pm
Location: Netherlands
Contact:

Re: Virtual device for usage last day/week/month

Post by AshaiRey »

raymonvdm wrote:It`s nice to see that you want me to learn VB but i don`t have enough time for it (it`s not going fast enough for me)
I can understand that.
I see if i can speed things up for you and give you insight in what is going on
For now i`m using to additional virtual devices which i can also use in HStouch. I tried the following script to see if it works but it does not (note: this is my first VB spin-off)

Code: Select all

Sub Main(parm as object)
Dim A As String
Dim B As String
Dim C As String
A = hs.DeviceValue("Q3")
B = hs.DeviceValue("K90")
C = A - B
hs.SetDeviceString("K91", C )
End Sub
You Do Dim A As String
The variable A can only hold a string which is said in a general way only letters and numbers (not values). The written things so to say

A = hs.deviceValue("Q3") Gets the value of Q3 and put that into A. But.. hs.devicevalue returns a variable of the type Long (that's an oversized integer) and not a string.
You have to convert that to a string like this A = CStr( hs.deviceValue("Q3") )


Here i am looking proud.. 8)
Victory :-)

The following is working

Code: Select all

Sub Main(parm as object)
hs.SetDeviceString("K91", CStr( hs.DeviceValue("Q3") - hs.DeviceString("K90") ) )
End Sub
Seeing this i think that K90 didn't have a value in it's value field but only in the string field ( the visible thing of the device)

Your first program adaption.
Your struggle wasn't for nothing.
Next up divide by 100(0) to get kW

Code: Select all

Sub Main(parm as object)
hs.SetDeviceString("K91", CStr( hs.DeviceValue("Q3") - hs.DeviceString("K90") ) /100 )
End Sub

About this part
I now understand why twittseer is sending strange tweets about the room setpoint of my OTG. It is sending a DV (DeviceValue)

Code: Select all

Tweet: Setpoint changed to EXPR(DV(O46)) °C 
But the setpoint is 20,50 so i should make it

Code: Select all

Tweet: Setpoint changed to EXPR(DS(O46)) °C
but then it complains about the ,

Code: Select all

Reference ID	2457
Status	17 = Unknown
Value	20  
String	 20,50 °C
Oke, as you know now a devicevalue is a Long, that's a extended integer. An intger can only hold whole numbers, no fraction. That is why i pointed you also to the pages about variables and integer. Nevermind.

As Sooty mentioned earlier
In your screenshot the device shows a status of 4788,37. hs.DeviceValue will return an integer value of 478837 for this device which will need to be divided by 100 to get the true kWh value.
If you want put 20,50 as value in devicevalue you have to multiply that by 100 so it becomes a whole number. If any event trigger on this devicevalue then take this into account.
Bram
Post Reply

Return to “Homeseer Scripts Forum”