Help needed with template sensor automation

Can someone smarter than me help me with an automation that does not seem to trigger. It is supposed to look for a sensor status and if it is true perform an action. it checks good in the config checker and the automation template returns true in the template editor. I have included the sensor code and the template code below. If manually activated this automation turns the light off/on it just does not seem to trigger on the sensor state change.
Sensor:

  • platform: template
    sensors:
    glances:
    friendly_name: “HTPC”
    value_template: >-
    {% if is_state(‘media_player.living_room_tv’, ‘off’) %}
    Off
    {% elif is_state(‘sensor.htpc_idle’, ‘Idle’) and is_state(‘media_player.living_room_tv’, ‘idle’) %}
    Idle
    {% else %}
    In Use
    {% endif %}

Automation

  • alias: Turn Off TV Lights when TV is off
    trigger:

    • platform: template
      value_template: “{% if is_state(‘media_player.living_room_tv’, ‘off’) %}true{% else %}false{% endif %}”
      action:
    • service: homeassistant.turn_off
      entity_id: switch.tv_lights_power
  • alias: Turn On TV Lights when TV is Idle/In Use
    trigger:

    • platform: template
      value_template: >-
      {% if is_state(‘sensor.glances’, ‘Idle’) %}
      true
      {% elif is_state(‘sensor.glances’, ‘In Use’) %}
      true
      {% else %}
      false
      {% endif %}
      action:
    • service: homeassistant.turn_on
      entity_id: switch.tv_lights_power

I’m sorry if this is the wrong area to ask for help.

Without formatting it’s impossible to say if your spacing is correct.

First observation - your automation “Turn off TV lights when TV is off” is overly complicated by using a value template.

alias: Turn Off TV Lights when TV is off
trigger:
  platform: state
  entity_id: media_player.living_room_tv
  to: 'off'
action:
  service: homeassistant.turn_off
  entity_id: switch.tv_lights_power

Is exactly the same.

Second observation - The template for your second automation is overly complicated as the glances sensor only has 3 states, and you want to evaluate if it is not ‘off’, therefore…

alias: Turn On TV Lights when TV is Idle/In Use
trigger:
  platform: template
  value_template: "{{ not is_state('sensor.glances' , 'Off') }}"
action:
  service: homeassistant.turn_on
  entity_id: switch.tv_lights_power

Would be more accurate.

Thank you for your help, I ended up getting what I want out of 1 automation using the folllowing.

- alias: Match TV Lights to HTPC State
trigger:
 - platform: state
   entity_id: sensor.glances
action:
  - service_template: >-
      {% if trigger.to_state.state == "Idle" or trigger.to_state.state == "In Use"%}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    entity_id: switch.tv_lights_power