Page 1 of 3

Playstation timer

Posted: Monday 16 May 2016 15:09
by mvzut
Hi all,

Just to let you know I created a custom counter that keeps track of how long the Playstation console in my house has been turned on. It uses two (virtual) Domoticz devices and a piece of LUA script.

First, I created a new device with the System Alive Checker. This uses pinging to check if the Playstation is turned on or off. It will probably also report "On" if the Playstation is in standby mode downloading something in the background, but that doesn't happen too often I think. Add the switch to the used switches under Setup>Devices and give it a name like "Playstation 4". From now on, Domoticz keeps track of when your game console is on and off.

Then, I created an incremental counter called "Playstation timer" using the Dummy hardware component (first add this component if you haven't already done so before, using Setup>Hardware). Then go to the Utility section, change the type of your new counter from Energy to Counter, and fill in the Value Quantity (e.g. "Gaming time") and Value Units ("Minutes"). Check the Device ID (idx) that is given to the new counter device. In my case, this was 344.

Finally, create a script in the domoticz/scripts/lua directory with a name like "script_time_playstation_update.lua" with the following contents:

Code: Select all

local timer_device_ID = 344

commandArray = {}

local TotalMinutesOn = otherdevices_svalues['Playstation timer']
if otherdevices['Playstation 4'] == 'On' then
  TotalMinutesOn = TotalMinutesOn + 1
end
commandArray['UpdateDevice'] = timer_device_ID .. '|0|' .. tostring(TotalMinutesOn)

return commandArray
That's it! Now you can do several things:
- Check remotely if your kids are on the game console when they should be doing their homework
- Program a notification if the console has been on for more than e.g. 2 hours that day
- Get weekly, monthly and yearly overviews of the usage per day
- Switch off the console if an agreed limit has been reached ;-) (although this is probably easy to bypass by your kids if you are using a simple power socket switch)

Note that this should work on any internet-connected device, so not only a Playstation but also XBox, Wii, laptop or even the TV. Come to think of it, you could even monitor how long a lamp or fan or any device that you control from Domoticz has been on!

Maybe this can be of use for others too.

UPDATE:
I edited the script a little bit, so that it updates the timer every minute, even when the value has not changed. This avoids sensor timeout indications in Domoticz.
If you don't get the script to work right away, here are a few tips:
- Restart Domoticz, I've sometimes seen that Domoticz cannot immediately "see" newly created devices from LUA and a restart solves this
- It could help to put

Code: Select all

commandArray['UpdateDevice'] = timer_device_ID .. '|0|0'
directly after the "commandArray = {}" line in your script. This will properly initialize the value of the timer to 0. Remove (or comment out) the line again after a few minutes.
- If you create the script using the built-in script editor, make sure you select the "time" option in the top right hand corner. If you leave it on the default "Device", the script will be executed whenever any device state changes, which can be at arbitrary moments and leads to incorrect timing (i.e. one count could be more or less than one minute)

Re: Playstation counter

Posted: Monday 16 May 2016 19:31
by Derik
MMM

I got this problem not yet with my kids...
Well with my Wife, TV watching....:-)

Thanks I give it a try..

Re: Playstation timer

Posted: Saturday 21 May 2016 0:09
by Derik
Thanks...

works...
ScreenShot254.jpg
ScreenShot254.jpg (16.99 KiB) Viewed 9684 times
And this blockley is not working...[ both counters not ]
ScreenShot256.jpg
ScreenShot256.jpg (25.12 KiB) Viewed 9680 times
Where do i wrong?

Re: Playstation timer

Posted: Saturday 21 May 2016 15:26
by ldjldj
Dear Mvzut,

Can't get this script working. It will not update the counter and keeps complaining .
I've doublechecked everything according to your setup and can't find the problem.
It looks like there is no value (in the beginning) and therefore the script is not able to count.

Could you please help?

Below a part of the log

2016-05-21 15:22:00.566 LUA: | Value of device Playstation timer was: nil Min
2016-05-21 15:22:00.566 LUA: nil
2016-05-21 15:22:00.567 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_playstation_update.lua: .../domoticz/scripts/lua/script_time_playstation_update.lua:22: attempt to perform arithmetic on local 'TotalMinutesOn' (a string value)

