State aware virtual switch

Hello, I’m new to home assistant.

I’ve set up matter bridge to share my home assistant devices over matter with my nest hub for local control. I also have a harmony hub and have discovered that you can’t just expose the harmony hub activities or remote entity over matter bridge. I have managed to create a toggle input Boolean and automations to create a virtual switch and can share this over matter bridge to my nest hub to run and shutdown the harmony hub activity to turn my tv on and off.

The question I have though regarding what I’ve achieved so far and would like to achieve is regarding virtual switches like the input Boolean toggle or other possible options where the virtual switch syncs with the physical state of the device it is intended to control. So if I were to activate the harmony hub through the harmony app, at the moment the input Boolean toggle isn’t aware this activity is running on the harmony hub although the harmony hub remote and activities entities in home assistant are aware and reflect the real world state. Is it possible to have the virtual switch/input Boolean or some other option that would trigger an action from within home assistant, but also update its status if the same action was triggered outside of home assistant?

You want a template switch. The state template informs home assistant of the state of the switch for just this case - when the switch is changed outside home assistant:

1 Like

Thanks for the advice, with these template switches does the intended action of the switch input only trigger when the switch is manually toggled from within home assistant?

If another automation fires within Home Assistant and that updates the switch position, it will fire the actions associated with the template switch.

As an example, I use Template Switches with the generic thermostat, so that I can run different actions when (the call for) heating is turned on or off.

That depends on how you define “manually”… The turn_on and turn_off action sequences only run when there is a switch.turn_on or switch.turn_off action called on the entity. That applies to actions from the dashboard, automations, scripts, etc.

A change in the switch’s state based on the state template will not cause either action sequence to run.

1 Like

You should probably take a look at the documentation for MQTT switches:

To use them there needs to be an external entity:

  • Zigbee2MQTT
  • A switch that natively speaks MQTT
  • Your own code

However the actual implementation use two queues:

  • A command queue which only updates when an automation or human changes the state of the switch.
  • A state queue which the external entity uses to send updates to the state from outside.

It’s probably more work than you want/need to do right now (given you are new to HA), but its useful to know about when you get further down the line.

Thanks to all that provided me advice. I figured out how to achieve what I was after using Boolean input and automations.

I have then been experimenting with the switch templates and have successfully got this one below working

template:

  • switch:
    • name: Harmony Remote
      state: “{{ is_state_attr(‘remote.harmony’, ‘current_activity’, ‘Watch TV’) }}”
      turn_on:
      action: switch.turn_on
      target:
      entity_id: remote.harmony
      turn_off:
      action: switch.turn_off
      target:
      entity_id: remote.harmony

The question I have now is that this switch only displays the entity state being on or off in line with the linked entity, but when you toggle this template switch it doesn’t trigger a state change of the entity it is linked with. Do I achieve this with an automation like I did with the boolean input, or can this be done in the code of the switch template itself?

Most of the templates are designed to have two channels:

  • A status channel - the template which reads the state.
  • A command channel - the actions to execute when the item is changed.

An example might be a garage door where you send a signal to move it and there are contact sensors to read if its open or closed.

If the problem you are trying to solve doesn’t have a way to check the status, its usually best to just use an input (boolean, select, etc) that way HA remembers the state and you use an automation to sends updates when you change something.

It doesn’t trigger because remote.harmony is not a switch.

Your config should be

template:
  - switch:
      - name: Watch TV
        state: "{{ is_state_attr('remote.harmony', 'current_activity', 'Watch TV') }}"
        turn_on:
          action: remote.turn_on
          target:
            entity_id: remote.harmony
          data:
            activity: Watch TV
        turn_off:
          action: remote.turn_on
          target:
            entity_id: remote.harmony
          data:
            activity: PowerOff

This post also provides a script that will create all your switches for you. Just copy/paste the result into configuration.yaml

Thanks petro your advice got it working, I ended up just changing the action part of my template to remote.turn_on and remote.turn_off from switch.turn_on/off and didn’t need to add the data or activity points. I’m finally starting to get my head around these templates. This is just what I was after a state aware switch that triggers the intended action and syncs with external to HA triggered state changes.

I’ve even just figured out how to add a customised icon that changes based on state also.

Another question for anyone that might know. I have seen a different method to create template switches is through the UI and not the configuration yaml.
You go settings>devices & services>helpers>create helper>template>switch

I’ve not been able to figure out how to get this to work. For the example I’ve provided for the harmony remote, can anyone guide me through how you configure a switch this way? The home assistant documents don’t explain this at all and only explain the configuration yaml method.

You just transcribe the values from the YAML config into the fields that match their keys… So what your config shows after state: goes in the “State” field, and what follows name: goes in “Name”… You use the “Add action” selectors in their respective areas to recreate the list of action objects that follow turn_on and turn_off.

The “Select device” and “Advanced options” fields are optional.

Keeping in mind that single line state: templates do not need to be quoted if using the UI. So just copy this from your yaml:

{{ is_state_attr('remote.harmony', 'current_activity', 'Watch TV') }}
1 Like

Thanks I can see where I was going wrong with this method. I was selecting a device and an action for the device to turn on and off, but now that I know the yaml configuration I can see I was supposed to select the remote turn off/on command and target the harmony remote. Got it working this way also now. Thanks for your help.

Just for a bit more understanding, if you use the UI templating method instead of modifying the configuration yaml, where would this code be reflected in home assistant? I thought it would end up in configuration yaml like the more manual method, as automations end up in their own automations yaml

It’s in a hidden file because it’s not meant to be user-modified.