PS4 times on

Hi all

is there a way to know how much time a PS4 (or similar device) was on in a day?

Maybe using device tracker or what?

Thanks

Do you have a smart switch to turn it on?
Switches have a log with info about times it was on similar to device tracker.

I use the this PS4 custom component. It ads the PS4 as a mediaplayer, now I can not only monitor the time spent on the playstation but also what games are played.

1 Like

I have a fake switch that shows only the state of the PS4, no switch function.
The rest is done with the history_stats and a template sensor.

switch:
  - platform: command_line
    switches:
      playstation4:
        command_on: "echo on"
        command_off: "echo off"
        command_state: "nc -z -w 5 192.168.100.24 9295"
        friendly_name: Playstation 4

sensor:
  - platform: history_stats
    name: PS4 today
    entity_id: switch.playstation4
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'

  - platform: template
    sensors:
      ps4_today_time:
        value_template: '{{ states.sensor.ps4_today.attributes.value }}'
1 Like

Hi @piotr
I have seen your link, but I’m on Hass.io and I saw it can be done with complex steps form.
Thanks anyway, I’ll keep in mind if I will move to hassbian someday.

Hi @maxdaniel

No, the PS4 it’s not connected to a smart switch, just a normal plug

Thanks

Hi @VDRainer

I’ll try your solution!

Thanks

Hello everybody,
It is an old threat but i have a question. I use hass.io on a raspberry pi.
The switch is working, but i get no data from time the playstation was on. Ps4 today and ps today time.

Anyone suggestion?

You probably have to use the history statistics sensor for that information. I’m not sure but I think it was implied in this post. Anyways, here’s the component:

Hi Petro,

this is my config now:
Switch:
switch:

  • platform: command_line
    switches:
    playstation4:
    command_on: “echo on”
    command_off: “echo off”
    command_state: “nc -z -w 5 IPnumber 9295”
    friendly_name: PlayStation

Sensor:

Playstation 4

  • platform: history_stats
    name: PS4 today
    entity_id: switch.playstation4
    state: ‘on’
    type: time
    start: ‘{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}’
    end: ‘{{ now() }}’

  • platform: template
    sensors:
    ps4_today_time:
    friendly_name: PS4 vandaag in tijd
    value_template: “{{ state_attr(‘ps4_today’) }}”

Switch is working, time at this point unknown. But before i continue, can you see if there are any faults in my script?

can you format it properly? Blue link at the top of the page will show you how to format. Click the link, the short instructions are poor.

switch:
  - platform: command_line
    switches:
      playstation4:
        command_on: "echo on"
        command_off: "echo off"
        command_state: "nc -z -w 5 IPNUMBER 9295"
        friendly_name: PlayStation 

sensor:
 - platform: history_stats
    name: PS4 today
    entity_id: switch.playstation4
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
    
  - platform: template
    sensors:
      ps4_today_time:
        friendly_name: PS4 vandaag in tijd
        value_template: "{{ state_attr('sensor.ps4_today') }}"   

I have already thought to try sensor.ps4_today but i am not that familiar with code so maby you can help. What i allready achieved is to get 2 unknown instead of nothing.

Thanks in advance

  1. Spacing is wrong platform: history_states
  2. state_attr is for getting attributes on the state, not the state.
sensor:
  - platform: history_stats
    name: PS4 today
    entity_id: switch.playstation4
    state: 'on'
    type: time
    start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
    end: '{{ now() }}'
    
  - platform: template
    sensors:
      ps4_today_time:
        friendly_name: PS4 vandaag in tijd
        value_template: "{{ states('sensor.ps4_today') }}"   

The value_template of the template sensor should point to the value attribute of the history_stats sensor.
So it should be:

value_template: "{{ state_attr('sensor.ps4_today', 'value') }}"

or like in my first post:

value_template: '{{ states.sensor.ps4_today.attributes.value }}'

Both the same.

1 Like

Hi VDRainer.
i see that changing the last line changed it to none instead of unknown. But i think, i think because of the language that i misunderstand ‘value’. I fill in value but i think that is wrong. The history_stats sensor still states in unknown fase.

Hi all,
I have done everything all over again.
Switch is working
History statistics sensor unknown
Template sensor blanc

I turn on ps4:
Switch is turned on - thus working
History sensor - unknown
Template sensor - blanc

I turn off ps4:
Switch is turned off
History sensor - unknown
Template sensor - blanc

So in my opinion there is something wrong with the history stats and therefore the template sensor is blanc. That the state ‘on’ for the switch isn’t reconized by the history stats.

Do you have History enabled?

History is enabled but when i click on history nothing is displayed in ui

Tested several scenario’s but logbook and history are not up showing anything in UI.

Should i make backup and reinstall my pi? And then restore backup with apps and settings?

Anything in the logs about recorder?
Maybe delete the home-assistant_v2.db in your config directory and restart ha.