Playstation timer

Posted: Sunday 22 May 2016 10:32
by mvzut
ldjldj wrote:Dear Mvzut,

Can't get this script working. It will not update the counter and keeps complaining .
I've doublechecked everything according to your setup and can't find the problem.
It looks like there is no value (in the beginning) and therefore the script is not able to count.

Could you please help?

Below a part of the log

2016-05-21 15:22:00.566 LUA: | Value of device Playstation timer was: nil Min
2016-05-21 15:22:00.566 LUA: nil
2016-05-21 15:22:00.567 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_playstation_update.lua: .../domoticz/scripts/lua/script_time_playstation_update.lua:22: attempt to perform arithmetic on local 'TotalMinutesOn' (a string value)
Hmm, that's strange. If you create a new counter, it usually has the value zero directly after it is created. Did you change anything to my script except from a few print commands? I guess line 22 is where 1 is added to the counter?

You could try to change this:

Code: Select all

local TotalMinutesOn = tonumber (otherdevices_svalues['Playstation timer']])
Although strange that this is not necessary for me. You can also check the value of the "Playstation timer" device on the Blocky events page, it has a button to display all current device states.

EDIT:
Is the value on the Blocky events page by any chance 0;0? It could help to put

Code: Select all

commandArray['UpdateDevice'] = timer_device_ID .. '|0|0'
directly after the "commandArray = {}" line in your script. This will (re)set the value to 0. Remove (or comment out) the line again after a few minutes.

Re: Playstation timer

Posted: Sunday 22 May 2016 11:02
by lukcinek
Hi

Nice and usefull script but it doesn't work for me either :(

In log I have : Error: EventSystem: in Asus: [string "local timer_device_ID = 163 ..."]:7: attempt to perform arithmetic on local 'TotalMinutesOn' (a nil value)

Any idea what is wrong?

Best regards

Re: Playstation timer

Posted: Sunday 22 May 2016 11:09
by Derik
is pi the owner of the script?
or root?

Code: Select all

local timer_device_ID = 4559

commandArray = {}

if otherdevices['IP: Mie'] == 'On' then
  local TotalMinutesOn = otherdevices_svalues['Test Laptop 4559']
  TotalMinutesOn = TotalMinutesOn + 1
  commandArray['UpdateDevice'] = timer_device_ID .. '|0|' .. tostring(TotalMinutesOn)
end

return commandArray
Do you have the names of your devices Correct???

Re: Playstation timer

Posted: Sunday 22 May 2016 11:47
by mvzut
lukcinek wrote:Hi

Nice and usefull script but it doesn't work for me either :(

In log I have : Error: EventSystem: in Asus: [string "local timer_device_ID = 163 ..."]:7: attempt to perform arithmetic on local 'TotalMinutesOn' (a nil value)

Any idea what is wrong?

Best regards
Are you sure you got the device names right? A restart may help, I think I needed to do that once too. Apparently, sometimes you cannot address new devices from LUA directory after creating them, and a restart probably updates the table of available variables.

Re: Playstation timer

Posted: Sunday 22 May 2016 13:39
by ldjldj
Dear MvZut,

Thanks, it works now.

Your suggestion of an extra code line at the beginnein gof the script for an initial value of "0" did the trick. After wachting the counter updating correctly I've commented this line out. Still doens't really explain why the script starts working directly - without initial value - at your place and why I have to put a starting value to get it working.

Re: Playstation timer

Posted: Sunday 22 May 2016 14:09
by Derik
some one has a b;ockley wprking with this counter...
Set x after y minutes / day...

Playstation timer

Posted: Sunday 22 May 2016 15:37
by mvzut
ldjldj wrote:Dear MvZut,

Thanks, it works now.

Your suggestion of an extra code line at the beginnein gof the script for an initial value of "0" did the trick. After wachting the counter updating correctly I've commented this line out. Still doens't really explain why the script starts working directly - without initial value - at your place and why I have to put a starting value to get it working.
I must admit that I played around with setting values a lot in the beginning to get things working, so I may have done this without remembering or realizing it was an essential step. I will update my original post to reflect this.

By the way, I have made a change to my script in the meantime: I now write the new counter value every time, regardless if it has changed or not. Otherwise, Domoticz will occasionally start to color the counter red in the UI, to indicate it hasn't been updated for quite a while.

Playstation timer

Posted: Sunday 22 May 2016 15:48
by mvzut
Derik wrote:some one has a b;ockley wprking with this counter...
Set x after y minutes / day...
Haven't tried this yet, when I have time I will give it a go. I remember that there were more device/sensor types in the past that couldn't be read properly from Blockly. I had problems myself with a dB meter in my Netatmo weather station. So we could just be unlucky ...

By the way, you can also send notifications from LUA directly, I'm pretty sure that will work anyway.

Re: Playstation timer

Posted: Monday 23 May 2016 1:20
by lukcinek
Hi mvzut

Thx for answer.
After adding (commandArray['UpdateDevice'] = timer_device_ID .. '|0|0') under (commandArray = {}) everything works great.
Thank You!

Best regards

EDIT:
I write this a little bit to early :(
Script works but...
I don't know what it measures...
They are not minutes and seconds.
It measures something but I don't know what time unit is this :(

Any idea?

Playstation timer

Posted: Monday 23 May 2016 7:29
by mvzut
lukcinek wrote:Hi mvzut

Thx for answer.
After adding (commandArray['UpdateDevice'] = timer_device_ID .. '|0|0') under (commandArray = {}) everything works great.
Thank You!

Best regards

EDIT:
I write this a little bit to early :(
Script works but...
I don't know what it measures...
They are not minutes and seconds.
It measures something but I don't know what time unit is this :(

Any idea?
As far as I know, Domoticz executes time scripts every minute, so the timer should exactly count in minutes. Maybe you have accidentally made it into a device script? That type of script executes every time a device changes state, which is at unpredictable time intervals.
If you have created a separate .lua file in the /scripts/lua folder, the name should begin with "script_time_", NOT "script_device_". If you have created the script in the editor within Domoticz, make sure you only select "Time" in the top right hand corner.

Re: Playstation timer

Posted: Monday 23 May 2016 12:34
by lukcinek
Hi mvzut
Thank You for answer.
You have right. I change checkbox in the right corner to "time" and now everything works great :)

Once again THANK You

Best regards

Re: Playstation timer

Posted: Monday 23 May 2016 20:11
by mvzut
lukcinek wrote:Hi mvzut
Thank You for answer.
You have right. I change checkbox in the right corner to "time" and now everything works great :)

Once again THANK You

Best regards
You're welcome! I updated the original post to avoid that others run into the same issue.

Re: Playstation timer

Posted: Tuesday 20 September 2016 23:15
by ayasystems
Im using your idea to know how many time my device is on!

Great! Regards!

Re: Playstation timer

Posted: Wednesday 21 September 2016 7:07
by mvzut
ayasystems wrote:Im using your idea to know how many time my device is on!

Great! Regards!
Nice to hear! Indeed, it can also be used without the ping part to monitor the on/off statistics of any device in Domoticz.

Re: Playstation timer

Posted: Monday 03 October 2016 11:36
by Erwinvos72
Thnx for the nice script but i have 2 questions

- when i create a system alive checker i only see the xbox 360 'on' when i do a internet test. After the test the xbox goes back to off and the xbox is still on. Do you know why?

- I created the timer but nothing was recorded. I created the script with atom editor on a windows pc and after that i did a conversion to linux to get rid of dos endings etc. Now i added the line 'commandArray['UpdateDevice'] = timer_device_ID .. '|0|' .. tostring(TotalMinutesOn)' directly under 'commandArray = {}' and will try it 2night. But could i have made a mistake by do it on windows and convert it afterwards?

Re: Playstation timer

Posted: Monday 03 October 2016 16:36
by Derik
Should there be not a option to shutdown a PS with a api or something else?
The power socket solution will not work for my kids...[ there smart like there mom.. :-) ]