PS4 Monitoring  [SOLVED]

Moderator: leecollings

Post Reply
BOverdevest
Posts: 23
Joined: Wednesday 28 March 2018 20:56
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.11307
Location: Nederland
Contact:

PS4 Monitoring  [SOLVED]

Post by BOverdevest »

To monitor a certain family member :ugeek: using a PS4 , I had a long wish to do this via Domoticz.
1) Log the status of the device
2) Measure how much time it is on
3) Do a remote shut down

I started with a Coolcam Plug, perfect for monitoring the power and switching it off.
Not so good for family relations; the PS4 does not like to lose power. :?

So researched a software solution:
For Linux PS4-waker is available: https://github.com/dhleong/ps4-waker
I combined this with ping to check first if the machine can be reached.

Step 1 is installing PS4-waker and making sure it work on the CLI (Command Line Interface
> PS4-waker check should give a JSON in return

I set up two bash scripts, placed in ~/domoticz/scripts (see below)
These are used to start and stop (stand by) the PS4, via PS4-waker

There is a dummy Selector Switch with 4 states (0=OFF, 10=Standby, 20=On, 30=Gaming)
The level 0 and 10 have "script://stopps4.sh" as action
The level 20 and 30 have "script://startps4.sh" as action
So using this the playstation can be started/ put in stand by.

Also, I made an Alert device. This one also holds the status of the PS4 as per above (including changing the color)
When it is in gaming mode, the game being played will be logged as well in the alert text as well.

And of course a timer. I have not been able to get the counter to work on 'time' itself, so i simply count the minutes.
There are two timers; 1 is incremented by 1 every minute, the other has the GameTIme.counterToday pushed in.
As I reset the timer back to zero at the end of the day, all data is lost --> needs to be improved to have just 1 timer....

Further improvement:
- Making the counter work, with a single counter based on time. Any examples?
- decide between using Alert device or selector switch, any opinions?
- notification to the parent when the game time goes beyond acceptable levels
- Expand the selector switch with actual games. (these could be started via ps4-waker as well)
- biggest wish: move the whole thing to a python plug in...

other ideas? improvements?

Code: Select all

local TimerSetting = "every minute" local n=1
-- local TimerSetting = "every 10 minutes" local n=10

return {

      on = {timer = {TimerSetting}},
      
      	logging = { level = domoticz.LOG_INFO
                ,marker = "PS4"},

	execute = function(domoticz, trigger)
        local PS4Switch      = domoticz.devices("PS4 Selector Switch") -- To see status and change status
        local PS4Alert       = domoticz.devices("PS4 Status")  -- General / Alert
        local GameTime       = domoticz.devices("PS4 Uptime") -- General /Incremental Counter
        local GameTimeToday  = domoticz.devices("PS4 Uptime Today") -- RFXMeter/RFXMeter counter
        local PS4StatusOld   = PS4Switch.state

        local grey          = domoticz.ALERTLEVEL_GREY
        local green         = domoticz.ALERTLEVEL_GREEN
        local yellow        = domoticz.ALERTLEVEL_YELLOW

        local json = require "JSON"
        local level = 0
        
        local handle2 = assert(io.popen("ping -c 1 192.168.1.241 | grep -q 'Destination Host Unreachable' && echo 'Off Line'"))
        local PS4status = handle2:read("*all")
        handle2:close()
     
        if string.sub(PS4status,1,8) == "Off Line" then
            if PS4Switch.level ~= level then PS4Switch.switchSelector(0).silent() end
            if GameTime.counter ~= 0 then GameTime.updateCounter(0) end
            if PS4Alert.text ~= "Off" then PS4Alert.updateAlertSensor(grey, "Off") end
                        
        else -- Playstation should be reachable. Time to use ps4-waker
            local handle = assert(io.popen("ps4-waker check"))
                local PS4status = string.gsub(handle:read("*all"), "-", "") -- remove the "-"
            handle:close()   
           jsonPS4status = json:decode(PS4status)            -- Turn JSON into LUA table
        
            if jsonPS4status.status == "Standby" then
                print("Standby")
                level = 10
                if PS4Switch.level ~= level then PS4Switch.switchSelector(level).silent() end-- Set selector to Standby
                if GameTime.counter ~= 0 then GameTime.updateCounter(0) end 
                if PS4Alert.text ~= "Standby" then PS4Alert.updateAlertSensor(grey, "Standby") end
                
            elseif jsonPS4status.runningappname then
                level = 30
                 if PS4Switch.level ~= level then PS4Switch.switchSelector(level).silent() end
                GameTime.incrementCounter(n) 
                GameTimeToday.updateCounter( GameTime.counterToday)
                local Game = jsonPS4status.runningappname
                if PS4Alert.text ~= Game then PS4Alert.updateAlertSensor(green, Game) end
                
            else -- jsonPS4status.status == "OK" then
                level = 20
                 if PS4Switch.level ~= level then PS4Switch.switchSelector(level).silent() end
                GameTime.incrementCounter(n) 
                GameTimeToday.updateCounter( GameTime.counterToday)
                PS4Alert.updateAlertSensor(yellow, "No Game")
            end
        end    
	end
}


Below two shell scripts. Put these in ~/domoticz/scripts
The '&' is important. it makes the line run in a separate background process and Domoticz will not wait for it to finish.

Code: Select all

#!/bin/sh
ps4-waker --skip-login &
Don't forget to do a "sudo chmod +x startps4.sh"

Code: Select all

#!/bin/bash
ps4-waker standby &
Don't forget to do a "sudo chmod +x stopps4.sh"
Schermafbeelding 2019-10-24 om 00.28.01.png
Schermafbeelding 2019-10-24 om 00.28.01.png (309.59 KiB) Viewed 3398 times
Schermafbeelding 2019-10-24 om 00.22.35.png
Schermafbeelding 2019-10-24 om 00.22.35.png (115.07 KiB) Viewed 3398 times
Schermafbeelding 2019-10-24 om 00.22.26.png
Schermafbeelding 2019-10-24 om 00.22.26.png (86.84 KiB) Viewed 3398 times
HUE, Tradfri, Zwave, RFXCOM, rooted TOON, PS4
Tiempie
Posts: 2
Joined: Wednesday 11 March 2020 12:29
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: PS4 Monitoring

Post by Tiempie »

seems to me that with the use of ps4-waker you don't need the Coolcam Plug?
Larsoss
Posts: 65
Joined: Friday 18 March 2016 10:11
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Netherlands
Contact:

Re: PS4 Monitoring

Post by Larsoss »

I set up two bash scripts, placed in ~/domoticz/scripts (see below)
These are used to start and stop (stand by) the PS4, via PS4-waker

And how do you name it?
Raspberry 4 - USB boot Domoticz /|\ Raspberry 2B - Dashticz /|\ Tasmota device's /|\ Philips Hue & Yeelight
rononline
Posts: 2
Joined: Friday 21 August 2020 16:12
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Rotterdam
Contact:

Re: PS4 Monitoring

Post by rononline »

I just installed domoticz, and i can't figure out how to use the lua script.

Can you help me in dummy steps? :-D
rononline
Posts: 2
Joined: Friday 21 August 2020 16:12
Target OS: Raspberry Pi / ODroid
Domoticz version: 2020.2
Location: Rotterdam
Contact:

Re: PS4 Monitoring

Post by rononline »

I put the script in /home/pi/domoticz/scripts/lua/script_device_PS4.lua, and created a Dummy hardware called PS4, and then the virtual sensors:
PS4 Uptime Today, PS4 Uptime, PS4 Status and PS4 Selector Switch. But that is not working.

And in the logging i get:
2020-08-21 16:59:08.276 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_PS4.lua: /home/pi/domoticz/scripts/lua/script_device_PS4.lua:8: attempt to index a nil value (global 'domoticz')
2020-08-21 16:59:08.310 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_PS4.lua: /home/pi/domoticz/scripts/lua/script_device_PS4.lua:8: attempt to index a nil value (global 'domoticz')
2020-08-21 16:59:08.337 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_PS4.lua: /home/pi/domoticz/scripts/lua/script_device_PS4.lua:8: attempt to index a nil value (global 'domoticz')
2020-08-21 16:59:08.375 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_PS4.lua: /home/pi/domoticz/scripts/lua/script_device_PS4.lua:8: attempt to index a nil value (global 'domoticz')
2020-08-21 16:59:08.396 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_PS4.lua: /home/pi/domoticz/scripts/lua/script_device_PS4.lua:8: attempt to index a nil value (global 'domoticz')
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PS4 Monitoring

Post by waaren »

rononline wrote: Friday 21 August 2020 17:00 I put the script in /home/pi/domoticz/scripts/lua/script_device_PS4.lua, and created a Dummy hardware called PS4, and then the virtual sensors:
PS4 Uptime Today, PS4 Uptime, PS4 Status and PS4 Selector Switch. But that is not working.

And in the logging i get:
2020-08-21 16:59:08.276 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_PS4.lua: /home/pi/domoticz/scripts/lua/script_device_PS4.lua:8: attempt to index a nil value (global 'domoticz')
You saved it as a Lua script but should save it as a dzVents script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pkessels
Posts: 5
Joined: Wednesday 22 February 2017 20:39
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: PS4 Monitoring

Post by pkessels »

I configured domoticz with the script and so on, but now i receive every time the following error:
2020-11-15 14:41:18.111 Error: dzVents: Error: (3.0.16) PS4: ------ Finished script_PS4_Status.lua after >18 seconds. (using 0.570 seconds CPU time !)
2020-11-15 14:42:10.123 Error: EventSystem: Warning!, lua script /home/pi/domoticz/dzVents/runtime/dzVents.lua has been running for more than 10 seconds
2020-11-15 14:42:18.147 Error: dzVents: Error: (3.0.16) PS4: An error occurred when calling event handler script_PS4_Status
2020-11-15 14:42:18.147 Error: dzVents: Error: (3.0.16) PS4: ...i/domoticz/scripts/dzVents/scripts/script_PS4_Status.lua:40: attempt to index a nil value (global 'jsonPS4status')

Does anybody have a clue?
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: PS4 Monitoring

Post by waaren »

pkessels wrote: Sunday 15 November 2020 14:48 Does anybody have a clue?
There are two places in the script where it needs to wait for an OS call (that could take forever) to return control.
I will leave it to the OP to correct it but using io.popen without putting the OS command in the background or force it to return control within a reasonable time leaves room for improvement...
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest