Switch Commandline

Hello everyone,

I haven’t been using Home Assistant for very long and now I’m trying to build the following:

I have a smart mirror with a Raspberry Pi in the hallway. It is connected to a TP Wi-Fi socket. I would now like to switch the mirror on and off via Home Assistant.

To switch it on, simply turn on the TP Wi-Fi socket. Then the Raspberry Pi and the screen turn on.

To switch it off, turn off the Raspberry Pi, wait for 1 minute, and then turn off the TP Wi-Fi socket.

A simple ping is sufficient to determine the status of the mirror.

So far, I have defined the following for this:

switch:
  - platform: command_line
    switches:
      switch_spiegel_rasp:
        friendly_name: "Flur_Spiegel"
        command_on: ""
        command_off: "ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] 'sudo /usr/sbin/shutdown -h now'"
        command_state: "ping -c 3 192.168.1.174"

ow can I access the Wi-Fi socket in HomeAssistant with command_on? It is also stored as an entity ID “switch.spiegel”.

Also, how can I set a sleep timer of 1 minute before turning off the Wi-Fi socket in command_off?

Do you have any ideas?

Best regards.
lnix

So, I would break things up a bit.

First, create a binary ping sensor that indicates the state of the Pi.

binary_sensor:
  # This creates binary_sensor.spiegel_pi
  - platform: ping
    host: 192.168.1.174
    name: Spiegel Pi
    scan_interval: 10  # default is 30

Then create a shell command that shuts down the Pi.

shell_command:
  # This creates service shell_command.spiegel_pi_shutdown
  spiegel_pi_shutdown: >
    ssh -i /config/ssh/id_rsa -o 'StrictHostKeyChecking=no' [email protected] 'sudo /usr/sbin/shutdown -h now'

Lastly, create a template switch that ties it all together:

switch:
  - platform: template
    switches:
      # This creates switch.flur_spiegel
      flur_spiegel:
        unique_id: flur_spiegel
        friendly_name: Flur Spiegel
        value_template: "{{ is_state('binary_sensor.spiegel_pi', 'on') }}"
        turn_on:
          - service: switch.turn_on
            entity_id: switch.spiegel
        turn_off:
          - service: shell_command.spiegel_pi_shutdown
          - delay:
              minutes: 1
          - service: switch.turn_off
            entity_id: switch.spiegel

The above should give you the basic functionality you’re asking for. However, it does have one flaw: i.e., if switch.flur_spiegel is turned off, and then turned back on before the turn_off sequence completes, it could leave things in an undesirable state.

That can be addressed by creating a script, and then using that script in the template switch.

script:
  # This creates script.spiegel_power
  spiegel_power:
    alias: Spiegel Power
    description: Turn Flur Spiegel on or off
    fields:
      value:
        name: Value
        description: Turn power on or off
        example: "off"
        required: true
        default: "on"
        selector:
          select:
            options:
              - "on"
              - "off"
    mode: queued
    sequence:
      - if: "{{ value == 'on' }}"
        then:
          - service: switch.turn_on
            entity_id: switch.spiegel
        else:
          - service: shell_command.spiegel_pi_shutdown
          - delay:
              minutes: 1
          - service: switch.turn_off
            entity_id: switch.spiegel

If the script is called to turn the power off, and then it is called again to turn the power on, but before the turn off sequence has completed, it will complete the turn off, and then do the turn on sequence. This is due to the chosen script mode, which is queued.

And the template switch will now look like this:

switch:
  - platform: template
    switches:
      # This creates switch.flur_spiegel
      flur_spiegel:
        unique_id: flur_spiegel
        friendly_name: Flur Spiegel
        value_template: "{{ is_state('binary_sensor.spiegel_pi', 'on') }}"
        turn_on:
          - service: script.turn_on
            target:
              entity_id: script.spiegel_power
            data:
              variables:
                value: "on"
        turn_off:
          - service: script.turn_on
            target:
              entity_id: script.spiegel_power
            data:
              variables:
                value: "off"

Hello Phil,

amazing. Thanks a lot. You are my hero of the day :slight_smile: