Command line switch icon

I have a command line switch which works fine, but I want to use a specific icon.
My attempt to use a icon_template seems to do nothing; help appreciated…

switch:
  - platform: command_line
    switches:
# Switch to disconnect battery 
# Off (0) normal
# On  (1) battery disconnected
  #   icon: mdi:connection
      battery_connect:
        command_on: /bin/bash -c "echo 0 >> /sys/class/power_supply/bq24190-charger/f_batfet_disable"
        command_off: /bin/bash -c "echo 1 >> /sys/class/power_supply/bq24190-charger/f_batfet_disable"
        icon_template: >-
          {%- if is_state('switch.battery_connect', 'on')  -%}
            mdi:electric-switch
          {%- else  -%}
            mdi:electric-switch-closed  
          {%- endif -%} 

Are you sure icon-template is supported?
I don’t see it here, but maybe I am looking at the wrong place :arrow_down:

The command line switch documentation is here.

(I don’t know how to include the reference like you did.)

Icon_template is mentioned…

check a bit further down

        icon_template: >
          {% if value_json.config.on == true %} mdi:toggle-switch
          {% else %} mdi:toggle-switch-off
          {% endif %}

I tried it, but doen’t seem to do anything. I looked at it before and didn’t understand the value_json stuff

heres what I tried


switch:
  - platform: command_line
    switches:
# Switch to disconnect battery 
# Off (0) normal
# On  (1) battery disconnected
      battery_connect
        command_on: /bin/bash -c "echo 0 >> /sys/class/power_supply/bq24190-charger/f_batfet_disable"
        command_off: /bin/bash -c "echo 1 >> /sys/class/power_supply/bq24190-charger/f_batfet_disable"
        value_template: >
          {{value_json.config.on}}
        icon_template: >
          {% if value_json.config.on == true %} mdi:toggle-switch
          {% else %} mdi:toggle-switch-off
          {% endif %}

Does your template work within the template editor at the developer tools of HA?

I have no experience with the specific switch config, but perhaps you could monitor the switch.battery_connect states somehow to see what the actual states are in the different situations.

You need a command_state in order to use value_template and icon_template. It will be a command that’s executed based on the polling frequency that queries the power supply.

If you don’t care about that and you just want to assume the command went through, then all you need is

switch:
  - platform: command_line
    switches:
      battery_connect:
        command_on: >
          /bin/bash -c "echo 0 >> /sys/class/power_supply/bq24190-charger/f_batfet_disable"
        command_off: >
          /bin/bash -c "echo 1 >> /sys/class/power_supply/bq24190-charger/f_batfet_disable"

This will assume it’s on when you send the on command and off when you send the off command.


As a side note, your YAML was invalid because you were missing a colon after battery_connect.

thanks
sorry for the missing colon.

my objective is to have the switch change icons depending on the state.

the current on or off is a bit confusing as on means battery is disconnected,
I can change this, but it would be nice to have the icon changing.

The only way you can have an icon_template is if you have a way to query the state. If there’s a command that returns a string that tells you if the battery is on or off, then you can use that as command_state then build a value_template and icon_template based on the state.

command_on is the command that’s sent to turn on the battery
command_off is the command that’s sent to turn off the battery

If those commands don’t make sense, then use different commands. You’re in control

Thanks everybody.
Here is the solution to my problem
The command on/off sends instructions
The command_state reads the value
The value_template extracts true
The icon_template changes the icon

switch:
  - platform: command_line
    switches:
# Switch to disconnect battery 
# On   (0) normal
# Off  (1) battery disconnected
      battery_connect:
        command_on: /bin/bash -c "echo 0 >> /sys/class/power_supply/bq24190-charger/f_batfet_disable"
        command_off: /bin/bash -c "echo 1 >> /sys/class/power_supply/bq24190-charger/f_batfet_disable"
        command_state: /bin/bash -c "cat /sys/class/power_supply/bq24190-charger/f_batfet_disable"
        value_template: '{{ value == "0" }}'
        icon_template: >
          {% if value == "0" %} mdi:electric-switch-closed
          {% else %} mdi:electric-switch
          {% endif %}

This is the result:


The only down point is, after a restart, the icon goes back to a standard switch. But once the switch is activated, the new icons are used.

2 Likes