Hello,
I’ve been banging my head against the wall for days so it’s time to ask for help. I have searched in forums and HA documentation yet I cannot find a solution. I don’t know how much detail is needed so I’ll describe my goal and my problem in the next paragraph, and will then follow with more details which may or may not be relevant - feel free to skip if it’s too much information.
Goal
I have a TV with audio output connected to a stereo system. I want to set up the stereo to come on when the TV is powered on, and to power off when the TV is powered off. I have two binary sensors in HA that indicate whether a TV and stereo system are on/off. I also have a momentary switch which toggles the power button on the stereo system (It’s an old stereo - I have literally soldered two wires to the PCB and have an ESP8266 running ESPHome with a relay physically short the wires mimicking the physical power button). I have a simple automation to toggle the stereo when TV power changes.
Problem
Now this works most of the time however the problem is that those power sensors are not instant, and neither is the stereo power toggle. As a result if I turn the TV on and off quickly the stereo powers on but does not power off and vice versa - if I turn the TV off and then on again the stereo will power down but will not power on again.
What I would like to do is to come up with a toggle switch for the stereo - after all I have a sensor which reflects it’s power state and a momentary switch to power it on/off. I have tried to build a template switch but no matter what I try it doesn’t work like a toggle switch but acts like a momentary switch.
Why this matters to me
One solution would be to set up another automation on a timer loop to check the states every 5s or so and, if they do not match, to toggle the stereo. While it would work I feel this is not a very elegant solution. Moreover, I have some other similar but more complex scenarios where that approach wouldn’t work. Finally, while this may be trivial, part of me thinks that constant timer loops with delays can be resource intensive although I might be making this up - if anyone knows more about how this stuff works under the hood let me know.
Details on my sensors / switch setup
Both my TV and stereo system have smart Wi-Fi power switches from Sonoff which measure power consumption. They are ESP8266 based so I have flashed them with ESPHome and they report almost instant power consumption of my devices (there’s maybe 500ms lag). In HA I have two binary template sensors which turn on when the power consumption goes above a certain threshold:
template:
- binary_sensor:
name: TV power state
state: '{% if states(''sensor.tv_power'') | float > 10 %} on {% endif %}'
delay_on: '00:00:03'
- binary_sensor:
name: Stereo power state
state: '{% if states(''sensor.stereo_power'') | float > 10 %} on {% endif %}'
(There’s a 3s delay_on for the TV because the power consumption occasionally spikes momentarily which then triggers the stereo to switch on. I could potentially reduce this but I cannot eliminate it. The stereo sensor is actually instant however there is a delay in the actual stereo when being power on and off - even using the physical switch it takes about 3 seconds to power on/off - it shows greetings and goodbyes on the screen, and other stupid animations…)
The stereo switch is actually a relay soldered directly to the PCB of the stereo which mimics the press of a physical power button. It is controlled by an ESP8266 flashed with ESPHome and the following code - this is how ESPHome documentation advises to build a momentary switch:
switch:
- platform: gpio
pin: 5
id: relay
name: "Stereo switch"
icon: "mdi:power"
on_turn_on:
- delay: 500ms
- switch.turn_off: relay
The problem I have here is that this actually creates a toggle switch in HA. The toggle is normally off, it goes on when clicked and goes back off after 500ms.
You can see all above entities in the screenshot below where the stereo switch is this momentary power switch from ESPHome.
Questions
- How can I create a toggle switch to power my stereo system using the binary power sensor and the momentary switch? I’m obviously open to changing the code for the sensors and even the ESP switch on the stereo.
- How can I tie the states of my TV power and stereo power together? I.e. How can I make sure that the stereo will always power on when the TV is switched on, and then back off when the TV is switched off?
- Also, is there a way to basically tie the two states (like, for example, what you do in HVAC systems in USA where you want the fan to physically bound to the heater so it ALWAYS comes on when the heating is on) so that they would work both ways i.e. the TV would also come on if I power the stereo instead (this would be the opposite of what I’m trying to achieve here but I need this for other setups). It’s not exactly a smart TV but I have an Nvidia Shield player which can power it on/off via HDMI so I do have a way to power it on/off from HA).
Thanks for bearing and me and I welcome any suggestions.