Switch template not changing value after being turned on

Hi I have created following switch template to use Logitech Harmony to turn on and off my AC:

switch:
  - platform: template
    switches:
      klima:
        value_template: "{{ is_state('remote.harmony_hub', 'on') }}"
        turn_on:
          service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            device: 55292816
            command: 'On/Off'
        turn_off:
          service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            device:  55292816
            command: 'Off'

Issue is, whenever i trigger this, the state of the switch does not change from Off to On, and once clicked again, it is sending the On command instead of Off.

What am I missing here?

Thats just checking if the harmony hub is in an OFF or ON status.
So its ON whenever you use any of the Activities and is OFF when you press the OFF button on the remote.
You’ll need to check the device if its on. Not the HUB itself.

OK, my understanding was that switch will automatically change itself to On value, once activated from Off value. In such case should I use something different? Or should I add automation which will update the value of the switch entity once the switch was triggered?

No, the template is looking at the Harmony HUB not the device itself. Unless you start an activity it will always show as OFF. And you can only get a status on which activity is active, not which device.

Also looking at your code you are sending an ‘On/Off’ for the turn_on command.

The On/off is correct since thats how the harmony has added the specific activity from its base of devices, which worked only for On command, then I manually added Off command by reading the AC units remote. There is no way to get the state info from device itself, so a workaround is needed. Thus, my question if I should make automation to chenge the entity state in HA.

I’m not 100% sure and cannot test this atm. But i think when u send a command via the hub it will not change its state to ON, it will only do so if an activity is started.

If you are using the Hub only for the AC then you could do it with the activity and it change its state.
Since when an activity is active it will show “ON” on its state

Yeah so now I am thinging that the code will look like so:

switch:
  - platform: template
    switches:
      klima:
        value_template: "{{ is_state('switch.klima', 'on') }}"
        turn_on:
          service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            device: 55292816
            command: 'On/Off'
        turn_off:
          service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            device:  55292816
            command: 'Off'

and I will create automation triggered by the event of activating switch, which will then change the value of the state of swich.klima from Off to On. Then on the another activation of the switch it will change the state to Off

No, that’s not how this works.

The value_template drives which service is used turn_on or turn_off. It also drives the state of the switch. Referencing it self will do nothing as a state has never been reached.

If you want to simulate the device turning on and off because the device does not report a state, then you need to use something to store the state. An input boolean can do that.

The input boolean would be turned on in the turn_on section (you’d add it before or after your existing service), and turned off in the turn_off section. And your value_template would reference the input_boolean.

Just add input_boolean.klima to your helpers, then the following switch template.

switch:
  - platform: template
    switches:
      klima:
        value_template: "{{ is_state('input_boolean.klima', 'on') }}"
        turn_on:
        - service: input_boolean.turn_on
          target:
            entity_id: input_boolean.klima
        - service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            device: 55292816
            command: 'On/Off'
        turn_off:
        - service: input_boolean.turn_off
          target:
            entity_id: input_boolean.klima
        - service: remote.send_command
          data:
            entity_id: remote.harmony_hub
            device:  55292816
            command: 'Off'

Keep in mind that this can get out of sync depending on when you retart HA and toggle the power to the device.

create a input boolean helper

Open your Home Assistant instance and show your helper entities.

Yeah, figured out I can just google it instead of taking your time. Thank you very much. Works like a charm :slight_smile: