Command Line Question

Hi, I’m trying to use command line to create a switch that help me to turn on my Broadlink TC-2 which is a RF switch. I had written a script to turn on or off the TC-2 and it work. Now I want to create a virtual switch so I can use in my Floor Plan as a normal switch that toggle and also change the floor plan picture to bright or dark as per the state of the switch. My config as below :

switch:
  - platform: command_line
    switches:
      wardrobe:
        command_on: script.power_on_wardrobe
        command_off: script.power_off_wardrobe 

I added the switch in Lovelace UI. When I press the switch, nothing happen. Appreciate some help.

I’ve never used this before but according to the docs

you have to put the actual command line command in command_on and command_off.

I actually tried both with and without the switch_command but does not work.

switch:
  - platform: command_line
    switches:
      wardrobe:
        command_on: switch_command script.power_on_wardrobe
        command_off: switch_command script.power_off_wardrobe
 

What is in your scripts?

power_off_wardrobe:
  alias: power_off_wardrobe
  mode: single
  sequence:
  - data:
      command: Turn Off Wardrobe Light
      delay_secs: 0.75
      device: wardrobe
      hold_secs: 2.5
      num_repeats: 1
    entity_id: remote.rm_pro_3_remote
    service: remote.send_command
power_on_wardrobe:
  alias: power_on_wardrobe
  mode: single
  sequence:
  - data:
      command: Turn On Wardrobe Light
      delay_secs: 0.75
      device: wardrobe
      hold_secs: 2.5
      num_repeats: 1
    entity_id: remote.rm_pro_3_remote
    service: remote.send_command

I also noticed the switch turn back to off state after a few seconds after I turn it on.

You need to use the template switch integration not the command line switch.

Is this possible with Scripts? Where to insert the scripts? Inside the value of the template? What put in entity_id? Sorry, I’m very new to HA. Just 2 weeks into this.

No, use the turn_on and turn_off actions. Like this (scripts are services):

switch:
  - platform: template
    switches:
      wardrobe:
        friendly_name: Wardrobe Light
        turn_on:
          service: script.power_on_wardrobe
        turn_off:
          service: script.power_off_wardrobe

The value_template is used for feedback to home assistant to tell it the state of the switch. Without this you will have two buttons, one to turn the switch on, one to turn it off, rather than a toggle switch.

You can use customize to change the icon if you wish.

Thanks for your help. I make the changes as this

switch:
  - platform: template
    switches:
      wardrobe:
        value_template: "{{ is_state('switch.wardrobe', 'on') }}"
        friendly_name: Wardrobe Light
        turn_on:
          service: script.power_on_wardrobe
        turn_off:
          service: script.power_off_wardrobe 

However, I noticed the switch will toggle back to off after I on it. Is this due to script?

I don’t think self referencing like this to obtain the state will work.

Consider this:

The switch state is off and the Lovelace button reflects this state.
You tell the switch to turn on by toggling the Lovelace button and the script.power_on_wardrobe script is called.
Home Assistant checks the value_template to see what the state of the switch is.
The switch state is still off (as the value template has not updated).
So the Lovelace switch button turns off again.

You could turn on/off an input_boolean in your on/off scripts and check that state in your value_template.

However this will get out of sync if you have any other method to control the wardrobe light other than the scripts (a physical remote control for example).

If this is likely to be the case your only option is to remove the value template so Lovelace has separate on/off buttons and no state representation for the wardrobe switch.

Unless you have a way of actually getting the state of the switch back into home assistant.

Ok. I understood now. Will add input boolean in the script. As there is no way I can get the state from the switch because this is RF switch, I will hv to live with the possibility of out of sync. Thanks for your help.

1 Like

I added the input boolean in the script as below:

power_off_wardrobe:
  alias: power_off_wardrobe
  mode: single
  sequence:
  - data:
      command: Turn Off Wardrobe Light
      delay_secs: 0.75
      device: wardrobe
      hold_secs: 2.5
      num_repeats: 1
    entity_id: remote.rm_pro_3_remote
    service: remote.send_command
  - service: input_boolean.turn_off
    data:
      entity_id: wardrobelightstatus
input_boolean:
  wardrobelightstatus:
      
switch:
  - platform: template
    switches:
      wardrobe:
        value_template: "{{ is_state('input_boolean.wardrobelightstatus', 'on') }}"
        friendly_name: Wardrobe Light
        turn_on:
          service: script.power_on_wardrobe
        turn_off:
          service: script.power_off_wardrobe

Although the switch works but I received the following error:
Wardrobe Light: Error executing script. Invalid data for call_service at pos 1: not a valid value for dictionary value @ data[‘entity_id’]

8:46:17 AM – helpers/script.py (ERROR) - message first occurred at September 19, 2020, 10:04:33 PM and shows up 12 times

power_off_wardrobe: Error executing script. Invalid data for call_service at pos 2: not a valid value for dictionary value @ data[‘entity_id’]

8:46:17 AM – Script (ERROR) - message first occurred at September 19, 2020, 10:04:39 PM and shows up 6 times

power_on_wardrobe: Error executing script. Invalid data for call_service at pos 2: not a valid value for dictionary value @ data[‘entity_id’]

8:46:12 AM – Script (ERROR) - message first occurred at September 19, 2020, 10:04:33 PM and shows up 6 times

Appreciate some help.

Nevermind. I found the problem. The entity id is wrong. Miss out the input_boolean. Thanks for all the help.

1 Like