Integrating USB-controlled power outlet using command line tools

Hello,

As my first non-trival project, I have a switchable legacy power outlet connected by USB to my Home Assistant server. I would like to integrate it in the Home Assistant frontend so I can use it to control the state of the power outlet.

I have a working command line tool on the server which has the functions toggling the on/off state and querying the current state (/home/homeassistant/bin/toggletv.sh -s currently prints “on” and “off” respectively).

Reading some documentation and forum entries, I arrived at below addition to my configuration.yaml .

The input_boolean is visible in the frontend and can indeed switch the connected TV on and off.

However, I would like to achieve that the status of the input_boolean symbol in the frontend is updated whenever the state changes. This can happen independently of input_boolean and, therefore, would need to be based on the status query. Currently, my first attempt in the direction, binary_sensor, does not appear to work. At least its status is shown as “Unknown” since more than 12 hours.

automation toggle_tv:
  - alias: "Toggle TV"
    trigger:
      platform: state
      entity_id: input_boolean.toggle_tv
    action:
      service: shell_command.sispmctl_toggle_tv

input_boolean:
  toggle_tv:
    name: Toggle TV

binary_sensor:
  - platform: command_line
    name: Sispmctl TV Power Status
    command: '/home/homeassistant/bin/toggletv.sh -s'

shell_command:
  sispmctl_toggle_tv: '/home/homeassistant/bin/toggletv.sh'

Reading some more documentation, I found that this is much more obvious to achieve with switch (changing slightly the behavior of the command line tool):

switch:
  - platform: command_line
    switches:
      tv:
        unique_id: sispmctl_tv
        friendly_name: TV Power
        command_on: '/home/homeassistant/bin/toggletv.sh -o'
        command_off: '/home/homeassistant/bin/toggletv.sh -f'
        command_state: '/home/homeassistant/bin/toggletv.sh'
        icon_template: >
          {% if value_json.config.on == true %} mdi:toggle-switch
          {% else %} mdi:toggle-switch-off
          {% endif %}

Strangely, I find that scan_interval seems unsupported in contrast to what the documentation says Command Line - Home Assistant .

Also, it is less obvious to me know how to use the switch in an automation. I would like add a configurable timeout which will switch the TV off if that function is active.