Switch for Playstation 4

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:

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: