Make IR tv smarter

Hello,

I’m thinking to do something in Home Assistant with my old tv (not smart), Logitech Harmony and a power reading switch but i don’t know if it’s possible and if it is possible where to begin!
Maybe someone can point me in the right direction.

What i am trying to archieve:
A switch in Home Assistant that changes state by looking at the power readings from the switch.
Above 100watt: set switch to “on” state
Below 1watt: set switch to “off” state

Turn tv “on” in HA: fire the PowerToggle command from Harmony to the tv.
Turn tv “off” in HA: same as above

Is this even possible?
If this works i can still use the tv remote, and use the state of the tv switch in my other automations…

Thanks!

I would opt for a MCU (ESP, Arduino or the like).
The simplest thing might be to sense the current consumption by a current transformer which will indicate that the TVset is turned on. Another way, if not together, to capture the IR commands and inform HA about which operation is taking place.

With the help of MCU library, I presume the matter might be solved in a couple of days.

I can already measure the current consumption with a tplink hs110.
Also know the IR command to execute the IR power toggle with the Logitech Harmony.

MCU Library?
What is that?
If i google the name it takes me to a Chinese website.

Thanks for the reply

MCU = Micro Controller Unit
For ATmega >> Arduino Playground - HomePage
For ESP >> GitHub - crankyoldgit/IRremoteESP8266: Infrared remote library for ESP8266/ESP32: send and receive infrared signals with multiple protocols. Based on: https://github.com/shirriff/Arduino-IRremote/
But I see you already got the tools to solve the matter. It might take to set the proper service on HA.

Take a look at this custom component.

And here is another solution to get the state of your TV if it has an USB port.

Exactly, the Harmony and the power reading switch is enough isn’t it?
I think it is possible with a template switch?
Something like this?
How do i define the state at the value_template?
That has to be: above 100w…

switch:
  - platform: template
    switches:
      tv_ir_switch:
        value_template: "{{ is_state('sensor.skylight', 'on') }}"
        turn_on:
          service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            command:
              - PowerToggle
            device: 59082838
        turn_off:
          service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            command:
              - PowerToggle
            device: 59082838

Thanks

I think it is possible with the hardware i allready use insn’t it?

Yes indeed it is.

You’ll need to set up the HS110 via integration or configuration.yaml as described in the docs, and use a template on your TV switch to set the state of the TV switch. I use the same method to detect if my IR controlled AC unit is on or off.

Contents

switch:
  - platform: command_line
    switches:
      aircon:
        command_on: 'irsend send_once aircon power'
        command_off: 'irsend send_once aircon power'
        friendly_name: 'Air Con'
        command_state: 'true'
        value_template: "{{ states('sensor.tp_link_acplug_watts') | int > 50 }}"

This should get you going. If you need any further pointers please let me know.

Edit: I forgot to add I have a sensor set to extract the usage in watts from the HS110: You’ll need this to update the switch described above:

sensor:
  platform: template
  sensors:
    tp_link_acplug_watts:
      value_template: '{{ states.switch.ac_plug.attributes["current_power_w"] | replace(" W", "") | float }}'
      unit_of_measurement: 'W'

After the “command_on:” you put the ir code to turn the power on?
Are you using Harmony?
I have to send commands like this:

    - service: remote.send_command
      data:
        entity_id: remote.harmony_hub
        command:
          - PowerToggle
        device: 59082838

How do i stick this in the code above?

The power extraction works fine on the hs110.

Thank you

No I’m not using Harmony, I’m using LIRC and IR codes from the remote so I’m afraid I can’t help you with that. Best of luck figuring it out! :slight_smile:

Haha, thanks, will post the solution in this topic if i find one…
There have to be more people who want to do this.

input_boolean:
  tv_power:
    name: TV Power
    icon: mdi:power
automation:
- alias: 'TV Power'
  trigger:
    platform: state
    entity_id: input_boolean.tv_power
    value_template: "{{ state.sensor.switch_hs110_1_watts }}"
    above: 80
    below: 120
  action:
    service: remote.send_command
    data_template:
      entity_id: remote.harmony_hub
      device: 53690681
      command: PowerOff

With the settings above the harmony is shooting commands to the tv to turn it on and off via IR.
The only problem is the value_template.
It’s nog working, when i check the configuration it says:

Invalid config for [automation]: [value_template] is an invalid option for [automation]. Check: automation->trigger->0->value_template. (See /config/configuration.yaml, line 194). Please check the docs at https://home-assistant.io/components/automation/

It says value_template is invalid on automations, but on this page:


they are using the value_template to in an automation…

What am i doing wrong?

Changed the platform from “state” to “numeric_state”, the error in the config check is now gone.
But the booleon switch is not working any more.

When i execute the automation i still send the ir command to the tv so the that part is still working…

Hope someone can help a newbie out :slight_smile:

You’re using two entities in your trigger, which will not work.
Put your template in a condition.

automation:
- alias: 'TV Power'
  trigger:
    platform: state
    entity_id: input_boolean.tv_power
    to: 'off'
  condition:
    - condition: template
      value_template: "{{ 80 < states('sensor.switch_hs110_1_watts') | int < 120 }}"
  action:
    service: remote.send_command
    data_template:
      entity_id: remote.harmony_hub
      device: 53690681
      command: PowerOff

The state of the switch does not change when the watt consumption changes.

This is how it switch has to act:
80 to 120w is on
another wattage is off (below 1 watt is oke)

When i press the switch when the tv is on (±100w) the switch is responding, the Logitec is shooting the IR command to turn the tv off.
The switch is nog working when the tv is off (below 1w), there isn’t happening anything then, the harmony does no respond…

Thanks for the help!

Sorry, i only corrected the automation from your last post and didn’t read the whole topic.
Your switch from post 6 should work with the right template.

switch:
  - platform: template
    switches:
      tv_ir_switch:
        value_template: "{{ 80 < states('sensor.switch_hs110_1_watts') | int < 120 }}"
        turn_on:
          service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            command:
              - PowerToggle
            device: 59082838
        turn_off:
          service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            command:
              - PowerToggle
            device: 59082838

You can check, and play with the template when you copy the part
{{ 80 < states('sensor.switch_hs110_1_watts') | int < 120 }}
in Dev Tools/templates.

Works like a charm! Tnx for the help!! :smiley:

It’s “almost” working perfectly.
There is still one thing not working wel, i’ll explain:

When the tv is already off and i use the switch.turn_off option in an automation, my tv is turned on.
So the automation is fireing the turn off command again, even if the switch is already off.

How do i disable that?
The turn_off command for this switch is the same command as the turn_on command, the PowerToggle command from the harmony…

When tv tv switch is already off and the switch.turn_off is used this switch don’t has the do anything. I think it’s done with an automation?

Hello Roelofk85,
does your TV happen to have a free USB port? You could program and connect a Wemos D1 mini, or something similar. If the TV is on, announces the D1 in the Wi-Fi, you can then query the status via “Ping” and then know if the TV is “on” or “off” …
Have you looked at “Smart IR” ?