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

@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…?

Yeh, I created and placed it in the same path…

Ok, then like Bartem said you should try to run it manually from inside of HAs runtime environment. Not sure here but HA OS runs HA in docker right? So you probably need to exec in to at shell of the HA container? docker exec -it <container name> bash and then /config/sensors/ps5.py -b 192.168.1.31.
I don’t run HA OS so correct me if I’m wrong here guys…

Yes that is how I test my command line stuff

Looks like a lot of extra work with the ps5-wake and the node based playactor stuff… maybe I should just add wake support to the python script as well…?
I really thought someone would’ve just ported the ps4 integration at this point but I guess “if you want something done, do it yourself” eh?

I’ll start with taking a look at adding wake to the python script.

Ok, there we go - added wake functionality:

Give it a try like so:
./ps5.py -w -u <user credential> <ps5 ip>

And to query for state:
./ps5.py -q <ps5 ip>

When tcpdumping, my user credential started with a “-” and my “model” header was “a” instead of “m” which I’ve seen earlier :man_shrugging:
I ran tcpdump on my UniFi AP btw - running it on my gateway (UniFi USG) didn’t catch the WAKEUP packets.

1 Like

All working great in HASS OS :slight_smile:

Anyone struggling - here’s the command line for testing :

docker exec -it homeassistant python /config/sensors/ps5.py -q ps5_ip

Binary sensor looks like this :

  - platform: command_line
    name: ps5
    command: python /config/sensors/ps5.py -q -b 192.168.1.225
    scan_interval: 10

Now - anyone got any clue how we send it to sleep again? The Remote Play App can…

If it’s a binary sensor you should add the -b option or HA will be confused when it’s in standby :slight_smile:

When it comes to putting the PS5 to sleep I haven’t found good info on how to do so. I saw playactor can do it but I’m having trouble following that code to port it.