
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 &
Code: Select all
#!/bin/bash
ps4-waker standby &