Playstation timer

In this subforum you can show projects you have made, or you are busy with. Please create your own topic.
mvzut
Posts: 429
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Playstation timer

Post by mvzut » Monday 16 May 2016 15:09

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)
Last edited by mvzut on Thursday 26 May 2016 21:07, edited 24 times in total.
Raspberry Pi 3 - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Xiaomi Gateway - Sonos - IP cameras - Wall mounted tablet + Imperihome - MANY switches/sensors

User avatar
Derik
Posts: 2185
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Playstation counter

Post by Derik » Monday 16 May 2016 19:31

MMM

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

Thanks I give it a try..
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

User avatar
Derik
Posts: 2185
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Playstation timer

Post by Derik » Saturday 21 May 2016 0:09

Thanks...

works...
ScreenShot254.jpg
ScreenShot254.jpg (16.99 KiB) Viewed 4697 times
And this blockley is not working...[ both counters not ]
ScreenShot256.jpg
ScreenShot256.jpg (25.12 KiB) Viewed 4693 times
Where do i wrong?
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

ldjldj
Posts: 6
Joined: Friday 16 January 2015 21:39
Target OS: Raspberry Pi
Domoticz version: 3.5113
Location: Near Utrecht
Contact:

Re: Playstation timer

Post by ldjldj » Saturday 21 May 2016 15:26

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)

mvzut
Posts: 429
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Playstation timer

Post by mvzut » Sunday 22 May 2016 10:32

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.
Last edited by mvzut on Sunday 22 May 2016 10:32, edited 3 times in total.
Raspberry Pi 3 - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Xiaomi Gateway - Sonos - IP cameras - Wall mounted tablet + Imperihome - MANY switches/sensors

lukcinek
Posts: 45
Joined: Sunday 20 March 2016 0:28
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Playstation timer

Post by lukcinek » Sunday 22 May 2016 11:02

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

User avatar
Derik
Posts: 2185
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Playstation timer

Post by Derik » Sunday 22 May 2016 11:09

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???
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

mvzut
Posts: 429
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Playstation timer

Post by mvzut » Sunday 22 May 2016 11:47

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.
Raspberry Pi 3 - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Xiaomi Gateway - Sonos - IP cameras - Wall mounted tablet + Imperihome - MANY switches/sensors

ldjldj
Posts: 6
Joined: Friday 16 January 2015 21:39
Target OS: Raspberry Pi
Domoticz version: 3.5113
Location: Near Utrecht
Contact:

Re: Playstation timer

Post by ldjldj » Sunday 22 May 2016 13:39

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.

User avatar
Derik
Posts: 2185
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Playstation timer

Post by Derik » Sunday 22 May 2016 14:09

some one has a b;ockley wprking with this counter...
Set x after y minutes / day...
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

mvzut
Posts: 429
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Playstation timer

Post by mvzut » Sunday 22 May 2016 15:37

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.
Last edited by mvzut on Sunday 22 May 2016 15:37, edited 2 times in total.
Raspberry Pi 3 - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Xiaomi Gateway - Sonos - IP cameras - Wall mounted tablet + Imperihome - MANY switches/sensors

mvzut
Posts: 429
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Playstation timer

Post by mvzut » Sunday 22 May 2016 15:48

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.
Raspberry Pi 3 - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Xiaomi Gateway - Sonos - IP cameras - Wall mounted tablet + Imperihome - MANY switches/sensors

lukcinek
Posts: 45
Joined: Sunday 20 March 2016 0:28
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Playstation timer

Post by lukcinek » Monday 23 May 2016 1:20

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?

mvzut
Posts: 429
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Playstation timer

Post by mvzut » Monday 23 May 2016 7:29

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.
Last edited by mvzut on Thursday 26 May 2016 21:08, edited 1 time in total.
Raspberry Pi 3 - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Xiaomi Gateway - Sonos - IP cameras - Wall mounted tablet + Imperihome - MANY switches/sensors

lukcinek
Posts: 45
Joined: Sunday 20 March 2016 0:28
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Playstation timer

Post by lukcinek » Monday 23 May 2016 12:34

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

mvzut
Posts: 429
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Playstation timer

Post by mvzut » Monday 23 May 2016 20:11

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.
Raspberry Pi 3 - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Xiaomi Gateway - Sonos - IP cameras - Wall mounted tablet + Imperihome - MANY switches/sensors

ayasystems
Posts: 33
Joined: Tuesday 19 April 2016 23:37
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Playstation timer

Post by ayasystems » Tuesday 20 September 2016 23:15

Im using your idea to know how many time my device is on!

Great! Regards!

mvzut
Posts: 429
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Playstation timer

Post by mvzut » Wednesday 21 September 2016 7:07

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.
Raspberry Pi 3 - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Xiaomi Gateway - Sonos - IP cameras - Wall mounted tablet + Imperihome - MANY switches/sensors

Erwinvos72
Posts: 61
Joined: Thursday 25 February 2016 17:30
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Playstation timer

Post by Erwinvos72 » Monday 03 October 2016 11:36

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?

User avatar
Derik
Posts: 2185
Joined: Friday 18 October 2013 23:33
Target OS: Raspberry Pi
Domoticz version: BETA
Location: Arnhem/Nijmegen Nederland
Contact:

Re: Playstation timer

Post by Derik » Monday 03 October 2016 16:36

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.. :-) ]
Xu4: Beta Extreme antenna RFXcomE,WU Fi Ping ip P1 Gen5 PVOutput Harmony HUE SolarmanPv OTG Winddelen Alive ESP Buienradar MySensors WOL Winddelen counting RPi: Beta SMAspot RFlinkTest Domoticz ...Different backups

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests