Hi All,
Hoping to get some advice here. I have a Kodi media center and a Bravia smart TV and I am trying to create an automation so that if I power off my TV, Kodi will stop playing any media that is currently being played but only if the TV is currently on Input HDMI 4. My automation currently looks like this:
- alias: Stop TV when no media
trigger:
platform: state
entity_id: media_player.chris_bedroom_tv
from: ‘on’
to: ‘off’
condition:
condition: state
state: ‘HDMI 4’
entity_id: sensor.chris_tv_bedroom_input
action:
- service: media_player.media_stop
entity_id: media_player.kodi
Where my sensor is updated from the media_title attribute of the Sony TV. The only issue is, as soon as the TV is powered off the ‘media_title’ attribute is removed from HomeAssistant and so my sensor gets a blank value so my automation never triggers. Is there a way to perform a condition on the last value the sensor was, or some other way of preventing the sensor from getting a value of blank?
Cheers!
I needed to know something similar for a lights automation and here’s what I did.
setup the ping
device tracker to track the tv. If the IP was pingable the TV is on. Then I created a sensor to tell me if the tv is on(home) or off(away).
- platform: template
sensors:
tv_status:
friendly_name: "Livingroom TV"
value_template: '{% if is_state("device_tracker.tv_tracker", "home") %}On{% else %}Off{% endif %}'
icon_template: mdi:television
Then I just use home/not_home state for condition checking. This might be an option that would work for you.
Thanks for the suggestion!
I can actually already tell if the TV is on or off (the state of the TV in HASS is helpfully updated to either ‘on’ or ‘off’). What I need to do is track the last state of an attribute (media_title) that is removed as soon as the TV is powered off.
Why not just create a sensor that changes when the TV is turned off (triggered) to be the last state of the media_title. Either that or every time the kodi attribute is changed it writes it to the sensor and then tell it to ignore it if it’s changed to off or whatever?
That would be perfect I think. Do you have any idea what sort of template I could use to prevent the sensor from updating if the TV is off?
Thanks again!
- platform: template
sensors:
kodi_last_played:
friendly_name: "Last Played"
value_template: '{% if is_state('media_player.tv_name_here', 'on') %} {{ states.media_player.kodi.attribute.media_title }} {% endif %}
I don’t know the syntax stuff for the kodi element so that may not be correct. This should change to be whatever is currently playing on your media device (i think, kinda spitballing here) but not change if it’s turned to off. The problem here is that as soon as you turn your tv on again it will change again so whatever automation you use will need to read that value before the TV is turned on. There’s probably a more elegant way to do this but this is just off the top of my head.
That’s perfect! exactly what I need to start building a config on!
Thanks a bunch!