How to change how a command line switch appears

So I’ve got an MQTT switch which is really clear in the front end whether it’s on/off and how to toggle its state.

I’ve got a command line switch which is the opposite. I don’t know which icon signifies on and off and in addition it has no means of displaying its state. I realise I would have to periodically ‘send’ the state from the host system if I wanted it to be up to date between HA reboots or if the state changed on the host system and NOT by command from Hass.

But is there any way I can make a command line switch display and work as an MQTT toggle switch?

Screenshot%202019-05-11%20at%2008

The correct method is to define the command_state (see here) so that HA knows if the switch is on or off.

The lazy method is to just use customize to define an assumed_state of false.

Thanks - I’d like to go the correct route - but does this mean I’d have to write a script that is constantly running on the source host waiting to answer the command_state query?

No, you need a script that returns whether the switch is on or off, that’s what you call.

1 Like

hmmm, thanks Tinkerer. My script at the moment returns “Value: NIGHT” or “Value: DAY” - is there some search term I should be Googling for how to convert those strings into binary? THANKS!

Define the value_template, something like

value_template: '{{ value == "Value: DAY" }}'

Assuming that Value: DAY means it’s on.

1 Like

Thanks, this worked a treat!

I want to join this thread. I had the same thing to do and following your tips I made it. Now I have command-line switch looking like real one and its state even refresh but very slow. I mean in very long tome after change is made from HA external device.

Is there any way to set “refresh time”? Or is any way to introduce weebhook to refresh “manually” its state by url call by external device?

Without knowing what your switch is like… it’s hard to help you.

There is a way to change how often some components refresh.

ok. thank you. thats my code for this switch:

switch:
  - platform: command_line
    switches:
      switch_name:
        command_on: "curl 'http://ip/something'"
        command_off: "curl 'http://ip/something'"
        command_state: "curl 'http://ip/something'"
        value_template: '{{ value == "1" }}'

should I just directly put piece of code as shown in https://www.home-assistant.io/docs/configuration/platform_options/#scan-interval ?

In under switch_name at the same level as command_on

Sorry, i don’t get it. More simple way please.

switch:
  - platform: command_line
    switches:
      switch_name:
        scan_interval: 5
        command_on: "curl 'http://ip/something'"
        ...

scan_interval is a configuration for switch_name

1 Like