Switch for Playstation 4

I like the netcat method but I’m not getting a 1 or 0 return - I get nothing unless I use the -v flag. Any ideas why?

Found that to get the output, the command looks like this:
> nc -z -w 5 192.168.1.66 997; echo $?

I don’t think that is the output. If you do only echo $? the result will be zero. But when i run the command i don’t get output also.
But it works in HA. Did you test by just adding it to your config like i did?

Yeah, that seems to work. Was surprised when I didn’t get output from the command line but oh well.

For those interested, I expanded on this idea a bit and added an input_select to launch to specific apps.

input_select:

  playstation4app:
    name: App Selection
    options:
     - "-- Select --"
     - "Playstation Vue"
     - "Plex"
    initial: "-- Select --"
    icon: mdi:apps

Automation

  - alias: PS4 - Open Vue
    trigger:
       platform: state
       entity_id: 'input_select.playstation4app' 
       to: "Playstation Vue"  
    action:
      - service: shell_command.playstationvue_start 
      - service: input_select.select_option
        data:
          entity_id: input_select.playstation4app
          option: "-- Select --"
      - service: notify.notifyall
        data:
          message: "Playstation starting Vue"
          
  - alias: PS4 - Open Plex
    trigger:
       platform: state
       entity_id: 'input_select.playstation4app' 
       to: "Plex"  
    action:
      - service: shell_command.playstationplex_start 
      - service: input_select.select_option
        data:
          entity_id: input_select.playstation4app
          option: "-- Select --"
      - service: notify.notifyall
        data:
          message: "Playstation starting Plex"

shell_command

  playstationvue_start: "sudo ps4-waker -c /config/ps4-waker/.ps4-wake.credentials.json -b 192.168.1.188 -d 192.168.1.66; sudo ps4-waker -c /config/ps4-waker/.ps4-wake.credentials.json -b 192.168.1.188 -d 192.168.1.66 start CUSA00960"

Important bit is the AppID at the end which is found following the instructions here: https://github.com/dhleong/ps4-waker/wiki

4 Likes

Thank you! examples are always welcome. Your post solved this question i posted on the forums. I had never thought of changing an input select with another one. Thank you!

Did you get the PS3 to turn on at all? I haven’t been able to… :frowning:

I don’t have a PS3 hooked up to test but it works great with my PS4

Hmmm, so from RPi, when logged in as “pi” user it works.

When I would like to wake up my PS4, I can try this (PS4 is already registered)

$ ps4-waker
Device must be awake for initial registration

Hmm, and when I sue it with sudo, it works:

$ sudo ps4-waker
WAKEUP sent to device… 10.1.2.105
Logging in…
Done!

So I enter my venv (I have RPi installed inside venv):

sudo su -s /bin/bash homeassistant
source /srv/homeassistant/homeassistant_venv/bin/activate

but when I want to launch a script from inside venv (as a homeassistant user), it doesnt work:

$ ps4-waker
No credentials; Use Playstation App and try to connect to PS4-Waker

And when i stry to run it via sudo:

$ sudo ps4-waker
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for homeassistant:

It needs user’s pssword for sudo-ing but I guess that homeassistant user (when installed via original doc into venv), it’s without password…

Any clue how to solve this? (rpi / HA via venv / ps4 wakeup)?

so, solved by copying creditentils from /rott folder into homeassistant folder:

sudo cp /root/.ps4-wake.credentials.json /home/homeassistant/
cd /home/homeassistant/
sudo chown homeassistant:homeassistant .ps4-wake.credentials.json

And one more question… Now it works just great. Except for one thing: I switch my PS4 for example from OFF to ON. SO I click on HS switch (toggle button). After clicking on the button, it will toggle from OFF to ON (of course). But after a little while, it will flip back to OFF. And after another while (few seconds) it will flip back to ON. This is probably caused by delay, coz PS4 needs some time to wake-up and in-between “OFF” value is returned… Is there some solution/workaround for this?

I am running into an issue where it seems to start, login, and launch netflix (as desired) for the first 1 or 2 times and then it starts booting the ps4 up the screen that says “welcome back, press the PS button on your controller…” Anyone know what is going on here? I am suspecting permission issues between sudo/homeassistant?

I also got that behaviour sometimes, but not all the time.

Nice tip Kylerw! I set my system like that. One problem though. I keep getting those annoying pop ups saying that “My companion app has turned on/off” I tried disabling all notifications from ps4 setup, but it doesn’t seem to care. Still getting the pop ups. Do you have the same situation?

I haven’t played with it enough to find a solution for this. My PS4 isn’t a primary component for me so it’s not on often.

Is there an option to ‘trust’ the app?

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