Switch for Playstation 4

Got it fixed by moving the credentials file into the HASS env. Thanks to the posters above.

I am having an issue running the add-current-game script from within HA. Here is what I have in the scripts file:

alias: 'Add Current Game'
sequence:
  - service: shell_command.add_current_game

I installed HA via the AiO script:
The file is executable
I have added user homeassistant to /etc/sudoers group as described above
I am able to run sudo /scriptpath from the console and it works (as pi user)

I would like to be able to click the activate button from within HA to add the current game. Could anyone provide some insight? Share working scripts/automations?

Thanks

Sorry,

Here is my shell_command:

add_current_game: "sudo /home/pi/Documents/Scripts/add-current-game.sh"

I have tried with both single and double quotes as well as no quotes. Have also tried with and without sudo

Oke we need to achieve that the script will run from the console (as the hass user) Then it also will work when it is assigned to a button in HA.

For now, a reason why it isn’t working i can think of is that the ownership of the add_current_game file is pi and HA can’t execute it as it is running as the hass or home-assistant user. (i run the shell command like this add_current_game: "/home/pi/Documents/myownscripts/ps4/add-current-game" )

So in your case maybe changing ownership helps. Like this:
sudo chown hass /home/pi/Documents/Scripts/add-current-game.sh
or (depending on the username where HA is running)
sudo chown homeassistant /home/pi/Documents/Scripts/add-current-game.sh

I forgot to mention that I did try this before, both in the location above and moving it inside the homeassistant folder to /shell_scripts. I have switched to the homeassistant account by running sudo su -s /bin/bash homeassistant.

This is what i get when trying it with and without sudo and with and without the “.sh” at the end:

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:

Now, I am no expert when it comes to linux and dont want to go making changes to the homeassistant account unless i know im doing it right. I suppose this is why it is not working? I have no idea what the homeassistant password is. Is there a way i can change it without breaking anything?

Hi i think i know why it isn’t working. I forgot a step… after editing the sudoers file.
It’s explained in this post.

So it comes down to doing this:

cd /etc/sudoers.d
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.

Its working like a charm to turn on/off the playstation but the status is always on off? any idea what it could be?

1 Like

What are you using for the command state?

command_state: 'nc -z -w 5 192.168.1.21 9295'

Yes exactly :slight_smile: command_state: ‘nc -z -w 5 192.168.1.4 9295’

and you did also install it netcat?

sudo apt-get install netcat

Yes, but it didnt change

hmm i can’t of anything right now. you have set your ps4 to use a static ip adress right?

I did aswel, going to recheck everything :slight_smile: thanks for the help

okay maybe you can post what the output is if you run the command from the console if ps4 is on and off. maybe that will give some insight.

edit: when i use it it has no output.

Have you tried executing the command with full path to netcat like this?
/bin/nc -z -w 5 192.168.1.4 9295

No I didnt but I will try the full path to see if it’s working :slight_smile:

strange… i don’t get it running at all:

Okay i noticed there a few people having issues with the on/off button showing the incorrect state.
I have found a solution for this, it’s basically using another program to check on the network if the ps4 is up.

So with this solution there still will be an small delay. When you press the button it will go to on for a few seconds and then go back to off and as soon as the ps4 is fully booted up the switch will stay on until the ps4 is shutdown.( This will all happen in the first 1 to max 2 minutes i think.)

Solution
Install if you don’t have it installed already:
sudo apt-get install nmap

Edit your command line switch.

  playstation_4:
    command_on: 'ps4-waker -d 192.168.1.10 -c /home/pi/Documents/ps4beneden/.ps4-wake.credentials.json'
    command_off: 'ps4-waker standby -d 192.168.1.10 -c /home/pi/Documents/ps4beneden/.ps4-wake.credentials.json'
    command_state: 'nmap -p 9295 192.168.1.10 | grep -w -Eo "open" '
    value_template: '{{ value == "open" }}'
    friendly_name: "Playstation 4"

small explanation what’s going on
As you see the command_state has changed and the value_template is added.
The nmap command will look at port 9295 on the ip address of the ps4 (if correctly specified.) and if it finds the word open it will change the button to on.
if you want you can try this command nmap -p 9295 192.168.1.10 in the console to see what the output is.

Hi, Thanks for reply
I install map and add the command like yours ( I changed my IP)
But I look my log it shows

17-04-01 10:53:32 ERROR (MainThread) [homeassistant.loader] Unable to find component command_on
17-04-01 10:53:33 ERROR (MainThread) [homeassistant.loader] Unable to find component switches
17-04-01 10:53:33 ERROR (MainThread) [homeassistant.loader] Unable to find component value_template
17-04-01 10:53:33 ERROR (MainThread) [homeassistant.loader] Unable to find component platform
17-04-01 10:53:33 ERROR (MainThread) [homeassistant.loader] Unable to find component friendly_name
17-04-01 10:53:33 ERROR (MainThread) [homeassistant.loader] Unable to find component command_off
17-04-01 10:53:33 ERROR (MainThread) [homeassistant.loader] Unable to find component ps4

It looks like an error wich has to do something with indentation (formatting) of your config file.
It looks like you have every line starting at the same proint.
Can you post your config?