Switch for Playstation 4

Just wanted to show you, how to switch your ps4 on/off with ha & ps4-waker.
For the switch itself I use the Command-Line Switch to trigger the shell commands of ps4-waker.

- platform: command_line
  switches:
    playstation4:
      command_on: 'ps4-waker'
      command_off: 'ps4-waker standby'
      command_state: 'telnet playstation4 9295 | grep "Connected" > nul && (echo "true") || (echo "false")' 
      value_template: '{{ value }}'
      friendly_name: "Playstation 4"

Greets Jacko

8 Likes

Will this work for the PS3 as well?

1 Like

I donā€™t think so, because ps4-waker acts like a Playstation App and the App only supports the PS4 with itā€™s start & sleep features.

no worries, will try a WOL for my PS3

Thanks for sharing. Iā€™m having a bit of difficulty controlling the ps4 through HA. Iā€™ve installed ps4-waker on my raspberry pi and I am able to wake and sleep the ps4 from the command line. However in HA it is always showing as off? I ran the command_state code in the command line and it does return ā€œtrueā€ if the ps4 is on but it takes around 15 seconds to complete, is this normal?

yes thats normal.
And sorry itā€™s not a binary switch itā€™s a command-line switch.

Greets
Jacko

Hi thank you for sharing! I am trying to set it up aswell.
I have now a switch that i can use to turn it on. But it goes back to off after a few seconds.
So i canā€™t use it to turn off the ps4.

This command should get the state if i understand well.

command_state: ā€˜telnet playstation4 9295 | grep ā€œConnectedā€ > nul && (echo ā€œtrueā€) || (echo ā€œfalseā€)ā€™

When i run this command on the pi it wil return true if ps4 is on. But when i run it in the virtual environment (where i installed hass) i get a message: permission denied and it will return false.

Do you know how i can fix this?

Is your virtual Environment a linux system or what?
then try it with a sudo in front of the command.

Yep. My system is RPI 2 with Raspbian jessie and hass installed using the all in one installer. But when i do that with the sudo in the virtual enviroment is asks for a password. But there is no password for hass user in the virtual enviroment.

I know itā€™s a dumb question, but is telnet even installed?
Because on some rpiā€™s I had to install it first.

Yes i did install it like this:

pi@raspberrypi:~ $ sudo apt-get install telnet

And in the ā€œpiā€ promt it is working it shows this (ps4 is on):

pi@raspberrypi:~ $ telnet 192.168.1.12 9295 | grep "Connected" > nul && (echo "true") || (echo "false")
Connection closed by foreign host.
true

And this is when i run it in the hass virtual enviroment:

(hass_venv) hass@raspberrypi:/home/pi$ telnet 192.168.1.12 9295 | grep "Connected" > nul && (echo "true") || (echo "false")
bash: nul: Permission denied
false

So it looks,like it needs permission, but i canā€™t give it with the sudo command because then i have to enter a password.

I got mine to work by changing the command state to use nc instead of telnet, which in my case was much quicker at responding. The value template is not needed because nc will return 0 on success, 1 on failure.

Hereā€™s a snippet of my switch definition:

- platform: command_line
  switches:
    playstation4:
      command_on: 'sudo ps4-waker'
      command_off: 'sudo ps4-waker standby'
      command_state: 'nc -z -w 5 192.168.1.11 9295'

The -z parameter tells netcat to scan for the open port, -w 5 tells netcat to timeout after 5 seconds otherwise it will wait endlessly. If testing in the command line is needed add -v for a more verbose output.

I just got the command_state to execute by changing nul to /dev/nul when i saw your post.

command_state: telnet 192.168.1.12 9295 | grep "Connected" > /dev/null && (echo "true") || (echo "false")
But i was messing arround with the value template but didnā€™t get it to work unfortunately.

So i did what you said and it is now working perfectly! thank you!
I installed netcat by:

sudo apt-get install netcat

This is what my config looks like:

- platform: command_line
  switches:
     playstation4:
       command_on: 'ps4-waker -d 192.168.1.12 -c /home/pi/Documents/.ps4-wake.credentials.json'
       command_off: 'ps4-waker standby -d 192.168.1.12 -c /home/pi/Documents/.ps4-wake.credentials.json'
       command_state: 'nc -z -w 5 192.168.1.12 9295'
       friendly_name: "Playstation 4"

I used the -c parameter to specify where the credentials are stored. Because it was originally stored at a location where from it could not be accessed from the virtual environment. So i copied it to my documents folder.

Maybe this can be some help for someone who is also trying to get this to work.

2 Likes

Do we still need the PS app, or does this method skip it?

You need the ps app one time to get your credentials file.

Thatā€™s awesome! Iā€™m gonna try this out myself!

Awesome. Iā€™ve got it all up and running. :slight_smile:

Hi,
I tried to install ps4-waker but i receive a strange error when I try to execute:

pi@raspberrypi:$ ps4-waker
                                                                                                                          
/usr/local/lib/node_modules/ps4-waker/bin/cmd.js:107                                                                      
    var keyNames = argv._.slice(remote).map((key) => key.toUpperCase());                                                  
                                                   ^
SyntaxError: Unexpected token >                                                                                           
    at Module._compile (module.js:439:25)                                                                                 
    at Object.Module._extensions..js (module.js:474:10)                                                                   
    at Module.load (module.js:356:32)                                                                                     
    at Function.Module._load (module.js:312:12)                                                                           
    at Function.Module.runMain (module.js:497:10)                                                                         
    at startup (node.js:119:16)                                                                                           
    at node.js:906:3   

Whatā€™s the problem?

Thanks

Hi,

I work throw this thread a couple of Days ago and had a similar Problem.
I had to update nodejs to Version 7.

Run these Commands to update nodejs to latest stable Version:

sudo npm cache clean -f
sudo npm install -g n
sudo n stable

BR

Lukas

3 Likes

Yeah man!! It works!! Thanks so much!! :slight_smile: