I’m using a current sensor and notice the TV consistently uses above 20W of power when on. This is only needed because my TV the gets toggled on and off using a remote and via an IR blaster connected to HA and frequently the status of my TV in HA is incorrect stemming from the use of the remote vs the IR blaster.
I tried the above, and while the Developer tab shows my sensor status correctly and I’m not getting errors, my switch’s status isn’t tracking with my sensor. Here’s my code:
My switch:
mqtt:
switch:
- name: "LG TV FR"
value_template: "{{ is_state_attr('switch.lg_tv_fr', states('sensor.lg_tv_fr_status'), 'on') }}"
command_topic: "cmnd/tasmota_1C1CE3/IRsend"
payload_on: '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF","DataLSB":"0x4FB08F7","Repeat":0}'
payload_off: '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF","DataLSB":"0x4FB08F7","Repeat":0}'
My sensor:
lg_tv_fr_status:
friendly_name: "LG TV FR Status"
value_template: "{{ 'on' if states('sensor.lg_power_plug_current_consumption') | float > 20 else 'off' }}"
My button:
cards:
- show_name: true
show_icon: true
type: button
entity: switch.lg_tv_fr
show_state: true
Any suggestions?
Solution: @Taras had a really creative solution: Combine MQTT Switch and Binary Sensor - #10 by 123
In English, an automation fires when the detected wattage from my sensor exceeds 20W or falls below 5W. That automation sends out a MQTT message “ON” if the wattage exceeds 20W, or “OFF” of the wattage falls below 5W. The MQTT switch listens on the state_topic for the MQTT message and compares the received value to my state_on and state_off conditions. As expected, the button action triggers the my IR device to turn the TV on and off, and the status of the TV is reported based on the value from my Kasa smart plug.
My switch in my switches.yaml file:
mqtt:
switch:
- name: "LG_TV_FR" #the entity name
state_topic: 'cmnd/tasmota_1C1CE3/lg'
state_on: 'ON'
state_off: 'OFF'
command_topic: "cmnd/tasmota_1C1CE3/IRsend"
payload_on: '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF","DataLSB":"0x4FB08F7","Repeat":0}'
payload_off: '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF","DataLSB":"0x4FB08F7","Repeat":0}'
An automation:
alias: MQTT_LG_TV
description: ""
triggers:
- entity_id:
- sensor.lg_power_plug_current_consumption
below: 5
trigger: numeric_state
- entity_id:
- sensor.lg_power_plug_current_consumption
trigger: numeric_state
above: 20
conditions: []
actions:
- choose:
- conditions:
- type: is_power
condition: device
device_id: 4e76f6a8511344758cca72e5dc232009
entity_id: 6e04531b3c64bd430164e2f0be0592a6
domain: sensor
below: 5
sequence:
- action: mqtt.publish
metadata: {}
data:
evaluate_payload: false
qos: 0
retain: true
topic: cmnd/tasmota_1C1CE3/lg
payload: "OFF"
- conditions:
- type: is_power
condition: device
device_id: 4e76f6a8511344758cca72e5dc232009
entity_id: 6e04531b3c64bd430164e2f0be0592a6
domain: sensor
above: 20
sequence:
- action: mqtt.publish
metadata: {}
data:
evaluate_payload: false
qos: 0
retain: true
topic: cmnd/tasmota_1C1CE3/lg
payload: "ON"
mode: single
hide_entity: true
And my button:
type: horizontal-stack
cards:
- show_name: true
show_icon: true
type: button
tap_action:
action: toggle
entity: switch.lg_tv_fr
show_state: true
title: LG TV Family Room