Simple automation trigger and script

Hi,

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

When you use the remote-control to turn on the TV, how is Home Assistant informed of the TV’s state-change?

I am using this automation to change the boolean state…but it is not working…

    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

Is the state of your media_player.living_room_tv really ‘on’ when it’s on - or is it ‘playing’?

It is ‘playing’…

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.

I think that it is working now… Thanks for the help!!

1 Like

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 :wink:

1 Like

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.

How does media_player.living_room_tv receive the TV’s current state?

If you tell me thre’s a platform involved in order to communicate with the TV then it obviates the need for an input_boolean.