Hi, I’m new to Home Assistant but I’m starting to get the hang of it, a bit…
Now I have to following question I’m probably unable to find the right searchwords for so I hope you guys can help:
I have managed to set up this PS4 Mediaplayer and it gives me what game I’m playing in source.
I’ve made some history stats to keep track of how long I’m playing that game.
Now I need to set a sensor for every game, and that’s a lot of work and rebooting
So I would like to create a sensor for every game that’s detected, it’s stored in the attribute “source” and aslo in a JSON file generated by the PS4 plugin
Ok, I’ve been working on it, and I’ve decided that writing a python script that would add the sensor to a yaml file would be the best choice.
Only problem would be that it would need a reboot to activate (I think)
so I started out with the hello_world.py example changed some values, but I can’t get the state of the sensor to the .py file to work with.
This is what I have hello_world.py:
game = data.get(‘game’, ‘world’)
logger.info(“Hello {}”.format(game))
an automation to call that when the state changes:
id: ‘1550943095391’
alias: yrdy
trigger:
entity_id: sensor.playing_playstation_4_pro
platform: state
condition: []
action:
data:
game:
value_template: {{ sensor.playing_playstation_4_pro.state }} <<< Here I’m baffled about what to put to get the sensor state as text to python.
service: python_script.hello_world
I don’t think it’s impossible, but I’m unable to find the right syntax to make it work.
Please help.
You can try something like this in a python script:
#you don't need this line, I don't have sensor.playing_playstation_4_pro
hass.states.set('sensor.playing_playstation_4_pro', 'playing', {"source":"Star Wars II"})
#create sensor.<game_name> from attribute 'source'
game=hass.states.get('sensor.playing_playstation_4_pro').attributes.get('source')
hass.states.set(('sensor.' + game), 'state you want', {"attribute1":"what you want1", "attribute2":"what you want2"})
What I want is to create a sensor for every game I play (automatically) that keeps track of the time I’ve played it.
The info is stored in sensor.playstation_4_pro_source in the state is the name of the game.
This is what I created manually and it’s working:
sensor:
- platform: history_stats
name: Played Youtube
entity_id: sensor.playstation_4_pro_source
state: ‘YouTube’
type: time
start: ‘{{ 0 }}’
end: ‘{{ now() }}’
I’ve also created a card in Lovelace that filters all "sensor.played_" to display in the card.
card:
title: Games Played
type: entities
filter:
include:
- entity_id: sensor.played_
show_empty: false
type: ‘custom:monster-card’
can I create a sensor from platform history_stats with the hass.states.set routine?
You can call the sensor this way to use with your monster card and get the history_stats information as state/attributes for the created sensor.played_<game_name>:
hass.states.set(('sensor.played_' + game), 'state you want', {"attribute1":"what you want1", "attribute2":"what you want2"})
I just did this, it’s not working as desired yet, but I got a sensor with wrong value’s:
hass.states.set((‘sensor.Played_’ + game), game, {“platform”:“history_stats”, “entity_id”:“sensor.playstation_4_pro_source”, “type”:“time”, “start”:"{{ 0 }}", “end”: “{{now()}}” })
Did get an error with games with spaces in, for now I just started a game with 1 name… Limbo in this case.
Not exactly what you’re looking for, but, for my config, I needed to track how much time I play (sum of all the ps4 games), here’s the solution I found:
create a template sensor ‘sensor.game_ps4’ that has two states on/off (‘on’ if playing to a game)
- platform: template
sensors:
game_ps4:
value_template: >-
{% set source = states.media_player.ps4.attributes.source -%}
{% set l = source | length %}
{% if (source not in ['Plex', 'YouTube', 'Netflix', 'Amazon Prime Video']) and (l > 0) %}
on
{% else %}
off
{% endif %}
Add daily / weekly tracking sensor using the history_stats platform
- platform: history_stats
name: Track Game on PS4 Week
entity_id: sensor.game_ps4
state: 'on'
type: time
start: '{{ as_timestamp( now().replace(hour=0).replace(minute=0).replace(second=0) ) - now().weekday() * 86400 }}'
end: '{{ now() }}'
- platform: history_stats
name: Track Game on PS4 Today
entity_id: sensor.game_ps4
state: 'on'
type: time
start: '{{ now().replace(hour=0).replace(minute=0).replace(second=0) }}'
end: '{{ now() }}'
EDIT : To determine whether the ps4 is playing a game and not a media app, I wrote down a list of the common media apps I use on my ps4 : [‘Plex’, ‘YouTube’, ‘Netflix’, ‘Amazon Prime Video’].
You can add a sensor file in order to load the ps4-games.json that stores all the games and filter them in your config File - Home Assistant