SSH switch immediately switch back to previous position

Hey!

I’m created a command line switch what is create a SSH connection with a remote Debian and run a python command on it.

 switch:
  - platform: command_line
    switches:
      ssh_switch:
        command_on: "ssh [email protected] 'python3 110be.py' "
        command_off: "ssh [email protected] 'python3 110ki.py' "
        friendly_name: "Internet switch 110"

It’s working fine! But my switch in the UI looks like this: haos
My problem is when I click to the “Turn off” and “Turn on” the command runs fine but the indicator on the switch doesn’t stay there, immediately switch back to the off position.
How can I solve this issue?

Thanks for the support!

Please format code snippets correctly: How to help us help you - or How to ask a good question — looks like you tried but you need three backticks either side of a block of code.

Your switch definition has no way of knowing what state it is in. You need to specify command_state and perhaps value_template such that it queries the remote host on the state of the switch.

Thanks for your quick response!
Can you help me how can I give values well in my case? What kind of command do I need to run on the Debian to get the command_state? And what is the value_template than?
I’m sorry for the lot of question i’m really beginner in HA!
Thank you!

I have no idea what’s on the remote system. You need to create a script that returns something that tells you whether the thing you are switching is on or off. command_state will then run that command to read the state of your “switch” on the remote system.

The value_template lets you convert that returned message, so if your remote script (which I’ve called 110status.py, but you’ll have to create that) returns "be" or "ki", you’d do:

  command_state: "ssh [email protected] 'python3 110status.py' "
  value_template: "{{ (value == 'be') }}"

My python script is connects to a Juniper firewall and change a policy to block a classroom’s internet connection.
I have no idea how to check if a policy permitted or denied on the firewall and how to return with a value.

I have even less idea how to do that. Perhaps a ping or an iplookup on a blocked address?

Another option is to create a toggle helper (input_boolean) in HA, and an automation that flips your command_line switch — that will be making the assumption that your system is the only thing changing that firewall setting. If the firewall is changed outside HA, there is no feedback to HA if you don’t set command_state. Here’s an example automation, assuming you’ve set up a toggle helper called Juniper Firewall:

alias: Firewall switch
description: Turns the Juniper firewall on or off
id: 0be4a0d2-53b7-4137-8646-d16eb5702614

trigger:
  - platform: state
    entity_id: input_boolean.juniper_firewall
    to:
      - 'on'
      - 'off'

action:
  - service: switch.turn_{{ trigger.to_state.state }}
    entity_id: switch.ssh_switch