PlayStation 5 Command Line Sensor Help - Command Failed/Empty JSON

Are you familiar with shell_commands ? The command in this post would be running it on a remote machine… the same way you could test to see if working before putting it into a sensor

I’ve now installed everything so far.
In the shell of home assistant I can already log into the other raspberry pi via ssh without a password and execute the command to start the playstation.

I used the following instructions for logging in loosely

http://linuxproblem.org/art_9.html

so far everything works.

Now I want a switch to send this command, but I don’t know how to create this switch.

➜  ~ ssh [email protected]
Linux raspberrypi 5.10.11-v7l+ #1399 SMP Thu Jan 28 12:09:48 GMT 2021 armv7l

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Thu Jun  3 18:50:42 2021 from 192.168.xxx.xx
[email protected]:~ $ cd ps5-wake/
[email protected]:~/ps5-wake $ ./ps5-wake -vW -194931325 -H 192.168.178.xx
Device found: PS5-248 [PS5/78C881B8BD09]: Home Screen
Sending wake-up...
[email protected]:~/ps5-wake $ 

I have now integrated it that way. Unfortunately the switch doesn’t work. Since the Playstation cannot be switched off, I would like the command to switch on the play for every switch operation

switch:
  - platform: command_line
    switches:
      ps5:
        command_on: "ssh [email protected] | cd ps5-wake/ | ./ps5-wake -vW -194931325 -H 192.168.178.62"
        command_off: "ssh [email protected] | cd ps5-wake/ | ./ps5-wake -vW -194931325 -H 192.168.178.62"
        friendly_name: 'Playstation 5'

Do you know how to get the bash shell inside the home assistant container? I use supervised so I’m not sure if the steps are the same on HaOS… but I would try that command from there so you can see the output… most likely you need to do ssh keygen so HA can run that command without issue… testing in the shell will let you see the error… I will edit post in a minute and link a post that walked me through that process…

So I’m assuming you got it working when manually typing the commands directly on that new pi?

Also the ps5 can automatically turn on and off based on the input of your tv changing… so you can have it shut off as soon as you switch off that input

So I enter the command in the shell directly on Home Assistant and from the home assistant I connect to the other raspberry pi.

this works. but not the platform

an ssh keygen has already been created by me in order to be able to switch to the pi via ssh one password. see my other post the link

Updated with working standby

Using a pi because I already use that for bluetooth → switchbot and, besides containers, I don’t want to install stuff on my NAS, where I run hass.

If your always-on computer is running hass os this probably will not work.

Install npm

sudo apt install nodejs npm

Install https://github.com/dhleong/playactor

sudo npm install -g playactor

Search for PS5

playactor browse

Outputs

{
  "address": {
    "address": "192.168.1.95",
    "family": "IPv4",
    "port": 9302,
    "size": 170
  },
  "hostRequestPort": 997,
  "extras": {
    "statusLine": "620 Server Standby",
    "statusCode": "620",
    "statusMessage": "Server",
    "status": "STANDBY"
  },
  "discoveryVersion": "0003...",
  "systemVersion": "08540...",
  "id": "78C88...",
  "name": "PS5-635",
  "status": "STANDBY",
  "type": "PS5"
}

Copy JSON "name":

playactor login --host-name PS5-635 --no-open-urls

Follow on screen steps

Open the following URL in a web browser to login to your PSN account.
When the page shows "redirect", copy the URL from your browser's address bar and paste it here.
  https://auth.api.sonyentertainmentnetwork.com/...

Checking PS5 state

playactor check outputs json but for some reason refuses to play nice with command line sensor so I also installed jq

sudo apt install jq

Home Assistant

These can be combined with a template switch

sensor:
  - platform: command_line
    name: Playstation
    scan_interval: 300
    command: ssh -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] 'playactor check --host-name PS5-635 | jq -r ".status"'
    value_template: >
      {% if value == 'AWAKE' %}
        ON
      {% elif value == 'STANDBY' %}
        OFF
      {% else %}
        Okänd
      {% endif %}
shell_command:
  playstation_5_wake: ssh -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] 'playactor wake --host-name PS5-635'
  playstation_5_sleep: ssh -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] 'playactor standby --host-name PS5-635'
3 Likes

Great guide! I installed npm, node and playactor on my Pi’s main os, everything is working great.

You can also do a switch entity that automatically updates the state. That way you dont have to do shell_command and an additional sensor, everything can be handled in one place:

