Switch for Playstation 4

I had an issue with my PS4 recently and it just shut off on me. Following this error, my netcat command says the connection to the ps4 succeeded even when my ps4 is in standby mode. Before the error, everything worked perfectly. Any advice or suggestion?

I have everything set, and I get the right status for the switch. The odd thing is that I can set the ps4 to standby, but it fails when the wake up call is sent

homeassistant@HASS:~ $ ps4-waker
WAKEUP sent to device… X.X.X.X
Error: Device didn’t wake in time
at loop (/usr/local/lib/node_modules/ps4-waker/index.js:295:26)
at /usr/local/lib/node_modules/ps4-waker/lib/detector.js:142:13
at Detector. (/usr/local/lib/node_modules/ps4-waker/lib/detector.js:120:26)
at emitTwo (events.js:106:13)
at Detector.emit (events.js:191:7)
at Socket. (/usr/local/lib/node_modules/ps4-waker/lib/detector.js:60:18)
at emitTwo (events.js:106:13)
at Socket.emit (events.js:191:7)
at UDP.onMessage [as onmessage] (dgram.js:547:8)

Any clue?

Edit: I have found the answer, there is an option on the power options of the ps4 to allow remote wake up, it should be enabled.

Hi with ps4-waker is it also possible to check which game is currently playing.
you can add a command line sensor for this like so;

platform: command_line
name: current game
command: ps4-waker search | grep -w "running-app-name" | cut -c 24- | rev | cut -c 3-  | rev | sed 's/[!@#\$%^&*(®)™]//g'
scan_interval: 60

For game selection option i added two input selects. and i created an script that can be run manually or every x minutes if the ps4 is on. The script will retrieve the current app name and id and will save it to my corresponding input_select.yaml
and then it creates an automation in my automatons folder to start the game if it is selected. So every time i play a new game it will be added to the dropdown list automatically.

In case somebody is interested in the script let me know then i will post it, but it assumes a few things.

4 Likes

Awesome, would love to see.

Great work! I will test it as soon as I get my PS 4.

Ok, but first of all im not a programmer. Everything i did is just trial and error. when i said script i didn’t mean an home assistant script :slight_smile:.
You need to have a specific configuration layout for your home-assistant yaml’s. or you need to adjust the script somehow. I use the same layout as Ben from bruhautomation.

Working of the script
The script will add every time a new game is discovered the appname and appid to the input_selects created for this.
It will also create an automation in your home-assistant config directory which take care of changing the second input select based on the first input_select.

In order to make this work the steps are as followed

Steps:

  1. Create two Input_select files
  2. Create an automation
  3. Save the script somewhere
  4. Create shell command
  5. Edit the script

1. Create two Input_select files

So then u need to create two different input_select.yaml files
The first one i have is located at /home/homeassistant/.homeassistant/config/input_select/choose_game_ps4_kevin.yaml

it contains:

choose_game:
  name: "Play Game"
  initial: "Choose Game"
  options:
   - Choose Game

the second file is located at /home/homeassistant/.homeassistant/config/input_select/choose_id_ps4_kevin.yaml

it contains:

choose_id:
  name: "Play id"
  initial: empty
  options:
   - empty

2. Create an automation

My automation is located at /home/homeassistant/.homeassistant/config/automations/playstation_kevin/a_choose_game.yaml

it contains:

alias: PS4 - Choose Game
trigger:
   platform: state 
   entity_id: 'input_select.choose_game' 
   to: "Choose Game"
action:   
  - service: input_select.select_option   
    data:   
      entity_id: 'input_select.choose_id' 
      option: "empty"

3. Save the script somewhere

You can save the file anywhere i think. Mine is located at /home/pi/Documents/myownscripts/ps4/add-current-game

This is the script:

#!/bin/bash

##script to discover game names and id's of playstation 4 games and apps when playing. 
##Then adds them to input selects within home-assistant configuration and creates a automation for each game 
## So it will be possible to start a game from input select

#For this script to work you need to have ps4-waker installed and working.
#ps4-waker https://github.com/dhleong/ps4-waker

# Created by Mister_Espria
# Date 12/02/2017

##########USER INPUT############################################

###Set ip adress for playstation 4 console###
ps4ip=192.168.1.21
###set location of input select.yaml for game names###  You nee to create a specific input_select yaml file which will only be used for one input select
yamlgamename=/home/homeassistant/.homeassistant/config/input_select/choose_game_ps4_kevin.yaml  #/home/pi/choose_game_ps4_kevin.yaml
###set location of input select.yaml for game ID's###
yamlgameid=/home/homeassistant/.homeassistant/config/input_select/choose_id_ps4_kevin.yaml
###set location for automations file (to be created) ###
yamlps4auto=/home/homeassistant/.homeassistant/config/automations/playstation_kevin/
###the text of prefixauto will come in front of your automation name so in this case the automation name will be a_PS4_gamename
prefixauto=a_PS4_

###name of input select you created for the game names
inputselect_name_for_game=choose_game
###name of input select you created for the game ID's
inputselect_name_for_id=choose_id

###Name of shellcommand you created to start selected game (without shellcommand. prefix)
### For example:  ps4_start_selection: ps4-waker -c /home/pi/Documents/.ps4-wake.credentials.json -d 192.168.1.21 start {{ states.input_select.choose_id.state }}
shellcommandname=ps4_start_selection

###see info below
### standard value for inputselect_name_for_game  --> in this case --> Choose Game
standard_value_inputselect_name_for_game="Choose Game"

###info 
##you need to create one automation yourself this is with the standard values of the input select. (see commented automation below)
##So after you have chosen a game it will autmatically go back to choose game value. 
##(make sure (in this case) you have choose Game and empty in your input selects

#alias: PS4 - Choose Game
#trigger:
#   platform: state 
#   entity_id: 'input_select.choose_game' 
#   to: "Choose Game"
#action:   
#  - service: input_select.select_option   
#    data:   
#      entity_id: input_select.choose_id' 
#      option: "empty"

########END USER INPUT###########################################


#commands to find current app-name and ID
nameVar=$(ps4-waker search -d "$ps4ip"  | grep -w "running-app-name" | cut -c 24- | rev | cut -c 3-  | rev | sed 's/[!@#\$%^&*(®)™]//g');
idVar=$(ps4-waker search -d "$ps4ip" | grep -w "running-app-titleid" | cut -c 27- | rev | cut -c 3-  | rev | sed 's/^// ');

#adding spaces to nameVar and idVar value
formnameVar="\  \ - $nameVar"
formidVar="\  \ - $idVar"
#add _ instead of spaces in game name
namenospaceVar=${prefixauto}${nameVar// /_}

#check if namevar already is in file if not add it
if sudo cat  "$yamlgamename" |                                                             
  grep -qFe "$nameVar"
then
#if found in file: 
  echo found
else
#if not:
  echo not found
  ###second check starts here
if sudo cat "$yamlgameid"  |
  grep -qFe "$idVar"
then
#if found in file: 
  echo found
else
#if not:
  echo not found
  sudo sed -i  "$ a $formnameVar"  "$yamlgamename" 
  sudo sed -i  "$ a $formidVar" "$yamlgameid" 


#create automation.yaml for game
cat > "$yamlps4auto$namenospaceVar.yaml" <<EOL
alias: PS4 - ${nameVar}
trigger:
   platform: state 
   entity_id: 'input_select.$inputselect_name_for_game'
   to: "${nameVar}"
action:   
  - service: input_select.select_option   
    data:   
      entity_id: 'input_select.$inputselect_name_for_id' 
      option: "${idVar}"
  - service: shell_command.$shellcommandname
  - service: input_select.select_option   
    data:   
      entity_id: 'input_select.$inputselect_name_for_game' 
      option: "$standard_value_inputselect_name_for_game"
EOL

fi
fi

4. Create two shell command

We create an shell command already to be able to run the script from within home-assitant.
My shell command is:
add_current_game: "/home/pi/Documents/myownscripts/ps4/add-current-game"
ps4_start_selection: "ps4-waker -c /home/pi/Documents/.ps4-wake.credentials.json -d 192.168.1.21 start {{ states.input_select.choose_id.state }}"

The first shell command can be used in an automation or script later if you want to fire the execution of the script.
This second shell command is used in the script when creating the automation. You nee to change the location of your credentials.json en the ip address of your console

5. Edit the script

You need to change a few variables.

  1. enter the ip address of your console.
  2. path to the first yaml file we created in step 1
  3. path to the second yaml file we created in step 1
  4. the location where newly created automation can be stored in order for home-assistant to read them
  5. is a prefix which can be changed, but no necessary.
  6. and 7. if you used the same names for the input select as in step 1 you don’t need to change this. otherwise change to the corresponding name.
  7. Enter the name of your shell command if you are using the same name as in step 4, you don’t need to change this.
  8. Change this to the initial value of the first inputs_select we created in step 1 (not needed to change if you are using the same value as in step 1; Choose Game).

Finishing

After this the script should work. You can create an script in home-assistant to fire your shell command or an automation to fire it automatically so now and then.

You may need to set permissions and make the file executable by home-assistant
i had some trouble with this. the script didn’t get executed by home-assistant but was working properly in the console.
I am on an hassbian install and had to add the user home-assistant to the sudoers in order to get it working which is not an nice way.

You can also run the script from the console like this to check if it is working:
/home/pi/Documents/myownscripts/ps4/add-current-game

Note
The script is working in your home-assistant config directory so keep this in mind.
Make backups before messing around there.
You can also change the path variables and check on a safe location if the script is working.

6 Likes

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.