Switch for Playstation 4

I have two issues:

  1. When I try to use Hue emulation to turn my PS4 off using Alexa, I get this error and the PS4 does not turn off. However, I am able to turn it off using just a command line.

    17-02-13 20:47:29 ERROR (Thread-5) [homeassistant.components.switch.command_line] Command failed: sudo ps4-waker standby -d 192.168.1.189

  2. When I try to start a specific app using command line, I get the following:
    Unable to connect to PS4 at 192.168.1.189 { [Error: connect ECONNREFUSED 192.168.1.189:997]
    code: ‘ECONNREFUSED’,
    errno: ‘ECONNREFUSED’,
    syscall: ‘connect’,
    address: ‘192.168.1.189’,
    port: 997 }

Here is the section from my configuration.yaml:

  - platform: command_line
    switches:
      playstation4:
        command_on: 'sudo ps4-waker -d 192.168.1.189'
        command_off: 'sudo ps4-waker standby -d 192.168.1.189'
        command_state: 'nc -z -w 5 192.168.1.189 9295'
        friendly_name: "PlayStation 4"

Any help would be greatly appreciated!

Hey when you try to start an specif app do you specify the location of your credentials.json in your command?

ps4-waker -c /path/to/.ps4-wake.credentials.json -d 192.168.1.21 start CUSA01163

I have tried that with the same result.

I think you have hass installed in an virtualenviroment?
If you go to the hass user (in putty) and try the command without the “sudo” part. check if that works.
if it still asks for a root password you can edit the sudoers file so i doesn’t ask anymore for a root password.
But this is not a secure way, but for home use like i do, i don’t find it a big security risk.

For changing the hass user so it doens’t require a password to launch things take a look at this post

Are you saying that specifically for controlling any of those functions from HA? Because I can do off and on from command line. I cannot launch a specific app from anywhere.

Yes i think this part:

means that home-assistant cannot execute your command. (so i think when you try to run that command within the virtual environment it will not execute but asks for a password.) with running the command from virtual environment i mean:

first running this to get in virtual environment (this commands differ on each installation method, so please tell me how did you install HA.)
sudo su -s /bin/bash hass
source /srv/hass/hass_venv/bin/activate

and then running:
ps4-waker standby -d 192.168.1.189

I know before going to the virtual environment the command will work just fine. but the virtual environment is the place where hass tries to execute your command.

So if i am right at this moment you can not turn your ps4 on or off with the toggle button in HA. And you are only able to use the commandline before entering the virtual environment and not within it?

On this iteration of my pi, I did the install manually following the instructions. Definitely set up a virtual environment. On a different SD card, I used the AIO and had the same issues.

If I run from the virtual environment using those commands (not sure if that’s the correct terminology, this is all new for me), I can get it to turn on the PS4 if I don’t try to start an app. Once the PS4 is on, I can run the command to start the app.

Is that expected? I have to turn it on first and then turn on the app?

Either way, I cannot use Home Assistant to turn the PS4 on or off. The state also isn’t showing correctly.

Ok this is indeed expected behaviour. (but once you got ik working you can alsway setup an automation/script with something like --> if ps4 is off and i try to launch a app that first the ps4 is started and after x seconds the app is called.)
That’s something to work on once you get it working :slight_smile:

And for the state you did install nc with the command?:
sudo apt-get install netcat

And how did you install waker? i did it like this (with -g parameter):
npm install ps4-waker -g

and my version of nodejs is:
nodejs -v
v7.5.0

at first i had an older version and it wans’t working. so you might check your nodejs version.

It’s working! I can control it from the toggle and using Alexa! I’m honestly not sure what I did differently. I swapped back to my old SD card where it wasn’t working before and now it is just working. Didn’t change anything about it…

Now to get it to auto-start an app… :slight_smile:

Haha nice to hear you got it set up. I found it hard to get it to work myself too.

Now i have also an image of the game which is currently being played. :slight_smile:

3 Likes

Damn, Mister-Espria. You’re setup looks awesome! Hopefully this will be made into a component sometime because some things in this thread are just way over my head. It looks like I’ll need to install nodejs, npm, and netcat before I install ps4-waker? Is that right? Then you had to do something with credentials to make sure that ps4-waker would work while running Home Assistant in a virtual environment?

Even I like the idea of making this a platform but it’s somewhat unlikely until someone wants to convert ps4-waker to Python3.

Can you please tell us how you display the image of the current game? It looks pretty amazing.

@shivan Thank you! I also hope this will be a component some day. then it would be much easier to set up.
But if you want to use it now. the hardest part is to get a good version of nodejs and npm installed. With this i was struggling the most.

To Install npm and nodejs
I checked what commands i used for it so maybe it is not all needed but this is what i tried:

sudo npm install -g n
sudo n stable
sudo npm stable
sudo install npm stable
sudo install nodejs stable
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -
sudo apt-get install -y nodejs 

After installing it you can check which version you have
by running these commands:
node -v
nodejs -v
npm -v

The version i have are:

node -v
v5.7.0
nodejs -v
v7.5.0
npm -v
3.6.0

Installing netcat (this is to determine if your PS4 is on or off)
sudo apt-get install netcat

So if you have this you should be good to go to install ps4-waker.

Installing ps4-waker
npm install ps4-waker -g

Then you can start it with the command: (make sure your ps4 is turned on)
sudo ps4-waker

The first time it start it will ask you to find a pin code on your ps4 and enter it. this is to make your credentials file.
just follow the steps it asks you to do.

When finished. i did go to the folder /root/ and copied the credentials.json to a location on /home/pi/documents/

Playstation 4 settings
Now there is one more step you need to do.
On your playstation go to settings --> Power Save Settings --> Set features available in Rest Mode --> turn on Enable Turning On PS4 from Network.
recommended: Set your PS4 to an static ip address

Testing it
So from this point you should be able to turn off your ps4 with the command:
ps4-waker standby -c /home/pi/Documents/.ps4-wake.credentials.json -d 192.168.1.21
-c is followed by the path where your credentials.json is.
-d is the ip address of your PS4

and you can turn it on by running:
ps4-waker -c /home/pi/Documents/.ps4-wake.credentials.json -d 192.168.1.21

Making it work in HA
Great change that this command will not be working yet if you try to run it as a shell command in Home Assistant.
So what i did is:
Login to putty and then:
sudo su
visudo

then find this line:

# User privilege specification
root    ALL=(ALL:ALL) ALL

copy and past the root line. So now you have two the same lines under each other. Then change root to the username where Home Assistant is running. ( you can find that name in the path to you configuration.yaml.) I think for an all in one install it will be “hass” and for a hassbian install like i have it is “homeassistant”

So the result will be:

# User privilege specification
root    ALL=(ALL:ALL) ALL
homeassistant    ALL=(ALL:ALL) ALL

press ctrl + x and then Y and then Enter.

After this go to the sudoers.d folder:
cd /etc/sudoers.d

And create a new file like this:
sudo nano 010_homeassistant-nopasswd

In this file copy the following line
homeassistant ALL=(ALL) NOPASSWD: ALL

and close it with ctrl + X, then Y then press Enter.

Now shell commands should be working.

@robbiet480 I don’t really have any real coding skills. But if there is nobody else looking into this. I want to try if i can achieve something. But let say if i have ps4-waker working in Python you can make a platform from it?

@jones ofcourse i want to tell you. But it is nothing fancy :slight_smile:

1. Create a folder ps4cam
ok what is did is create a folder called ps4cam in
/home/homeassistant/.homeassistant/www/ps4cam/
and i filled it with images of games i play. The name is important. I saved them with a “1” followed by the current id of the game.
So i have files like this:

1CUSA01433.jpg
1CUSA02387.jpg

2.Create a folder ps4current
Create a folder ps4current with a currentgame.jpg file in it.
/home/homeassistant/.homeassistant/www/ps4current/

so the only thing in this folder is:
currentgame.jpg
(this can be any jpg file it doesn’t matter.)

3. Add a sensor for current id of the game
You also need this sensor (with your ps4 ip address) it will show you the id of the game your playing like this CUSA01433.

platform: command_line
name: current id
command: ps4-waker search -d 192.168.1.21 | grep -w "running-app-titleid" | cut -c 27- | rev | cut -c 3-  | rev | sed 's/^// '

4. Add a Shell Command
I added this command:
ps4_currentapp_update: "cp /home/homeassistant/.homeassistant/www/ps4cam/1{{ states.sensor.current_id.state }}.jpg /home/homeassistant/.homeassistant/www/ps4current/currentgame.jpg"

So what this does is it copies a file from the ps4cam folder with 1 + the game id as the name to the ps4current folder and renames it to currentgame.jpg.

5. Create an automation that changes the images when the current id sensor changes
Create this automation:

alias: PS4 - State change
trigger:
    platform: state
    entity_id: sensor.current_id
action:   
  - service: shell_command.ps4_currentapp_update

6. Last step; Add the camera :wink:
Now add an camera to your configuration like this:

 platform: local_file
 name: currently playing
 file_path: /home/homeassistant/.homeassistant/www/ps4current/currentgame.jpg

Note: also add an image called 1.jpg to your ps4cam folder. This is the image that will be displayed when the PS4 is turned off.

As you can see this is not an very automated process. For every game you want to display you need to find an image and rename and save it yourself. It would be nice if there was some online database with images named after the app id’s Sony gives to their games. So the images could be pulled automatically :slight_smile:

3 Likes

This is fantastic! And I see you have some instructions on setting up the game selection menu earlier in this thread?

Have you found that there is any particular resolution that works best for the game images?

Thank you again for doing this. Can’t wait to try it out.

Yes that is true. It is decribed in post 47. You can leave the step with the script out if you want. The only thing it does is putting text in two input_selects and making an automation. So if you leave that step out you only need to do that yourself.

So in short what you need is 2 input.selects. One for the game name you want to display and one for the game id of the games.
Then you need to create an automation like in step 2. where you make it so when you select a game name that the automation change the input.select with the id to the correct id for that game.
Then you only need the ps4_start_selection: shell command from step 4. to start the game you selected.

As far as image resolution it doesn’t really matter. I have a lot of different sizes. The only think i maybe do is resize them all so they all are the same size. Sometimes one images appears a little bit smaller then the other one.

Sweet! I’ll give it a shot. Thanks for your work putting some instructions together!

1 Like

Thanks @Mister-Espria for sharing. I’ve set it up according to your instructions but I think you’ve missed out an essential step. The shell command to update the ps4 cam image would not work on its own - at least not for me. It has to be triggered so I added this automation that looks after changes of the current id sensor and then executes the command:

- alias: PS4 Current Game Image
  trigger:
    platform: state
    entity_id: sensor.current_id
  action:
    service: shell_command.ps4_currentapp_update

After adding this automation the image updates itself and everything is working fine.

Thank You! you are absolutely right. I forgot to write that down. i edited my post so it’s complete. If somebody else want to set it up.
Thank you again and nice you found the solution yourself :+1: