Powering off a Pi - Check ping status before switching off?

I have an automation that turns off a Pi when I return home (an internal camera). At the moment I just issue a remote shutdown using ssh (ssh cam1 sudo power off), and then after 30 seconds I turn off the power via an Orvibo switch.

So that I don’t corrupt an SD card if shutting down takes longer than 30 seconds, I’d like to verify that the Pi has been shutdown via ping before then continuing with switching power off.

At the moment, my automation looks like this:

shell_command:
  turncam1off: ssh cam1 sudo poweroff
  turncam2off: ssh cam2 sudo poweroff

automation:
  - alias: "Homekit Home Mode"
    trigger:
      - platform: state
        entity_id: input_boolean.iamhome
        from: 'off'
        to: 'on'
    action:
      - service: shell_command.turncam1off
      - service: shell_command.turncam2off
      - delay:
          seconds: 30
      - service: switch.turn_off
        entity_id: switch.cam1
      - service: switch.turn_off
        entity_id: switch.cam2

My thoughts are to somehow integrate a binary_sensor in the action block to replace the delay line using a data_template and if statement, but I haven’t had much luck so far. Am I heading in the right direction?

binary_sensor:
  - platform: ping
    host: 192.168.0.5
    name: cam1

shell_command:
  turncam1off: ssh cam1 sudo poweroff
  turncam2off: ssh cam2 sudo poweroff

automation:
  - alias: "Homekit Home Mode"
    trigger:
      - platform: state
        entity_id: input_boolean.iamhome
        from: 'off'
        to: 'on'
    action:
      - service: shell_command.turncam1off
      - service: shell_command.turncam2off
      - service: switch.turn_off
      - data_template:
        entity_id: >
          {% if is_state('binary_sensor.cam1', 'off') %}
            switch.cam1
          {% endif %}

urgh, it’s all about getting those indents just right! :slight_smile: The following at least now passes the configuration checks, so onto actual testing. I’ll report back if it all works as hoped.

automation:
  - alias: "Homekit Home Mode"
    trigger:
      - platform: state
        entity_id: input_boolean.iamhome
        from: 'off'
        to: 'on'
    action:
      - service: shell_command.turncam1off
      - service: shell_command.turncam2off
      - service: switch.turn_off
        data_template:
          entity_id: >
            {% if is_state('binary_sensor.cam1', 'off') %}
              switch.cam1
            {% endif %}

It’s not quite working as hoped…

I ended up modifying the above yaml code because I wanted a delay of a few seconds after ping had stopped because the Pi is still shutting down, but it doesn’t seem to execute the wait_template or issue a switch off.

Could it be the binary_sensor isn’t the best way to monitor when a computer is no longer responding to ping? Or is wait_template not the right method?

    action:
      - service: shell_command.turncam1off
      - wait_template: "{{ states.binary_sensor.cam1.state == 'off' }}"
      - delay:
          seconds: 30
      - service: switch.turn_off
        entity_id: switch.cam1