Performing switch/input_boolean "off action" when hass thinks it is already off. Broadlink

Hi all,

Pretty new to all this Hass stuff… so forgive me if I’m being silly.

Background:
I have a Broadlink RF/IR device which can send signals to turn on any RF/IR device. I have set up in home assistant all the switches for various buttons on my TV, AMP, TiVo box etc etc… I have also successfully set up Haaska so all my devices are accessible by Alexa and work with voice commands to my Echo Dot.
The way I did this was first create all the switches for buttons on various remotes, then create scenes for various tasks (EG: turn on TV switches on the TV, my Amp and sets the inputs of the TV and Amp). Then I made an input_boolean where the “on” state runs an automaton which activates the TV On scene, and the “off” state runs an automation which activates the TV Off scene.

Problem:
Eveything works fine, and I can say “Alexa, turn on the TV” which will turn on all associated devices. However a problem arises when devices have been turned on (or off) manually… not by Alexa/Hass. EG: when I come home from work and my wife has turned on the TV and other devices using the real remotes - Hass thinks that they are “Off” so when I say “Alexa, turn off the TV” nothing happens as the input_boolean that controls the TV on/off function is already in the “off” position.

I have some possible solutions that in my head would work, however I am not sure if it is possible in Hass and if it is, how to do it.

Solution 1:
Change input_boolean to two separate “lightnig bolt” buttons rather than a toggle switch. - When I first created the switches in Hass for the buttons on the remote they appeared in Hass as two lightning bolt buttons rather than a toggle switch. This allowed me to activate the On or Off state if it was already in that state. I tried configuring the input_boolean with assumed_state: true but that did not work. (As adding assumed_state: false to the original switch changed it from two lightning bolt buttons to one toggle switch)

Solution 2:
Add into my automation or scene a command to “virtually” change the input_boolean to on or off without activating the on/off command.
EG: when I want to turn/activate input_boolean off (when it is already in the off position) I first need to make it think that it is in the on position without activating the on command. Not sure if this can be done.

Any other ideas?? Here is my code, note I have only included one example of turning the TV on.

scene:
- name: TV On
  entities:
    switch.lg_tv_power: on
    switch.amp_power: on
    switch.lg_tv_hdmi1: on
- name: TV Off
  entities:
    switch.lg_tv_power: off
    switch.amp_power: off


input_boolean:
  tv:
    name: TV
    initial: off
    icon: mdi:television


automation:
- alias: Turn TV on
  trigger:
      platform: state
      entity_id: input_boolean.tv
      from: 'off'
      to: 'on'
  action:
      service: scene.turn_on
      entity_id: scene.tv_on
- alias: Turn TV off
  trigger:
      platform: state
      entity_id: input_boolean.tv
      from: 'on'
      to: 'off'
  action:
      service: scene.turn_on
      entity_id: scene.tv_off

I use 2 scripts to toggle on/off command. When use script via voice command there is only on command to enable these 2 scripts.

I use input_boolean to activate and deactivate either 1 of my 2 automations and also this input_boolean will toggle the status which automation is running. In voice command by on/off input_boolean will order which automation to run.

I think we have the same setup except I am using scenes and you are using scripts. The input boolean works as expected. But I think you maybe missed that if I manually turn on my devices (outside of home assistant) that the boolean controls then the boolean can not but turned off as it was never turned on in the first place.

The status is not up to date because manually turn on devices. So I use voice command to enable the script directly to turn off.

my input boolean is actually to check whether my family are home or away (normally this turn on as all home), 1st automation link to turn off put boolean when nobody home. 2nd automation is to turn on this input boolean when everybody home.

Then 3rd automation is camera motion to turn on if input boolean is on (nobody home). Then 4th automation is camera motion to turn off if input boolean is off (everyboday home), also in the 3 and 4 automation I have enable the alarm away by script and disable alarm by script not from the physical alarm panel.

