I am trying to get simple automation to work with my TV (turning on and off). I am able to create a script that turns the TV on and off, but the problem is when I turn the TV on or off via the remote control the status is not changing in the boolean. Here are the scripts:
input_boolean:
tv:
name: TV
initial: off
icon: mdi:television
script:
tv_on:
alias: 'Turn Television on'
sequence:
- condition: state
entity_id: input_boolean.tv
state: 'off'
- service: media_player.turn_on
data:
entity_id: media_player.living_room_tv
- service: homeassistant.turn_on
data:
entity_id: input_boolean.tv
tv_off:
alias: 'Turn Television off'
sequence:
- condition: state
entity_id: media_player.living_room_tv
state: ''
- service: media_player.turn_off
data:
entity_id: media_player.living_room_tv
- service: homeassistant.turn_off
data:
entity_id: input_boolean.tv
automation:
- alias: 'Update TV status in Home Assisstant'
initial_state: 'on'
trigger:
- platform: state
entity_id: media_player.living_room_tv
action:
- service_template: "homeassistant.turn_{{ 'on' if trigger.to_state.state == 'on' else 'off' }}"
data:
entity_id: input_boolean.tv
So, I guess one action would be to change the line - service_template: "homeassistant.turn_{{ 'on' if trigger.to_state.state == 'playing' else 'off' }}"
Not sure it will fix all issues, I have not used the service_template before.
It’s a good lesson I had to learn, too.
Looking at the dev-state page to see what a state is actually called rather than assuming what it should be - especially ‘on’ vs. ‘On’ and ‘True’ vs. ‘true’ tripped me up multiple times
I’ll ask again, you state you are using a remote-control to turn the TV on and off. When the TV changes state how is it reporting its state-change to Home Assistant?
Or are you using another device to monitor the commands transmitted by the remote-control?
Or does the remote-control also reporting its transmitted commands to Home Assistant?
It seems like he’s using an input_boolean to record the state; the input_boolean is changed by the automation above, i.e. if the state of the media player changes to playing the input_boolean is changed to on so the state of the the can be tracked…
I use this kind of ‘detour’ for some of my no-so-smart switches as well; not ideal, but if e.g. my security camera is switching to from ‘not_home’ to ‘home’ it means that the rf-controlled switch that provides or cuts power to the camera is set to on.
How does Home Assistant detect the remote-control’s operation that changes the TV’s state?
The first post states using a remote-control (IR? Bluetooth? WiFi?) to turn on the TV (and the automation fails to trigger on it). How does Home Assistant know the remote-control just transmitted a command to turn on the TV?
Does the TV report its status to Home Assistant? If it does, then why would an input_boolean be needed?
It stands to reason that Home Assistant needs to detect the operation of a remote-control (or to receive status from the controlled device) in order to trigger an automation designed to detect that operation.
It doesn’t need to - that’s what the state of the media_player.living_room_tv is for.
If the media_player changes to playing it is an indicator that the TV is on and this gets recorded in the input_boolean which holds the state of the TV.