Incorporate "ping device" into script

Hello fellow smart homers! I have recently discovered it is possible to track device on/off states with ping in Home Assistant

binary_sensor:
- platform: ping
name: device_name
host: xx.xx.xx.xx
scan_interval: 15
count: 5

switch:
- platform: template
  switches:
    device_name:
      value_template: "{{ is_state('binary_sensor.device_name', 'on') }}"

But how do I incorporate that into my script such as this

tv_off_plex_off:
alias: Turn TV & Plex off
sequence:
- service: switch.turn_off
  data:
    entity_id: switch.tv_power

if “{{ is_state(‘binary_sensor.plex’, ‘on’) }}”

run

- service: switch.turn_off
data:
  entity_id: switch.plex_power

else

So that Plex only gets turned off if it’s on?

Add a condition to your script.

tv_off_plex_off:
  alias: Turn TV & Plex off
  sequence:
  - service: switch.turn_off
    entity_id: switch.tv_power
  - condition: state
    entity_id: binary_sensor.plex
    state: on
  - service: switch.turn_off
    entity_id: switch.plex_power

If the condition is not true, the script will stop.
Unlike in automations, conditions can be placed anywhere in a script.