So actually if my family turn on the physical alarm panel. I can use voice command to disarm by script, which not effect the input boolean.

A little bit of mix up explain hope you don’t mind and Sorry for my broken english language.

Ah… I think I understand what you mean.

If I understand correctly tho - a script can only be turned on - is that correct? (the same as a scene). If your alarm script is called “disarm alarm” you activate it by saying “turn on alarm disarm” So if I had a script called, for example: Turn TV Off. To activate this script I would have to say “Alexa, turn on Turn TV Off”? Which is quite unnatural to say. Scenes work the same way - you can only turn them on. I have the scenes below to turn the TV on and off:

scene:
- name: TV On
  entities:
    switch.lg_tv_power: on
    switch.amp_power: on
    switch.lg_tv_hdmi1: on
- name: TV Off
  entities:
    switch.lg_tv_power: off
    switch.amp_power: off

The first one is okay - I can say “Alexa, turn on TV On” and it will activate the first scene (I could remove the “on” from the scene name to make it better). However to activate the second scene I would have to say “Alexa, turn on TV Off” which is why I made the input_boolean. The purpose of the input_boolean is that I can say “turn on” and “turn off”

Just a thought. Could I use the Template Switch to run the Turn Off scene even if input_booleon was already set to off? I haven’t used these before and am a little confused…

Would something like this work? Not at home just yet so I can’t test it… but just wondering if something like this would trigger the off action of input_boolean if already off…

switch:
  platform: template
    switches:
        TV:
            value_template: "{{ is_state('input_boolean.tv', 'on') }}"
            turn_on:
              - service: input_boolean.turn_on
                entity_id: input_boolean.tv
            turn_off:
              - service: input_boolean.turn_off
                entity_id: input_boolean.tv

Just not sure about the value template - ‘on’ or ‘off’??

create a sensor that monitors the state of switch.lg_tv_power, switch.amp_power, switch.lg_tv_hdmi1:

tv:
  platform: template
  value_template: >
    {% if is_state('switch.lg_tv_power', 'on') or is_state('switch.lg_tv_power', 'on') or is_state('switch.lg_tv_power', 'on') %}
      on
    {% else %}
      off
    {% endif %}

Then make another automation that flips your input_boolean:

- alias: sync boolean on
  trigger:
    platform: state
    entity_id: sensor.tv
    from: off        
    to: on
  condition:
    condtion: state
    entity_id: input_boolean.tv
    state: off
  action:
    service: homeassistant.turn_on
    entity_id: input_boolean.tv

repeat that automation for off as well.

Or you can try the switch template you listed:

switch:
  platform: template
    switches:
        TV:
            value_template: "{{ is_state('input_boolean.tv', 'on') }}"
            turn_on:
              - service: input_boolean.turn_on
                entity_id: input_boolean.tv
            turn_off:
              - service: input_boolean.turn_off
                entity_id: input_boolean.tv

Thanks for the suggestion. Unfortunately that won’t work either. If the tv/amp/etc gets turned on manually (by a physical remote control) the switches (switch.lg_tv_power, etc) do not get turned on. So their state will remain off in HA even tho in reality the devices are on.

Basically I need to be able to trigger the off action on the input_boolean even if it is off (or on if it is on…). Is there any way to change the input_boolean from a toggle switch to 2 separate lightning bolts? as per image below (ignore the names… just showing example of “lightning bolts”) As these allow me to trigger the off state again even if HA thinks it is already off.

01

Either that, or another method which I don’t know of…

So I managed to find a solution to this.

I can forget about using input_boolean as this will not allow me to turn off if already in the off state.
I used the switch template and set the value_template to “false”. Therefore it is not looking at anything to change state - it is it’s own switch which I can customise.
I set it to turn my automations on - which was really just turning my scenes on… so I bypassed that and made the switch template directly control the scene. It still shows up as a toggle switch, so by adding “assumed_state: true” I was able to make it into a lightning bolt. Thus enabling me to trigger the off action whenever it is already off, and the on action when it is already on.