switch:
- platform: command_line
  switches:
      playstation:
        value_template: >
          {% if value == 'AWAKE' %}
            true
          {% elif value == 'STANDBY' %}
            false
          {% else %}
            Unknown
          {% endif %}
        command_state: ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] 'playactor check --host-name PS5-639 | jq -r ".status"'
        command_on: ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] 'playactor wake --host-name PS5-639'
        command_off: ssh -i /config/ssh/id_rsa -o StrictHostKeyChecking=no [email protected] 'playactor standby --host-name PS5-639'
        friendly_name: Playstation 5

@skynet01 Nice! That’s neater.

btw @wicol’s state sensor is faster
https://github.com/wicol/ha-config/blob/590f494721b889dda3f7ab689b61ac6fede8cfd8/sensors/ps5.py

switch:
  - platform: command_line
    switches:
      playstation_5:
        friendly_name: Playstation
        command_state: !secret playstation_5_state
        value_template: >
          {{ value == 'ON' }}
        command_on: !secret playstation_5_wake
        command_off: !secret playstation_5_sleep

Home Assistant Operating System


Proof of concept

https://developers.home-assistant.io/docs/operating-system/debugging/

This section is not for end users. End users should use the SSH add-on to SSH into Home Assistant. This is for developers of Home Assistant. Do not ask for support if you are using these options.


root

ssh [email protected] -p 22222

build dockerfile

docker build -t playactor:latest -f - . << EOF
FROM alpine:3.12.8
RUN apk add nodejs-current
RUN apk add npm
RUN npm install -g playactor
EOF

run container

docker run -d --name playactor --network=host playactor tail -f /dev/null

test

docker exec -it homeassistant /bin/bash
ssh-keygen -f /config/.ssh/id_rsa
cat /config/.ssh/id_rsa.pub #copy
exit
vi /root/.ssh/authorized_keys #paste
docker exec -it homeassistant /bin/bash
ssh -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] -p 22222 'docker exec playactor sh -c "playactor about"'

entity

sensor:
  - platform: command_line
    command: ssh -o StrictHostKeyChecking=no -i /config/.ssh/id_rsa [email protected] -p 22222 'docker exec playactor sh -c "playactor about"'

sensor

EDIT:
For auth persistence add RUN mkdir /root/.config/playactor/ to docker build and map generated credentials.json to local folder

-v /path/to/folder:/root/.config/playactor

2 Likes

@Mattias_Persson so the ssh script is good for turning it on and off while Wicol’s is better for status? If the value_template is used in the switch does it really matter which method is used? I think the switch only checks for the state when PS is turned / turned off and not at constant intervals?

Supposedly scan_interval should work… looks like an old post, I’ll try it if I get a chance later.

Just started my ps5 manually, and the sensor updated without ha interaction :+1:
default seems to be 30 seconds, which is quite often

  - platform: command_line
    scan_interval: 300
    switches:
      playstation_5:
        ...

…yeah the switch is about 30 seconds I would say. It seems to work ok, even though I agree its too often. Unfortunately I use it an automation where it switches my receiver and tv to correct inputs so I can’t be there waiting for 90 seconds

I mean if you toggle the command_line switch it updates fine. If you turn on physically it may take 30 seconds if the scan interval cycle just started.

If I’m turning on physically hdmi-cec takes care of hdmi switching anyway so I’m fine with the 300s

also there is this spam script if you have a trigger

- repeat:
    count: 10
    sequence:
      - condition: not
        conditions:
          - condition: state
            entity_id: switch.playstation_5
            state: 'on'
      - service: homeassistant.update_entity
        entity_id: switch.playstation_5

I’m getting an error when trying to implement this. Apart from dropping the .py script into a folder and replicating your code in binary_sensors (with correct ip addess), is there something I’m missing?

What is the error you are getting and how are you trying to run it?

Hi! Seems to have issues running the command…

Logger: homeassistant.components.command_line
Source: components/command_line/init.py:41
Integration: command_line (documentation, issues)
First occurred: 11:11:28 AM (203 occurrences)
Last logged: 11:45:24 AM

Command failed: /config/sensors/ps5.py -b 192.168.1.31

What type of install are you using and do you know how to get to the command line via ssh so you are running it as if you are homeassistant ?

HomeAssistant OS. Yeh, but not 100% sure of the commands?

Do you know what folder you dropped it in? It looks like you’re trying to run it from the exact same path as me which might be wrong…?