Here is my code:

#add this to configuration.yaml
    switch:
      - platform: template
        switches:
          television:
            value_template: "false"
            turn_on:
              - service: scene.turn_on
                entity_id: scene.tv_on
            turn_off:
              - service: scene.turn_on
                entity_id: scene.tv_off


   #add this to customize.yaml
    switch.television:
      friendly_name: Television
      icon: mdi:power
      assumed_state: true

I think I will substitute scenes for scrips so I can add some delays in… I can only manage to add one delay in a scene :confused:

Note: switch.template does not support “assumed_state” So you need to add it in to the customize,yaml. If you add it under the switch template in configuration.yaml you will get an error and switch.template will not load.

So there is no state even that occurs in HA when the device is turned on?

Correct. When using my original manufacturer’s TV remote (infra-red) to turn on the tv… it does nothing in HA - theres no way for HA to know I have sent an IR command to my TV. So I need the HA switches to give on/off commands even if the switches are already in that current state. But glad I got a solution that works :slight_smile: Thanks for the input tho :slight_smile:

Ah ok, yeah, avoid input_boolean then.

I use customize to change the way of display in frontend of toggle on/off or input_boolean on/off by put the assumed_state: true or false. You can try in customzie.

there is no correct state display in the frontend when we use physical remote. This is only the rf and ir remote.

If the physical remote or device has ip address then it show correct state no matter how we turn on and off.

It’s not exactly a solution for your current setup, but I use a smart plug that reports energy usage to tell if my TV is on or off. You could get one of those and use it to determine the proper state for your switch.

I even use it to turn my TV off by having it cut the power and then turn back on which causes my TV to turn off then back on in standby. That may not work for your TV though and an RF/IR device to control it is probably better anyway. I’ve been thinking about getting one to use as the remote and just leave the outlet always on to determine the correct state.

I know this is a bit old now, but I solved the same problem on my heater by using an input_select with states of ‘on’, ‘off’, and ‘unknown’ rather than an input_boolean. Then I have scripts to turn them to ‘on’ or ‘off’ via ‘unknown’ - for example the ‘off’ script first sets the select to ‘unknown’ and then to ‘off’. This solves the problem because changing from ‘off’ to ‘off’ does nothing, but changing from ‘off’ to ‘unknown’ to ‘off’ does something. So if the heater is manually turned on, but the RM2 still thinks it’s off, when I tell the Google Home to turn it off, it actually goes off.

However I no longer have this setup because I now have a tp-link switch also monitoring the heater power so I can detect when it is manually turned on or off based on power usage.

I have the same problem…best would be to have a way in the config to force to trigger the event…

Hello! I know I’m a bit late to the party… but I had the same issue and found a solution.

In case this helps anyone else, the solution for me was to create three actions on the automation that updated the state - the first to turn off the automation that sends out the state change via MQTT, then update the state, then turn back on the other automation!

There’s some more specific detail of my issue and solution on Reddit, here:

A bit late again… but. If your TV connected to local network (through wifi or ethernet) - use binary sensor with ping platform :wink:

I am very late to this discussion, but here is my 2 cents

I recently moved from an Ocelot controlling my x10 setup.  I was using x102mqtt.  I was required to create all my x10 devices through manual entries in the configuration.yaml file.  I was able to change from a toggle button to two decreet lightning bolts (on/off) by turning optimistic on or off.

Later I was attempting to use an input_bolean, but could not turn it on/off if out of sync, due to a local change of the device it was mirroring.'

I decided to make a switch entry in my configuration.yaml file for a virtual switch.  I then used this switch to mirror the real world device.  Now I can turn it off even if HA thinks it already off,  and on even if HA thinks its on.  Simply pubishing an mqtt message, with HA automation listening for the command.